-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleEvaluator.java
More file actions
125 lines (116 loc) · 4.86 KB
/
SimpleEvaluator.java
File metadata and controls
125 lines (116 loc) · 4.86 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package Evaluator;
public class SimpleEvaluator {
public static int[][] whiteToBlackTable(int[][] table) {
int[][] new_table = new int[Values.boardSize][Values.boardSize];
for(int i = 0; i < table.length / 2; i++)
{
int[] temp = table[i];
new_table[i] = table[table.length - i - 1];
new_table[table.length - i - 1] = temp;
}
return new_table;
}
public static boolean isEndGame(char[][] chessBoard) {
int queenNum = 0;
for (int row = 0; row < Values.boardSize; row++) {
for (int col = 0; col < Values.boardSize; col++) {
if (chessBoard[row][col] == 'q' || chessBoard[row][col] == 'Q') {
queenNum++;
}
if (queenNum > 0) {
return false;
}
}
}
return true;
}
public static int whiteEvaluation(char[][] chessBoard) {
int evaluationTotal = 0;
for (int row = 0; row < Values.boardSize; row++) {
for (int col = 0; col < Values.boardSize; col++) {
if (chessBoard[row][col] == 'p') {
evaluationTotal += Values.pawnValue;
evaluationTotal += Values.pawnPositionTable[row][col];
} else if (chessBoard[row][col] == 'n') {
evaluationTotal += Values.knightValue;
evaluationTotal += Values.knightPositionTable[row][col];
} else if (chessBoard[row][col] == 'b') {
evaluationTotal += Values.bishopValue;
evaluationTotal += Values.bishopPositionTable[row][col];
} else if (chessBoard[row][col] == 'r') {
evaluationTotal += Values.rookValue;
evaluationTotal += Values.rookPositionTable[row][col];
} else if (chessBoard[row][col] == 'q') {
evaluationTotal += Values.queenValue;
evaluationTotal += Values.queenPositionTable[row][col];
} else if (chessBoard[row][col] == 'k') {
evaluationTotal += Values.kingValue;
if (isEndGame(chessBoard)) {
evaluationTotal += Values.kingPositionTableEndGame[row][col];
} else {
evaluationTotal += Values.kingPositionTableMidGame[row][col];
}
}
}
}
return evaluationTotal;
}
public static int blackEvaluation(char[][] chessBoard) {
int evaluationTotal = 0;
for (int row = 0; row < Values.boardSize; row++) {
for (int col = 0; col < Values.boardSize; col++) {
if (chessBoard[row][col] == 'P') {
evaluationTotal += Values.pawnValue;
evaluationTotal += whiteToBlackTable(Values.pawnPositionTable)[row][col];
} else if (chessBoard[row][col] == 'N') {
evaluationTotal += Values.knightValue;
evaluationTotal += whiteToBlackTable(Values.knightPositionTable)[row][col];
} else if (chessBoard[row][col] == 'B') {
evaluationTotal += Values.bishopValue;
evaluationTotal += whiteToBlackTable(Values.bishopPositionTable)[row][col];
} else if (chessBoard[row][col] == 'R') {
evaluationTotal += Values.rookValue;
evaluationTotal += whiteToBlackTable(Values.rookPositionTable)[row][col];
} else if (chessBoard[row][col] == 'Q') {
evaluationTotal += Values.queenValue;
evaluationTotal += whiteToBlackTable(Values.queenPositionTable)[row][col];
} else if (chessBoard[row][col] == 'K') {
evaluationTotal += Values.kingValue;
if (isEndGame(chessBoard)) {
evaluationTotal += whiteToBlackTable(Values.kingPositionTableEndGame)[row][col];
} else {
evaluationTotal += whiteToBlackTable(Values.kingPositionTableMidGame)[row][col];
}
}
}
}
return evaluationTotal;
}
public static void main(String[] args) {
char[][] chessBoard =
{{'R', 'N', 'B', 'K', 'Q', 'B', 'N', 'R'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'-', '-', '-', '-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-', '-', '-', '-'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'}};
int whiteEvaluation = whiteEvaluation(chessBoard);
int blackEvaluation = blackEvaluation(chessBoard);
if (isEndGame(chessBoard)) {
System.out.println("We have an end game");
} else {
System.out.println("We do not have an end game");
}
if (whiteEvaluation == blackEvaluation) {
System.out.println("White and Black have the same evaluation");
} else if (whiteEvaluation > blackEvaluation) {
System.out.println("White is better");
} else {
System.out.println("Black is better");
}
System.out.println("White evaluation: " + whiteEvaluation);
System.out.println("Black evaluation: " + blackEvaluation);
}
}