-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefensivePlacement.java
More file actions
46 lines (36 loc) · 1.19 KB
/
DefensivePlacement.java
File metadata and controls
46 lines (36 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package stateManager;
import boards.TicTacToeBoard;
import game.Board;
import game.Cell;
import game.Move;
import game.Player;
import java.util.Optional;
public class DefensivePlacement implements Placement{
private static DefensivePlacement defensivePlacement;
public synchronized static Placement get(){
if(defensivePlacement!= null) return defensivePlacement;
return new DefensivePlacement();
}
@Override
public Optional<Cell> place(TicTacToeBoard board, Player player){
return Optional.ofNullable(defense(player,board));
}
@Override
public Placement next(){
return OffensivePlacement.get();
}
private Cell defense(Player player, TicTacToeBoard board) {
for (int i = 0; i < 3; i++) {
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.dummyMove(move);
if (ruleEngine.getState(boardCopy).isOver()) {
return new Cell(i,j);
}
}
}
}
return null;
}
}