mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Fixed major issue in game deepcopy
This commit is contained in:
24
app/src/main/java/org/toop/app/game/GameController.java
Normal file
24
app/src/main/java/org/toop/app/game/GameController.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package org.toop.app.game;
|
||||
|
||||
import org.toop.app.canvas.GameCanvas;
|
||||
import org.toop.app.game.TurnBasedGameThread;
|
||||
import org.toop.app.widget.view.GameView;
|
||||
|
||||
public abstract class GameController implements UpdatesGameUI {
|
||||
// Reference to primary view
|
||||
protected final GameView primary = new GameView(null, null, null);
|
||||
|
||||
// Reference to game canvas
|
||||
protected final GameCanvas canvas;
|
||||
|
||||
// Reference to gameThread
|
||||
protected TurnBasedGameThread gameThread;
|
||||
|
||||
protected GameController(GameCanvas canvas) {
|
||||
this.canvas = canvas;
|
||||
}
|
||||
|
||||
protected void setThread(TurnBasedGameThread gameThread){
|
||||
this.gameThread = gameThread;
|
||||
}
|
||||
}
|
||||
47
app/src/main/java/org/toop/app/game/TicTacToeController.java
Normal file
47
app/src/main/java/org/toop/app/game/TicTacToeController.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package org.toop.app.game;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.toop.app.App;
|
||||
import org.toop.app.canvas.TicTacToeCanvas;
|
||||
import org.toop.app.game.Players.LocalPlayer;
|
||||
import org.toop.app.game.Players.Player;
|
||||
import org.toop.app.widget.WidgetContainer;
|
||||
import org.toop.game.tictactoe.TicTacToeR;
|
||||
|
||||
public class TicTacToeController extends GameController {
|
||||
|
||||
public TicTacToeController(Player[] players) {
|
||||
TicTacToeR ticTacToeR = new TicTacToeR();
|
||||
super(new TicTacToeCanvas(Color.GRAY,
|
||||
(App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3,(c) -> {if (players[ticTacToeR.getCurrentTurn()] instanceof LocalPlayer lp) {lp.enqueueMove(c);}}));
|
||||
// TODO: Deal with this thread better. Can't give it to super because of "this" refence.
|
||||
setThread(new TurnBasedGameThread(players, ticTacToeR, this));
|
||||
|
||||
initUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUI() {
|
||||
canvas.clearAll();
|
||||
drawMoves();
|
||||
}
|
||||
|
||||
private void initUI(){
|
||||
primary.add(Pos.CENTER, canvas.getCanvas());
|
||||
WidgetContainer.getCurrentView().transitionNext(primary);
|
||||
}
|
||||
|
||||
private void drawMoves(){
|
||||
int[] board = gameThread.getBoard();
|
||||
|
||||
// Draw each move
|
||||
for (int i = 0; i < board.length; i++){
|
||||
switch(board[i]){
|
||||
case 0 -> canvas.drawChar('X', Color.RED, i);
|
||||
case 1 -> canvas.drawChar('O', Color.BLUE, i);
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,97 +1,75 @@
|
||||
package org.toop.app.game;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.toop.app.App;
|
||||
import org.toop.app.canvas.GameCanvas;
|
||||
import org.toop.app.canvas.TicTacToeCanvas;
|
||||
import org.toop.app.game.Players.LocalPlayer;
|
||||
import org.toop.app.game.Players.Player;
|
||||
import org.toop.app.widget.WidgetContainer;
|
||||
import org.toop.app.widget.view.GameView;
|
||||
|
||||
import org.toop.game.PlayResult;
|
||||
import org.toop.game.TurnBasedGameR;
|
||||
import org.toop.game.enumerators.GameState;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class TurnBasedGameThread implements Runnable {
|
||||
private final Player[] players; // List of players, can't be changed.
|
||||
private final TurnBasedGameR game; // Reference to game instance
|
||||
|
||||
private final AtomicBoolean isRunning = new AtomicBoolean();
|
||||
//private final GameController controller;
|
||||
|
||||
protected final GameView primary = new GameView(null, null, null);
|
||||
protected final GameCanvas canvas;
|
||||
|
||||
public TurnBasedGameThread(Player[] players, TurnBasedGameR game) {
|
||||
// Set reference to controller
|
||||
//this.controller = controller;
|
||||
private final AtomicBoolean isRunning = new AtomicBoolean(true);
|
||||
private final GameController controller;
|
||||
|
||||
public TurnBasedGameThread(Player[] players, TurnBasedGameR game, GameController controller) {
|
||||
// Make sure player list matches expected size
|
||||
if (players.length != game.getPlayerCount()){
|
||||
throw new IllegalArgumentException("players and game's players must have same length");
|
||||
}
|
||||
|
||||
// Set vars
|
||||
this.controller = controller;
|
||||
this.players = players;
|
||||
this.game = game;
|
||||
|
||||
// Create and run thread
|
||||
Thread thread = new Thread(this::run);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
// UI SHIZ TO MOVE
|
||||
canvas = new TicTacToeCanvas(Color.GRAY,
|
||||
(App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3,(c) -> {if (players[game.getCurrentTurn()] instanceof LocalPlayer lp) {lp.enqueueMove(c);}});
|
||||
primary.add(Pos.CENTER, canvas.getCanvas());
|
||||
WidgetContainer.getCurrentView().transitionNext(primary);
|
||||
|
||||
public int[] getBoard(){
|
||||
return game.getBoard();
|
||||
}
|
||||
|
||||
public Player[] getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
// Move to UI shiz
|
||||
private void drawMove(int move) {
|
||||
if (game.getCurrentTurn() == 1){
|
||||
canvas.drawChar('X', Color.RED, move);
|
||||
}
|
||||
else{
|
||||
canvas.drawChar('O', Color.RED, move);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
isRunning.set(true);
|
||||
|
||||
// Game logic loop
|
||||
while(isRunning.get()) {
|
||||
|
||||
// Get current player
|
||||
Player currentPlayer = players[game.getCurrentTurn()];
|
||||
System.out.println(game.getCurrentTurn() + "'s turn");
|
||||
|
||||
// Get this player's valid moves
|
||||
int[] validMoves = game.getLegalMoves();
|
||||
|
||||
// Get player's move, reask if Move is invalid
|
||||
// Make sure provided move is valid
|
||||
// TODO: Limit amount of retries?
|
||||
int move = currentPlayer.getMove(game.clone());
|
||||
while (!contains(validMoves, move)) {
|
||||
move = currentPlayer.getMove(game.clone());
|
||||
}
|
||||
// Make move
|
||||
System.out.println(Arrays.toString(game.getBoard()));
|
||||
GameState state = game.play(move);
|
||||
drawMove(move);
|
||||
|
||||
// Make move
|
||||
PlayResult result = game.play(move);
|
||||
|
||||
// Tell controller to update UI
|
||||
controller.updateUI();
|
||||
GameState state = result.state();
|
||||
if (state != GameState.NORMAL) {
|
||||
if (state == GameState.WIN) {
|
||||
// Someone won
|
||||
// Win, do something
|
||||
System.out.println(result.winner() + " WON");
|
||||
} else if (state == GameState.DRAW) {
|
||||
// THere was a draw
|
||||
// Draw, do something
|
||||
System.out.println("DRAW");
|
||||
}
|
||||
System.out.println(state);
|
||||
isRunning.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
5
app/src/main/java/org/toop/app/game/UpdatesGameUI.java
Normal file
5
app/src/main/java/org/toop/app/game/UpdatesGameUI.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package org.toop.app.game;
|
||||
|
||||
public interface UpdatesGameUI {
|
||||
void updateUI();
|
||||
}
|
||||
@@ -36,7 +36,7 @@ public class LocalMultiplayerView extends ViewWidget {
|
||||
}
|
||||
|
||||
switch (information.type) {
|
||||
case TICTACTOE -> new TurnBasedGameThread(new Player[]{new LocalPlayer(), new ArtificialPlayer<>(new TicTacToeAIR())}, new TicTacToeR());
|
||||
case TICTACTOE -> new TicTacToeController(new Player[]{new LocalPlayer(), new ArtificialPlayer<>(new TicTacToeAIR())});
|
||||
case REVERSI -> new ReversiGame(information);
|
||||
case CONNECT4 -> new Connect4Game(information);
|
||||
// case BATTLESHIP -> new BattleshipGame(information);
|
||||
|
||||
Reference in New Issue
Block a user