-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bot.java
297 lines (216 loc) · 6.77 KB
/
Bot.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import java.util.HashMap;
/**
* This class is a simple bot that play insted player 2
*
*
* @author Mohammad Mahdi Malmasi
* @version 0.1.0
*/
public class Bot extends Player
{
/* Feilds */
// the blocks that bot can choose
// rank is a float that make difrence between selectable blocks
// the bot will chose the higher rank
private HashMap<String, Float> emptyBlocksRank;
// rank is an int that make difrence squers rotate
// the bot will select the higher rank
private HashMap<String, Integer> squeresRotateRank;
/* Constructor */
/**
* Creat a new bot with given ID and character
*
* @param botID : the ID of the bot
* @param botCharacter : the charater of the bot
*/
public Bot(int botID, char botCharacter)
{
super("BOT", "", 0, botID, botCharacter, "");
emptyBlocksRank = new HashMap<>();
squeresRotateRank = new HashMap<>();
}
/* Methods */
/**
* This method is a kind of main method for bot
* It makes bot decision
*
* @param gameBoard : the board of the game
*/
public void playTurn(Board gameBoard)
{
resetRanks();
// check that the board has empty blocks or not
if (gameBoard.getNumberOfEmptyBlocks() == 0)
return;
// rank the empty blocks
rankTheEmptyBlocks(gameBoard);
String botChoose = null;
for (String block : emptyBlocksRank.keySet())
{
if (botChoose == null)
{
botChoose = block;
continue;
}
if (emptyBlocksRank.get(block) > emptyBlocksRank.get(botChoose))
botChoose = block;
}
int y = deCode(botChoose, 'Y');
int x = deCode(botChoose, 'X');
Rules.applyPlayerSelect(gameBoard, y, x, this);
// check that the game is ended or not
if (Rules.isWinner(gameBoard, this))
return;
// rank the squares rotate
rankSqueresRotate(gameBoard);
String botRotateChoose = null;
for (String rotate: squeresRotateRank.keySet())
{
if (botRotateChoose == null)
{
botRotateChoose = rotate;
continue;
}
if (squeresRotateRank.get(rotate) >= squeresRotateRank.get(botRotateChoose))
botRotateChoose = rotate;
}
int squareNumber = (int)botRotateChoose.charAt(0) - 48;
boolean rotateWise = botRotateChoose.charAt(1) == 't' ? true: false;
gameBoard.rotate(squareNumber, rotateWise);
}
// this method rank the empty blocks
private void rankTheEmptyBlocks(Board gameBoard)
{
float rank = 0;
for (int j = 0; j < gameBoard.getMainBoardY(); j++)
for (int i = 0; i < gameBoard.getMainBoardX(); i++)
if (gameBoard.getMainBoardBlockKind(j, i) == 0)
{
rank = (-1.1f) * (Math.abs(j - 2.5f) + Math.abs(i - 2.5f));
emptyBlocksRank.put(makeCode(j, i), rank);
}
}
// this method rank the squares rotate
private void rankSqueresRotate(Board gameBoard)
{
Player opponent = new Player("", "", 0, (-1)*this.getPlayerID(), '\0', "");
for (int n = 1; n < 5; n++)
{
gameBoard.rotate(n, true);
if (Rules.isWinner(gameBoard, opponent))
squeresRotateRank.put("" + n + 't', -10);
else
squeresRotateRank.put("" + n + 't', opponentBlocksCount(gameBoard, (-1)*this.getPlayerID(), n));
gameBoard.rotate(n, false);
gameBoard.rotate(n, false);
if (Rules.isWinner(gameBoard, opponent))
squeresRotateRank.put("" + n + 'f', -10);
else
squeresRotateRank.put("" + n + 'f', opponentBlocksCount(gameBoard, (-1)*this.getPlayerID(), n));
gameBoard.rotate(n, true);
}
Rules.setWinner(null);
}
// this method reset the ranks of the blocks and squares
private void resetRanks()
{
// reset the empty blocks rank
for (String block: emptyBlocksRank.keySet())
{
emptyBlocksRank.replace(block, -3000.0f);
}
// reset the squers rank
for (String squarRotate: squeresRotateRank.keySet())
{
squeresRotateRank.replace(squarRotate, -3000);
}
}
// this method count the number of the opponent blocks in square
private int opponentBlocksCount(Board gameBoard, int opponentID, int squareNumber)
{
int y = 0, x = 0;
switch (squareNumber)
{
case 2:
x = 3;
break;
case 3:
y = 3;
break;
case 4:
x = y = 3;
break;
}
int cntr = 0;
for (int j = 0; j < 3; j++)
for (int i = 0; i < 3; i++)
if (gameBoard.getMainBoardBlockKind(y+j, x+i) == opponentID)
cntr++;
return cntr;
}
// thie method converts the i,j to string
// example: (3, 4) >>> "5D"
private String makeCode(int y, int x)
{
char X = '\0';
switch (x)
{
case 0:
X = 'A';
break;
case 1:
X = 'B';
break;
case 2:
X = 'C';
break;
case 3:
X = 'D';
break;
case 4:
X = 'E';
break;
case 5:
X = 'F';
break;
case 6:
X = 'G';
break;
case 7:
X = 'H';
break;
}
return "" + (y+1) + X;
}
// this method converts string to i,j
// example: "5D" >>> (3, 4)
private int deCode(String choosenBlock, char which)
{
switch (which)
{
case 'X':
return (int)choosenBlock.charAt(1) - 65;
case 'Y':
switch (choosenBlock.charAt(0))
{
case '1':
return 0;
case '2':
return 1;
case '3':
return 2;
case '4':
return 3;
case '5':
return 4;
case '6':
return 5;
case '7':
return 6;
case '8':
return 7;
}
}
return -1;
}
}