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
1 change: 1 addition & 0 deletions api/AIEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import boards.TicTacToeBoard;
import game.*;
import stateManager.DefensivePlacement;
import stateManager.OffensivePlacement;
import stateManager.Placement;

import java.util.Optional;
Expand Down
48 changes: 22 additions & 26 deletions api/RuleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import boards.TicTacToeBoard;
import game.*;
import stateManager.DefensivePlacement;
import stateManager.OffensivePlacement;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -28,37 +30,31 @@ public GameState getState(Board board){
}
}

public GameInfo getInfo(Board board) {
if (board instanceof TicTacToeBoard) {
TicTacToeBoard ticTacToeBoard = (TicTacToeBoard) board;
public GameInfo getInfo(CellBoard board) {
if (board instanceof TicTacToeBoard ticTacToeBoard) {
GameState gameState = getState(ticTacToeBoard);
final String [] players = new String[] {"X","O"};
Cell forkCell = null;
for (int index =0; index <2 ;index++) {
for(TicTacToeBoard.Symbol symbol : TicTacToeBoard.Symbol.values() ){
Player player = new Player(symbol.getSymbol());
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Player player = new Player(players [index]);
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++) {
forkCell= 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;
}
if(ticTacToeBoard.getSymbol(i,j)!=null) continue;
TicTacToeBoard ticTacToeBoard1 = ticTacToeBoard.dummyMove(new Move(player,new Cell(i,j)));
DefensivePlacement defensivePlacement = DefensivePlacement.get();
Optional<Cell> defensiveMove = defensivePlacement.place(ticTacToeBoard1,player.flip());
if(defensiveMove.isPresent()){
ticTacToeBoard1 = ticTacToeBoard1.dummyMove(new Move(player.flip(),defensiveMove.get()));
OffensivePlacement offensivePlacement = OffensivePlacement.get();
Optional<Cell> offensiveMove = offensivePlacement.place(ticTacToeBoard1,player);
if(offensiveMove.isPresent()){
return new GameInfoBuilder()
.isOver(gameState.isOver())
.winner(gameState.getWinner())
.hasFork(true)
.player(player)
.forkCell(new Cell(i,j))
.build();
}
}
if(canStillWin) {
return new GameInfoBuilder()
.isOver(gameState.isOver())
.winner(gameState.getWinner())
.hasFork(true)
.player(player.flip())
.forkCell(forkCell)
.build();
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions boards/TicTacToeBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ public class TicTacToeBoard implements CellBoard, Cloneable {
public TicTacToeBoard() {
}

public enum Symbol{
X("X"),O("O");

final String symbol;

Symbol (String o){
this.symbol = o;
}

public String getSymbol() {
return symbol;
}
}

private TicTacToeBoard(TicTacToeBoard board) {
for (int i = 0; i < 3; i++) {
this.cells[i] = Arrays.copyOf(board.cells[i], 3); // Deep copy of each row
Expand Down
2 changes: 1 addition & 1 deletion stateManager/CenterPlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CenterPlacement implements Placement{

private static CenterPlacement centerPlacement;

public synchronized static Placement get(){
public synchronized static CenterPlacement get(){
if(centerPlacement!= null) return centerPlacement;
return new CenterPlacement();
}
Expand Down
2 changes: 1 addition & 1 deletion stateManager/CornerPlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CornerPlacement implements Placement{

private static CornerPlacement cornerPlacement;

public synchronized static Placement get(){
public synchronized static CornerPlacement get(){
if(cornerPlacement!= null) return cornerPlacement;
return new CornerPlacement();
}
Expand Down
2 changes: 1 addition & 1 deletion stateManager/DefensivePlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DefensivePlacement implements Placement{

private static DefensivePlacement defensivePlacement;

public synchronized static Placement get(){
public synchronized static DefensivePlacement get(){
if(defensivePlacement!= null) return defensivePlacement;
return new DefensivePlacement();
}
Expand Down
3 changes: 2 additions & 1 deletion stateManager/ForkPlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ForkPlacement implements Placement{

private static ForkPlacement forkPlacement;

public synchronized static Placement get(){
public synchronized static ForkPlacement get(){
if(forkPlacement!= null) return forkPlacement;
return new ForkPlacement();
}
Expand All @@ -29,6 +29,7 @@ private Cell fork(Player player, TicTacToeBoard board) {
GameInfo gameInfo = ruleEngine.getInfo(board);
Cell forkCell = null;
if(gameInfo.hasAFork()){
System.out.println("GAME HAS A FORK AT -> "+ gameInfo.getForkCell().getRow() + ","+gameInfo.getForkCell().getCol()+" \n");
forkCell = gameInfo.getForkCell();
return forkCell;
}
Expand Down
2 changes: 1 addition & 1 deletion stateManager/OffensivePlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class OffensivePlacement implements Placement{
private static OffensivePlacement offensivePlacement;

public synchronized static Placement get(){
public synchronized static OffensivePlacement get(){
if(offensivePlacement!= null) return offensivePlacement;
return new OffensivePlacement();
}
Expand Down