-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOthelloGame.java
83 lines (76 loc) · 2.49 KB
/
OthelloGame.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.*;
public class OthelloGame {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
Player human = new Player(1);
Player robot = new Player(-1);
Game othello = new Game(human, robot);
int depth = 7;
int[][] start = othello.getGameState().getStateMatrix();
// int timeLimit = timeLimit(sc).longValue();
// if(timeLimit <= 1) {
// depth = 1;
// } else if(timeLimit <= 5){
// depth = 5;
// } else if(timeLimit > 5){
// depth = 7;
// }
plotCurrentState(start);
System.out.print("\n");
Node rootNode = new Node(othello.getGameState());
createChildren(depth,othello.getCurrentPlayer().getId(), rootNode);
while(!othello.isFinished()) {
Player p = othello.getCurrentPlayer();
Point action = p.chooseAction(rootNode, depth);
for(Node n : rootNode.getChildren()){
if(n.getNodeState().getLastPlacedRow() == action.getRow() && n.getNodeState().getLastPlacedCol() == action.getCol()){
rootNode = n;
}
}
createChildren(depth, p.getId(), rootNode);
if(p.getId() == robot.getId()){
System.out.println(action.getRow()+", "+action.getCol());
System.out.print("\n");
}
plotCurrentState(rootNode.getNodeState().getStateMatrix());
othello.updateGame();
}
}
private static void createChildren(int depth, int id, Node parent) {
ArrayList<Point> possibleMoves = parent.getNodeState().possibleActions(id);
if(depth == 0) {
return;
} else {
if(parent.getChildren().size() == 0){
for(Point move : possibleMoves) {
State childState = new State(parent.getNodeState().getUpdatedStateMatrix(move, id), move.getRow(), move.getCol());
Node childNode = new Node(childState);
parent.addChildren(childNode);
}
for(Node n : parent.getChildren()) {
createChildren(depth - 1, -id, n);
}
}
}
}
private static int timeLimit(Scanner sc){
Integer timeLimit = 10^6;
System.out.println("Please enter the number of seconds the computer is allowed to think: ");
timeLimit = sc.nextInt();
return timeLimit;
}
private static void plotCurrentState(int[][] currentMatrix){
for(int i = 0; i <=7; i++){
for(int j = 0; j <= 7; j++){
if(currentMatrix[i][j] == -1) {
System.out.print("w"+" ");
} else if(currentMatrix[i][j] == 1){
System.out.print("b"+" ");
} else {
System.out.print(currentMatrix[i][j]+" ");
}
}
System.out.print("\n");
}
}
}