-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessUtility.java
170 lines (143 loc) · 4.54 KB
/
ChessUtility.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.util.*;
public class ChessUtility {
public static int eval1(char[][] state, int player)
{
int myPiecesNum = findPieces(player,state).size();
int enemyPiecesNum = findPieces(player,state).size();
int diff = enemyPiecesNum - myPiecesNum;
return diff;
}
public static int eval2(char[][] state, int player)
{
double diffCheckFactor = 2.0;
double diffCountFactor = 1.0;
Set<Point> myInCheck = findPiecesInCheck(player, state);
List<Point> myPieces = findPieces(player, state);
int myInCheckNum = myInCheck.size();
int myNumPieces = myPieces.size(); // Lower = better
int enemyPlayer = (player == 1) ? 2 : 1;
Set<Point> enemyInCheck = findPiecesInCheck(enemyPlayer, state);
List<Point> enemyPieces = findPieces(enemyPlayer, state);
int enemyInCheckNum = enemyInCheck.size();
int enemyNumPieces = enemyPieces.size(); // More = better
int diffCount = enemyNumPieces - myNumPieces; // higher = better
int diffCheck = myInCheckNum - enemyInCheckNum; // higher = better
return (int)(diffCount*diffCountFactor) + (int)(diffCheck*diffCheckFactor);
}
public static int eval3(char[][] state, int player)
{
double diffCheckFactor = 2.0;
double diffCountFactor = 1.0;
Set<Point> myInCheck = findPiecesInCheck(player, state);
List<Point> myPieces = findPieces(player, state);
int myInCheckNum = myInCheck.size();
int myNumPieces = myPieces.size(); // Lower = better
int enemyPlayer = (player == 1) ? 2 : 1;
Set<Point> enemyInCheck = findPiecesInCheck(enemyPlayer, state);
List<Point> enemyPieces = findPieces(enemyPlayer, state);
int enemyInCheckNum = enemyInCheck.size();
int enemyNumPieces = enemyPieces.size(); // More = better
int diffCount = enemyNumPieces - myNumPieces; // higher = better
int diffCheck = myInCheckNum - enemyInCheckNum; // higher = better
int score = 0;
score += (int)(diffCount*diffCountFactor);
score += (int)(diffCheck*diffCheckFactor);
// Modify score based on types of pieces on the board
int pieceTypeVal = 0;
double pieceTypeFactor = 1.0;
double goodVal = 1.0;
double badVal = 2.0;
for(Point p : myPieces)
{
char piece = state[p.x][p.y];
switch(piece)
{
case 'p': case 'P': case 'k': case 'K': case 'n': case 'N':
pieceTypeVal -= goodVal;
break;
case 'q': case 'Q': case 'r': case 'R': case 'b': case 'B':
pieceTypeVal -= badVal;
break;
}
}
for(Point p : enemyPieces)
{
char piece = state[p.x][p.y];
switch(piece)
{
case 'p': case 'P': case 'k': case 'K': case 'n': case 'N':
pieceTypeVal += goodVal;
break;
case 'q': case 'Q': case 'r': case 'R': case 'b': case 'B':
pieceTypeVal += badVal;
break;
}
}
score += (int)(pieceTypeVal*pieceTypeFactor);
return score;
}
public static HashSet<Point> findPiecesInCheck(int player, char[][] board) {
// IF YOU ARE COMBINING THIS WITH ANOTHER CLASS, REMEMBER TO REMOVE "FindMoves." BELOW
ArrayList<ArrayList<Point>> theirMoves = FindMoves.getMoves(player == 1 ? 2 : 1, board);
HashSet<Point> points = new HashSet<Point>(16);
for(ArrayList<Point> a : theirMoves) {
Point to = a.get(1);
if (board[to.x][to.y] == '.') {
return points;
}
else {
points.add(to);
}
}
return points;
}
public static HashSet<Point> findPiecesChecking(int player, char[][] board) {
// IF YOU ARE COMBINING THIS WITH ANOTHER CLASS, REMEMBER TO REMOVE "FindMoves." BELOW
ArrayList<ArrayList<Point>> theirMoves = FindMoves.getMoves(player == 1 ? 2 : 1, board);
HashSet<Point> points = new HashSet<Point>(16);
for(ArrayList<Point> a : theirMoves) {
Point from = a.get(0);
Point to = a.get(1);
if (board[to.x][to.y] == '.') {
return points;
}
else {
points.add(from);
}
}
return points;
}
public static ArrayList<Point> findPieces(int player, char[][] board) {
ArrayList<Point> toRet = new ArrayList<Point>(16);
for(int i=0;i<8;++i) {
for(int j=0;j<8;++j) {
if (board[i][j] == '.') {
continue;
}
else if (player == 1 && board[i][j] >= 'a') {
toRet.add(new Point(i,j));
}
else if (player == 2 && board[i][j] <= 'Z') {
toRet.add(new Point(i,j));
}
}
}
return toRet;
}
public static int numPieces(char[][] state, int player)
{
int count1 = 0;
int count2 = 0;
for (int i = 0; i<state.length; ++i) {
for (int j = 0; j<state[0].length; ++j) {
if (state[i][j] == '.') continue;
if (state[i][j] >= 'a' && player== 1) count1++;
if (state[i][j] <= 'Z' && player == 2) count2++;
}
}
if(player==1)
return count1;
else // player==2
return count2;
}
}