-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI.java
More file actions
45 lines (39 loc) · 747 Bytes
/
AI.java
File metadata and controls
45 lines (39 loc) · 747 Bytes
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
public class AI implements AII {
EvaluationI ev;
private int move;
public AI(EvaluationI ev) {
this.ev = ev;
}
/*
* (non-Javadoc)
*
* @see AII#findMove(ConnectFourI, int)
*/
@Override
public int findMove(ConnectFourI node, int depth) {
negamax(node, depth);
return move;
}
/*
* (non-Javadoc)
*
* @see AII#negamax(ConnectFourI, int)
*/
@Override
public int negamax(ConnectFourI node, int depth) {
if(depth == 0 || node.isGameOver()){
return ev.evaluate(node, depth);
}
int max = -10000;
for(int col: node.getMoves()){
node.makeMove(col);
int score = -negamax(node, depth - 1);
node.undoMove();
if(score > max){
move = col;
max = score;
}
}
return max;
}
}