From 3ca4dd1b58e6859a89bba5221b273fb3c749c225 Mon Sep 17 00:00:00 2001 From: Sufyan Date: Wed, 26 Feb 2025 21:17:42 +0530 Subject: [PATCH] SUF-14 Good Engineer2: New Feature - History of the Board --- Main.java | 7 +++++- api/AIEngine.java | 6 ++--- api/GameEngine.java | 2 +- api/RuleEngine.java | 6 ++--- boards/History.java | 35 ++++++++++++++++++++++++++++ boards/TicTacToeBoard.java | 31 ++++++++++++++++++++++-- game/Board.java | 3 ++- stateManager/DefensivePlacement.java | 3 +-- stateManager/OffensivePlacement.java | 3 +-- 9 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 boards/History.java diff --git a/Main.java b/Main.java index 88322bc..f0d48b7 100644 --- a/Main.java +++ b/Main.java @@ -1,6 +1,8 @@ import api.AIEngine; import api.GameEngine; import api.RuleEngine; +import boards.History; +import boards.TicTacToeBoard; import game.Board; import game.Cell; import game.Move; @@ -32,7 +34,10 @@ public static void main(String[] args) { System.out.println(board); } } - + if(board instanceof TicTacToeBoard board1){ + History boardHistory = board1.getHistory(); + boardHistory.printHistory(); + } System.out.println("Game winner is " + ruleEngine.getState(board).getWinner()); } diff --git a/api/AIEngine.java b/api/AIEngine.java index b8cf37f..61c7e5d 100644 --- a/api/AIEngine.java +++ b/api/AIEngine.java @@ -27,8 +27,7 @@ private Cell offense(Player player, TicTacToeBoard board) { if (board.getSymbol(i,j)==null){ Move move = new Move(player.flip(), new Cell(i,j)); - Board boardCopy = board.clone(); - boardCopy.move(move); + Board boardCopy = board.dummyMove(move); if(ruleEngine.getState(boardCopy).isOver()){ return new Cell(i,j); } @@ -43,8 +42,7 @@ private Cell defense(Player player, TicTacToeBoard board) { for (int j = 0; j < 3; j++) { if (board.getSymbol(i, j) == null) { Move move = new Move(player.flip(), new Cell(i,j)); - Board boardCopy = board.clone(); - boardCopy.move(move); + Board boardCopy = board.dummyMove(move); if (ruleEngine.getState(boardCopy).isOver()) { return new Cell(i,j); } diff --git a/api/GameEngine.java b/api/GameEngine.java index 54fc372..bc6fa4e 100644 --- a/api/GameEngine.java +++ b/api/GameEngine.java @@ -14,7 +14,7 @@ public Board start(String type){ public void move(Board board, Move move) { if(board instanceof TicTacToeBoard board1){ - board1.move(move); + board1.move(move); } else{ throw new IllegalArgumentException(); } diff --git a/api/RuleEngine.java b/api/RuleEngine.java index 87a5c38..5a095ae 100644 --- a/api/RuleEngine.java +++ b/api/RuleEngine.java @@ -37,15 +37,13 @@ public GameInfo getInfo(Board board) { for (int index =0; index <2 ;index++) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - Board b = ((TicTacToeBoard) board).clone(); Player player = new Player(players [index]); - b.move(new Move(player,new Cell(i,j))); + TicTacToeBoard b = ((TicTacToeBoard) board).dummyMove(new Move(player,new Cell(i,j))); boolean canStillWin = false; for (int k = 0; k < 3; k++) { for (int l = 0; l < 3; l++) { - Board b1 = ((TicTacToeBoard) b).clone(); forkCell= new Cell(k,l); - b1.move(new Move(player.flip(),new Cell(k,l))); + Board b1 = b.dummyMove(new Move(player.flip(),new Cell(k,l))); if(getState(b1).getWinner().equals(player.flip().symbol())){ canStillWin = true; break; diff --git a/boards/History.java b/boards/History.java new file mode 100644 index 0000000..c3ebf70 --- /dev/null +++ b/boards/History.java @@ -0,0 +1,35 @@ +package boards; + +import game.Board; + +import java.util.ArrayList; +import java.util.List; + +public class History { + + List history = new ArrayList<>(); + + public void add (Board board){ + history.add(board); + } + + public Board undo(){ + history.removeLast(); + return history.getLast(); + } + + public Board getHistoryAtPos(int index){ + int n = history.size(); + for(int i=index+1;i " + board1); + } + } +} diff --git a/boards/TicTacToeBoard.java b/boards/TicTacToeBoard.java index 7398f9e..0e5f7bb 100644 --- a/boards/TicTacToeBoard.java +++ b/boards/TicTacToeBoard.java @@ -10,6 +10,7 @@ public class TicTacToeBoard implements CellBoard, Cloneable { String[][] cells = new String[3][3]; + History history = new History(); public TicTacToeBoard() { } @@ -19,7 +20,12 @@ private TicTacToeBoard(TicTacToeBoard board) { this.cells[i] = Arrays.copyOf(board.cells[i], 3); // Deep copy of each row } } - public String getSymbol(int i,int j){ + + public History getHistory() { + return history; + } + + public String getSymbol(int i, int j){ return this.cells[i][j]; } @@ -40,9 +46,30 @@ public static RuleSet getRules(){ rules.add(new Rule (TicTacToeBoard::countMoves)); return rules; } + + @Override + public TicTacToeBoard copy() { + TicTacToeBoard ticTacToeBoard = new TicTacToeBoard(); + for (int i = 0; i < 3; i++) { + System.arraycopy(cells[i], 0, ticTacToeBoard.cells[i], 0, 3); + } + ticTacToeBoard.history = history; + return ticTacToeBoard; + } + + @Override - public void move(Move move){ + public TicTacToeBoard move(Move move){ + TicTacToeBoard boardCopy = copy(); + history.add(boardCopy); setCell(move.getCell(),move.getPlayer().symbol()); + return this; + } + + public TicTacToeBoard dummyMove(Move move){ + TicTacToeBoard boardCopy = copy(); + boardCopy.setCell(move.getCell(),move.getPlayer().symbol()); + return boardCopy; } @Override diff --git a/game/Board.java b/game/Board.java index 1f232df..ac47885 100644 --- a/game/Board.java +++ b/game/Board.java @@ -1,5 +1,6 @@ package game; public interface Board { - void move(Move move); + Board move(Move move); + Board copy(); } diff --git a/stateManager/DefensivePlacement.java b/stateManager/DefensivePlacement.java index 8729be6..c1f45e2 100644 --- a/stateManager/DefensivePlacement.java +++ b/stateManager/DefensivePlacement.java @@ -33,8 +33,7 @@ private Cell defense(Player player, TicTacToeBoard board) { for (int j = 0; j < 3; j++) { if (board.getSymbol(i, j) == null) { Move move = new Move(player.flip(), new Cell(i,j)); - Board boardCopy = board.clone(); - boardCopy.move(move); + Board boardCopy = board.dummyMove(move); if (ruleEngine.getState(boardCopy).isOver()) { return new Cell(i,j); } diff --git a/stateManager/OffensivePlacement.java b/stateManager/OffensivePlacement.java index e7abf22..dc82d61 100644 --- a/stateManager/OffensivePlacement.java +++ b/stateManager/OffensivePlacement.java @@ -31,8 +31,7 @@ private Cell offense(Player player, TicTacToeBoard board) { if (board.getSymbol(i,j)==null){ Move move = new Move(player.flip(), new Cell(i,j)); - Board boardCopy = board.clone(); - boardCopy.move(move); + Board boardCopy = board.dummyMove(move); if(ruleEngine.getState(boardCopy).isOver()){ return new Cell(i,j); }