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
4 changes: 2 additions & 2 deletions api/AIEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Move getSmartMove(Player player, TicTacToeBoard board){
for (int j = 0; j < 3; j++) {
if (board.getSymbol(i,j)==null){
Move move = new Move(player, new Cell(i,j));
Board boardCopy = board.copy();
Board boardCopy = board.clone();
boardCopy.move(move);
if(ruleEngine.getState(boardCopy).isOver()){
return move;
Expand All @@ -38,7 +38,7 @@ public Move getSmartMove(Player player, TicTacToeBoard board){
if (board.getSymbol(i,j)==null){
Move move = new Move(player.flip(), new Cell(i,j));

Board boardCopy = board.copy();
Board boardCopy = board.clone();
boardCopy.move(move);
if(ruleEngine.getState(boardCopy).isOver()){
return new Move(player, new Cell(i,j));
Expand Down
30 changes: 14 additions & 16 deletions boards/TicTacToeBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@

import java.util.Arrays;

public class TicTacToeBoard implements Board {
public class TicTacToeBoard implements Board, Cloneable {
String[][] cells = new String[3][3];

public TicTacToeBoard() {
}

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
}
}
public String getSymbol(int i,int j){
return this.cells[i][j];
}
Expand All @@ -21,21 +29,6 @@ public void setCell(Cell cell, String symbol) {
}
}

@Override
public TicTacToeBoard copy(){
TicTacToeBoard ticTacToeBoard = new TicTacToeBoard();

for(int i=0;i<3;i++){
ticTacToeBoard.cells[i] = Arrays.copyOf(this.cells[i], 3); // This the deep copy, no reference to other cells
/*
for(int j=0;j<3;j++){ This is the shallow copy and contains the reference of original board
ticTacToeBoard.cells[i][j] = cells[i][j];
}
*/
}
return ticTacToeBoard;
}

@Override
public void move(Move move){
setCell(move.getCell(),move.getPlayer().symbol());
Expand All @@ -53,4 +46,9 @@ public String toString(){
}
return result.toString();
}

@Override
public TicTacToeBoard clone() {
return new TicTacToeBoard(this);
}
}
2 changes: 1 addition & 1 deletion game/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface Board {
public abstract void move(Move move);
public abstract Board copy();
// public abstract Board copy();
}
1 change: 0 additions & 1 deletion test/GamePlayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import game.Move;
import game.Player;

import java.util.Scanner;

import org.junit.Assert;
import org.junit.Before;
Expand Down