-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLookAheadAIPlayer.java
More file actions
85 lines (77 loc) · 2.12 KB
/
Copy pathLookAheadAIPlayer.java
File metadata and controls
85 lines (77 loc) · 2.12 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
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
84
85
import java.util.*;
public class LookAheadAIPlayer extends Player {
// The number of the player (1 or 2)
int thisPlayer;
int lookahead;
public LookAheadAIPlayer(int thisPlayer, int lookahead) {
this.thisPlayer = thisPlayer;
this.lookahead = lookahead;
}
public String callSign() {
switch(lookahead) {
case(2): return "Easy AI";
case(4): return "Medium AI";
case(6): return "Hard AI";
case(8): return "Tournament AI";
}
//This is for you to experiment with if you want.
return "Custom AI";
}
/*
* Featurea:
* - Negamax
* - Alpha-beta pruning
* - Random between equally good moves
*/
public int decideMove(Board b) {
ArrayList<Integer> moveList = b.validMoves();
Collections.shuffle(moveList);
int tempVal, bestVal = -100, bestMove = -3;
for (int m: moveList) {
b.makeMove(m);
tempVal = -negaMax(b, 1, -100, 100);
b.revert(b.numTotalMoves()-1);
//System.out.println("move "+m+ " val "+ tempVal);
if (tempVal > bestVal) {
bestVal = tempVal;
bestMove = m;
}
}
return bestMove;
}
private int negaMax(Board b, int depth, int lowerBound, int upperBound) {
// Terminal Nodes
if (b.isWinning() != 0 || depth >= lookahead) {
return (depth%2==0? 1 : -1) * heuristicEva(b, depth);
}
// Normal Nodes
int bestVal = -100, tempVal;
ArrayList<Integer> moveList = b.validMoves();
Collections.shuffle(moveList);
// Search every sub-tree
for (int m: moveList) {
b.makeMove(m);
tempVal = -negaMax(b, depth+1, -upperBound, -lowerBound);
b.revert(b.numTotalMoves()-1); // undo the move just made
if (tempVal > bestVal) {
bestVal = tempVal;
}
if (tempVal > lowerBound) {
lowerBound = tempVal;
}
if (lowerBound >= upperBound) {
break;
}
}
return bestVal;
}
private int heuristicEva(Board b, int depth) {
if (b.isWinning() == thisPlayer) {
return 100 - depth;
} else if (b.isWinning() == 3 - thisPlayer) {
return depth - 100;
} else {
return depth;
}
}
}