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:
@@ -1,17 +0,0 @@
|
||||
/*package org.toop.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 {
|
||||
// TODO: Seperate this from game Thread
|
||||
protected final GameView primary = new GameView(null, null, null);
|
||||
protected final GameCanvas canvas;
|
||||
protected final TurnBasedGameThread gameThread;
|
||||
|
||||
protected GameController(GameCanvas canvas, TurnBasedGameThread gameThread) {
|
||||
this.gameThread = gameThread;
|
||||
this.canvas = canvas;
|
||||
}
|
||||
}*/
|
||||
6
game/src/main/java/org/toop/game/PlayResult.java
Normal file
6
game/src/main/java/org/toop/game/PlayResult.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package org.toop.game;
|
||||
|
||||
import org.toop.game.enumerators.GameState;
|
||||
|
||||
public record PlayResult(GameState state, int winner) {
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*package org.toop.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.TurnBasedGameThread;
|
||||
import org.toop.app.widget.WidgetContainer;
|
||||
|
||||
public class TicTacToeController extends GameController {
|
||||
|
||||
public TicTacToeController() {
|
||||
super(new TicTacToeCanvas(Color.GRAY,
|
||||
(App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3,(c) -> {if (players[game.getCurrentTurn()] instanceof LocalPlayer lp) {lp.enqueueMove(c);}}), new TurnBasedGameThread());
|
||||
primary.add(Pos.CENTER, canvas.getCanvas());
|
||||
WidgetContainer.getCurrentView().transitionNext(primary));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUI() {
|
||||
|
||||
}
|
||||
}*/
|
||||
@@ -12,6 +12,7 @@ public abstract class TurnBasedGameR extends GameR {
|
||||
protected TurnBasedGameR(TurnBasedGameR other){
|
||||
super(other);
|
||||
this.playerCount = other.playerCount;
|
||||
this.turn = other.turn;
|
||||
}
|
||||
|
||||
public int getPlayerCount(){return this.playerCount;}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.toop.game;
|
||||
|
||||
public interface UpdatesGameUI {
|
||||
void updateUI();
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.toop.game.interfaces;
|
||||
|
||||
import org.toop.game.PlayResult;
|
||||
import org.toop.game.enumerators.GameState;
|
||||
|
||||
/**
|
||||
@@ -26,5 +27,5 @@ public interface IPlayableR {
|
||||
* @param move the move to play, represented as an integer
|
||||
* @return the {@link GameState} after the move is played
|
||||
*/
|
||||
GameState play(int move);
|
||||
PlayResult play(int move);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package org.toop.game.tictactoe;
|
||||
|
||||
import org.toop.game.AIR;
|
||||
import org.toop.game.PlayResult;
|
||||
import org.toop.game.enumerators.GameState;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* AI implementation for playing Tic-Tac-Toe.
|
||||
* <p>
|
||||
@@ -30,13 +33,14 @@ public final class TicTacToeAIR extends AIR<TicTacToeR> {
|
||||
public int findBestMove(TicTacToeR game, int depth) {
|
||||
assert game != null;
|
||||
assert depth >= 0;
|
||||
|
||||
final int[] legalMoves = game.getLegalMoves();
|
||||
|
||||
// If there are no moves, return -1
|
||||
if (legalMoves.length == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If first move, pick a corner
|
||||
if (legalMoves.length == 9) {
|
||||
return switch ((int)(Math.random() * 4)) {
|
||||
case 0 -> legalMoves[2];
|
||||
@@ -49,6 +53,7 @@ public final class TicTacToeAIR extends AIR<TicTacToeR> {
|
||||
int bestScore = -depth;
|
||||
int bestMove = -1;
|
||||
|
||||
// Calculate Move score of each move, keep track what moves had the best score
|
||||
for (final int move : legalMoves) {
|
||||
final int score = getMoveScore(game, depth, move, true);
|
||||
|
||||
@@ -57,7 +62,6 @@ public final class TicTacToeAIR extends AIR<TicTacToeR> {
|
||||
bestScore = score;
|
||||
}
|
||||
}
|
||||
|
||||
return bestMove != -1 ? bestMove : legalMoves[(int)(Math.random() * legalMoves.length)];
|
||||
}
|
||||
|
||||
@@ -72,7 +76,9 @@ public final class TicTacToeAIR extends AIR<TicTacToeR> {
|
||||
*/
|
||||
private int getMoveScore(TicTacToeR game, int depth, int move, boolean maximizing) {
|
||||
final TicTacToeR copy = game.clone();
|
||||
final GameState state = copy.play(move);
|
||||
final PlayResult result = copy.play(move);
|
||||
|
||||
GameState state = result.state();
|
||||
|
||||
switch (state) {
|
||||
case DRAW: return 0;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.toop.game.tictactoe;
|
||||
|
||||
import org.toop.game.PlayResult;
|
||||
import org.toop.game.TurnBasedGameR;
|
||||
import org.toop.game.enumerators.GameState;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class TicTacToeR extends TurnBasedGameR {
|
||||
@@ -23,72 +23,75 @@ public final class TicTacToeR extends TurnBasedGameR {
|
||||
@Override
|
||||
public int[] getLegalMoves() {
|
||||
final ArrayList<Integer> legalMoves = new ArrayList<Integer>();
|
||||
final char currentValue = getCurrentValue();
|
||||
|
||||
for (int i = 0; i < this.getBoard().length; i++) {
|
||||
if (Objects.equals(this.getBoard()[i], EMPTY)) {
|
||||
legalMoves.add(i);
|
||||
}
|
||||
}
|
||||
System.out.println(Arrays.toString(legalMoves.stream().mapToInt(Integer::intValue).toArray()));
|
||||
return legalMoves.stream().mapToInt(Integer::intValue).toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameState play(int move) {
|
||||
public PlayResult play(int move) {
|
||||
assert move >= 0 && move < this.getBoard().length;
|
||||
|
||||
// TODO: Make sure this move is allowed, maybe on the board side?
|
||||
this.setBoard(move);
|
||||
movesLeft--;
|
||||
nextTurn();
|
||||
|
||||
if (checkForWin()) {
|
||||
return GameState.WIN;
|
||||
int t = checkForWin();
|
||||
if (t != -1) {
|
||||
return new PlayResult(GameState.WIN, t);
|
||||
}
|
||||
|
||||
if (movesLeft <= 2) {
|
||||
if (movesLeft <= 0 || checkForEarlyDraw()) {
|
||||
return GameState.DRAW;
|
||||
return new PlayResult(GameState.DRAW, EMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
return GameState.NORMAL;
|
||||
nextTurn();
|
||||
return new PlayResult(GameState.NORMAL, EMPTY);
|
||||
}
|
||||
|
||||
private boolean checkForWin() {
|
||||
private int checkForWin() {
|
||||
// Horizontal
|
||||
for (int i = 0; i < 3; i++) {
|
||||
|
||||
final int index = i * 3;
|
||||
|
||||
if (!Objects.equals(this.getBoard()[index], EMPTY)
|
||||
&& Objects.equals(this.getBoard()[index], this.getBoard()[index + 1])
|
||||
&& Objects.equals(this.getBoard()[index], this.getBoard()[index + 2])) {
|
||||
return true;
|
||||
return this.getBoard()[index];
|
||||
}
|
||||
}
|
||||
|
||||
// Vertical
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (!Objects.equals(this.getBoard()[i], EMPTY) && Objects.equals(this.getBoard()[i], this.getBoard()[i + 3]) && Objects.equals(this.getBoard()[i], this.getBoard()[i + 6])) {
|
||||
return true;
|
||||
return this.getBoard()[i];
|
||||
}
|
||||
}
|
||||
|
||||
// B-Slash
|
||||
if (!Objects.equals(this.getBoard()[0], EMPTY) && Objects.equals(this.getBoard()[0], this.getBoard()[4]) && Objects.equals(this.getBoard()[0], this.getBoard()[8])) {
|
||||
return true;
|
||||
return this.getBoard()[0];
|
||||
}
|
||||
|
||||
// F-Slash
|
||||
return !Objects.equals(this.getBoard()[2], EMPTY) && Objects.equals(this.getBoard()[2], this.getBoard()[4]) && Objects.equals(this.getBoard()[2], this.getBoard()[6]);
|
||||
if (!Objects.equals(this.getBoard()[2], EMPTY) && Objects.equals(this.getBoard()[2], this.getBoard()[4]) && Objects.equals(this.getBoard()[2], this.getBoard()[6]))
|
||||
return this.getBoard()[2];
|
||||
|
||||
// Default return
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
private boolean checkForEarlyDraw() {
|
||||
for (final int move : this.getLegalMoves()) {
|
||||
final TicTacToeR copy = this.clone();
|
||||
|
||||
if (copy.play(move) == GameState.WIN || !copy.checkForEarlyDraw()) {
|
||||
if (copy.play(move).state() == GameState.WIN || !copy.checkForEarlyDraw()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -96,10 +99,6 @@ public final class TicTacToeR extends TurnBasedGameR {
|
||||
return true;
|
||||
}
|
||||
|
||||
private char getCurrentValue() {
|
||||
return this.getCurrentTurn() == 0 ? 'X' : 'O';
|
||||
}
|
||||
|
||||
@Override
|
||||
public TicTacToeR clone() {
|
||||
return new TicTacToeR(this);
|
||||
|
||||
Reference in New Issue
Block a user