-
Notifications
You must be signed in to change notification settings - Fork 0
/
Evaluator.java
80 lines (60 loc) · 1.85 KB
/
Evaluator.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
class Evaluator {
private int maxPlayer;
private int minPlayer;
Evaluator(int maxPlayer, int minPlayer) {
this.maxPlayer = maxPlayer;
this.minPlayer = minPlayer;
}
/**
* Evaluate the current state
* Different evaluation is used for EOG and MOG
*/
int evaluateMove(GameState gameState) {
if (gameState.isEOG()) {
return evalueEOG(gameState);
}
return evaluateMOG(gameState);
}
/**
* Evaluate the middle of game state based on max and min pieces
*/
private int evaluateMOG(GameState gameState) {
int maxPieces = 0;
int minPieces = 0;
for (int position = 0; position < GameState.NUMBER_OF_SQUARES; position++) {
int piece = gameState.get(position);
if (piece == maxPlayer) {
maxPieces++;
}
if (piece == maxPlayer + 4) {
maxPieces += 2;
}
if (piece == minPlayer) {
minPieces++;
}
if (piece == minPlayer + 4) {
minPieces += 2;
}
}
return maxPieces - minPieces;
}
/**
* Evaluate the end of game state
*/
private int evalueEOG(GameState state) {
if (maxPlayer == Constants.CELL_WHITE && state.isWhiteWin()) {
return Integer.MAX_VALUE;
}
if (maxPlayer == Constants.CELL_RED && state.isRedWin()) {
return Integer.MAX_VALUE;
}
if (minPlayer == Constants.CELL_WHITE && state.isWhiteWin()) {
return Integer.MIN_VALUE;
}
if (minPlayer == Constants.CELL_RED && state.isRedWin()) {
return Integer.MIN_VALUE;
}
//draw
return 0;
}
}