Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Main.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
6 changes: 2 additions & 4 deletions api/AIEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion api/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 2 additions & 4 deletions api/RuleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions boards/History.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package boards;

import game.Board;

import java.util.ArrayList;
import java.util.List;

public class History {

List<Board> 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<n;i++){
history.removeLast();
}
return history.getLast();
}

public void printHistory(){
for(int i=0;i<history.size();i++){
Board board1 = history.get(i);
System.out.println("board at index "+ i + "is -> " + board1);
}
}
}
31 changes: 29 additions & 2 deletions boards/TicTacToeBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

public class TicTacToeBoard implements CellBoard, Cloneable {
String[][] cells = new String[3][3];
History history = new History();

public TicTacToeBoard() {
}
Expand All @@ -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];
}

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion game/Board.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package game;

public interface Board {
void move(Move move);
Board move(Move move);
Board copy();
}
3 changes: 1 addition & 2 deletions stateManager/DefensivePlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions stateManager/OffensivePlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down