-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackEndCopy.txt
423 lines (404 loc) · 28.1 KB
/
BackEndCopy.txt
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package com.meietiim.pvlaevadepommitamine;
import java.util.Arrays;
import java.util.SplittableRandom;
import static com.meietiim.pvlaevadepommitamine.FrontEnd.*;
/**
* @author Gregor Suurvarik
*/
public class BackEnd {
// Makes code shorter because Gregor is lazy
int playerPlaceShipX = MAIN.playerPlaceShipX;
int playerPlaceShipY = MAIN.playerPlaceShipY;
int playerPlaceShipH = MAIN.playerPlaceShipH;
int playerPlaceShipW = MAIN.playerPlaceShipW;
int playerPlacedBombX = MAIN.playerPlaceBombX;
int playerPlacedBombY = MAIN.playerPlaceBombY;
// AI Data storage
private boolean AiShipsPlaced = false;
private int AiPlacedX = 0;
private int AiPlacedY = 0;
private int AiPlacedH = 0;
private int AiPlacedW = 0;
private int AiBombdX = 0;
private int AiBombdY = 0;
private boolean guessingOrientation = false;
private int orientationAttempt = 0;
// 1 up -1 down 2 right -2 left 0 not in guessing
private int[][] AiBombData = new int[10][6];
// AiBombData stores x y orientation length1 and 2, and if the case exists
private int bombCase = 0;
public static final int[] SHIPS = new int[]{5, 4, 3, 3, 2};
// Ship orientation cases
private final static int SHIP_UP = 1;
private final static int SHIP_DOWN = -1;
private final static int SHIP_RIGHT = 2;
private final static int SHIP_LEFT = -2;
//Random
private SplittableRandom random = new SplittableRandom();
public void update() {
MAIN.response = RESPONSE_EMPTY;
MAIN.error = ERROR_NONE;
switch (MAIN.action) {
case ACTION_PLACE_SHIP: // Player placed a ship
// Store placed ship in playerShip
// Check if the ship is placed within the bounds
boolean correctPlacement = false;
if (playerPlaceShipX >= 0 && playerPlaceShipY >= 0 &&
playerPlaceShipX + playerPlaceShipW - 1 < 10 &&
playerPlaceShipY + playerPlaceShipH - 1 < 10) {
correctPlacement = true;
}
// Ship is placed correctly, add the changes to board's state
if (correctPlacement) {
// Add the new ship to playerShipData
MAIN.playerShipData[MAIN.placingShipID] = new int[]{
playerPlaceShipX, playerPlaceShipY,
playerPlaceShipW, playerPlaceShipH,
0, 0};
MAIN.placingShipID++;
// Add each piece onto playerShips[][]
for (int x = playerPlaceShipX; x < playerPlaceShipX + playerPlaceShipW - 1; x++) {
for (int y = playerPlaceShipY; y < playerPlaceShipY + playerPlaceShipH - 1; y++) {
MAIN.playerShips[x][y] = true;
}
}
} else {
// Ship is placed incorrectly
MAIN.error = ERROR_INCORRECT_PLACEMENT;
}
break;
case ACTION_PLACE_BOMB: // Player placed a bomb
MAIN.playerBombs[playerPlacedBombX][playerPlacedBombY] = true; // Marks bob on the field
if (MAIN.computerShips[playerPlacedBombX][playerPlacedBombY]) { // If there is a ship
MAIN.response = RESPONSE_HIT; // it will response hit
// And will add a bomb to ship data
MAIN.computerShipData[getShipID(playerPlacedBombX, playerPlacedBombY, MAIN.computerShipData)][4]++;
// it will check if ship has sunken
if (hasShipSunk(MAIN.computerBombs, MAIN.playerShipData[getShipID(playerPlacedBombX, playerPlacedBombY, MAIN.playerShipData)])) {
// Marks ship dead
MAIN.computerShipData[getShipID(playerPlacedBombX, playerPlacedBombY, MAIN.playerShipData)][5] = 1;
// Responds ship dead
MAIN.response = RESPONSE_DEAD;
}
}
break;
case ACTION_PLACE_COMPUTER: // Computer's turn
AiTurn:
if (AiShipsPlaced) { // If true then ships are placed otherwise place ships
int t = 0; // Variable for while loop
int attempts = 0; // To avoid endless loops
while (t != 1) { // to be "stuck" here until it has made the move
//################################################# Advanced AI ####################################################
if (AiBombData[bombCase][5] == 1) { // If that case exists
int x = AiBombData[bombCase][0]; // Read X coordinate
int y = AiBombData[bombCase][1]; // Read Y coordinate
if (guessingOrientation = true) { // If it does not know the orientation
if (MAIN.computerBombs[x][y + 1]) {
if (MAIN.playerShips[x][y + 1]) {
AiBombData[bombCase][2] = SHIP_UP; // Tell the AI the orientation of the ship
AiBombData[bombCase][3]++;
guessingOrientation = false; // We know orientation so we don't have to guess any more
} else if (MAIN.computerBombs[x][y - 1]) {
if (MAIN.playerShips[x][y - 1]) {
AiBombData[bombCase][2] = SHIP_UP; // We put the ship upwards because its relevant later
AiBombData[bombCase][4]++;
guessingOrientation = false; // We know orientation so we don't have to guess any more
}
}
} else {
if (MAIN.computerBombs[x][y + 1] = false) { // when it hasn't asked that point now it will
MAIN.computerBombs[x][y + 1] = true; // Makes a move
break AiTurn; // Ends AIs turn
} else {
if (MAIN.computerBombs[x][y - 1] = false) { // When it hasn't asked that point it will now
MAIN.computerBombs[x][y - 1] = true; // makes a move
break AiTurn; // Ends AIs turn
} else {
AiBombData[bombCase][2] = SHIP_RIGHT; // When the orientation is not vertical must be horizontal
guessingOrientation = false;
}
}
}
}
//################### End of guessing start of bombing
else {
switch (AiBombData[bombCase][2]) { // Switches to suitable case
case SHIP_LEFT:// If ship goes left
if (MAIN.computerBombs[x - AiBombData[bombCase][4]][y]) { // Have we bombed that place
if (MAIN.playerShips[x - AiBombData[bombCase][4]][y]) { // If we had a ship
AiBombData[bombCase][4]++; // extends the ship length to AI
// Didn't Respond that because it must have been responded before
} else {
AiBombData[bombCase][2] = AiBombData[bombCase][2] * -1;
}
} else {
MAIN.computerBombs[x + AiBombData[bombCase][4]][y] = true; // Bombs next spot
if (MAIN.playerShips[x + AiBombData[bombCase][4]][y]) {
AiBombData[bombCase][4]++; // Extends ship length to AI
MAIN.response = RESPONSE_HIT; // Responds Hit
// finds the ship and increases the bomb count
MAIN.playerShipData[getShipID(x - AiBombData[bombCase][4], y,
MAIN.playerShipData)][4]++;
// Checks if ship has sunken
if (hasShipSunk(MAIN.computerBombs, MAIN.playerShipData[getShipID(
x - AiBombData[bombCase][4], y, MAIN.playerShipData)])) {
// Marks ship dead
MAIN.playerShipData[getShipID(x - AiBombData[bombCase][4], y,
MAIN.playerShipData)][5] = 1;
//Responds "Ship is down on the ocean floor"
MAIN.response = RESPONSE_DEAD;
// Following 4 lines of code mark the area around the ship as bombed,
// it comes from the rules that ships cant be side by side and have a common corner
for (int i = AiBombData[bombCase][0] - 1; i < AiBombData[bombCase][0] +
AiBombData[bombCase][3] + AiBombData[bombCase][4] - 2 + 1; i++) {
for (int j = AiBombData[bombCase][1] - 1; j < AiBombData[bombCase][1] + 1; j++) {
if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
MAIN.computerBombs[i][j] = true;
}
}
}
bombCase++; // Ends cycle
break AiTurn; // Ends AIs turn
}
} else { // If there wasn't a ship it should continue to next direction
AiBombData[bombCase][2] = AiBombData[bombCase][2] * -1;
break AiTurn; // Ends AIs turn
}
}
case SHIP_RIGHT: // If ship goes right
if (MAIN.computerBombs[x + AiBombData[bombCase][3]][y]) { // Have we bombed that place
if (MAIN.playerShips[x + AiBombData[bombCase][3]][y]) { // If we had a ship
AiBombData[bombCase][3]++; // extends the ship length to AI
// Didn't Respond that because it must have been responded before
} else {
AiBombData[bombCase][2] = AiBombData[bombCase][2] * -1;
}
} else {
MAIN.computerBombs[x + AiBombData[bombCase][3]][y] = true; // Bombs next spot
if (MAIN.playerShips[x + AiBombData[bombCase][3]][y]) {
AiBombData[bombCase][3]++; // Extends ship length to AI
MAIN.response = RESPONSE_HIT; // Responds Hit
// finds the ship and increases the bomb count
MAIN.playerShipData[getShipID(x + AiBombData[bombCase][3], y,
MAIN.playerShipData)][4]++;
// Checks if ship has sunken
if (hasShipSunk(MAIN.computerBombs, MAIN.playerShipData[getShipID(
x + AiBombData[bombCase][4], y, MAIN.playerShipData)])) {
// Marks ship dead
MAIN.playerShipData[getShipID(x + AiBombData[bombCase][3], y,
MAIN.playerShipData)][5] = 1;
//Responds "Ship is down on the ocean floor"
MAIN.response = RESPONSE_DEAD;
// Following 4 lines of code mark the area around the ship as bombed,
// it comes from the rules that ships cant be side by side and have a common corner
for (int i = AiBombData[bombCase][0] - 1; i < AiBombData[bombCase][0] +
AiBombData[bombCase][3] + AiBombData[bombCase][4] - 2 + 1; i++) {
for (int j = AiBombData[bombCase][1] - 1; j < AiBombData[bombCase][1] + 1; j++) {
if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
MAIN.computerBombs[i][j] = true;
}
}
}
bombCase++; // Ends cycle
break AiTurn; // Ends AIs turn
}
} else { // If there wasn't a ship it should continue to next direction
AiBombData[bombCase][2] = AiBombData[bombCase][2] * -1;
break AiTurn; // Ends AIs turn
}
}
case SHIP_DOWN: // If ship goes Down
if (MAIN.computerBombs[x][y - AiBombData[bombCase][4]]) { // Have we bombed that place
if (MAIN.playerShips[x][y - AiBombData[bombCase][4]]) { // If we had a ship
AiBombData[bombCase][4]++; // extends the ship length to AI
// Didn't Respond that because it must have been responded before
} else {
AiBombData[bombCase][2] = AiBombData[bombCase][2] * -1;
}
} else {
MAIN.computerBombs[x][y - AiBombData[bombCase][4]] = true; // Bombs next spot
if (MAIN.playerShips[x][y - AiBombData[bombCase][4]]) {
AiBombData[bombCase][4]++; // Extends ship length to AI
MAIN.response = RESPONSE_HIT; // Responds Hit
// finds the ship and increases the bomb count
MAIN.playerShipData[getShipID(x, y - AiBombData[bombCase][4],
MAIN.playerShipData)][4]++;
// Checks if ship has sunken
if (hasShipSunk(MAIN.computerBombs, MAIN.playerShipData[getShipID(x,
y - AiBombData[bombCase][3], MAIN.playerShipData)])) {
// Marks ship dead
MAIN.playerShipData[getShipID(x, y - AiBombData[bombCase][4],
MAIN.playerShipData)][5] = 1;
//Responds "Ship is down on the ocean floor"
MAIN.response = RESPONSE_DEAD;
// Following 4 lines of code mark the area around the ship as bombed,
// it comes from the rules that ships cant be side by side and have a common corner
for (int i = AiBombData[bombCase][0] - 1; i < AiBombData[bombCase][0] + 1; i++) {
for (int j = AiBombData[bombCase][1] - 1; j < AiBombData[bombCase][1] +
AiBombData[bombCase][3] + AiBombData[bombCase][4] - 2 + 1; j++) {
if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
MAIN.computerBombs[i][j] = true;
}
}
}
bombCase++; // Ends cycle
break AiTurn; // Ends AIs turn
}
} else { // If there wasn't a ship it should continue to next direction
MAIN.error = ERROR_UNKNOWN;
}
}
case SHIP_UP: // If ship goes right
if (MAIN.computerBombs[x][y + AiBombData[bombCase][3]]) { // Have we bombed that place
if (MAIN.playerShips[x][y + AiBombData[bombCase][3]]) { // If we had a ship
AiBombData[bombCase][3]++; // extends the ship length to AI
// Didn't Respond that because it must have been responded before
} else {
AiBombData[bombCase][2] = AiBombData[bombCase][2] * -1;
}
} else {
MAIN.computerBombs[x][y + AiBombData[bombCase][3]] = true; // Bombs next spot
if (MAIN.playerShips[x][y + AiBombData[bombCase][3]]) {
AiBombData[bombCase][3]++; // Extends ship length to AI
MAIN.response = RESPONSE_HIT; // Responds Hit
// finds the ship and increases the bomb count
MAIN.playerShipData[getShipID(x, y + AiBombData[bombCase][3],
MAIN.playerShipData)][4]++;
// Checks if ship has sunken
if (hasShipSunk(MAIN.computerBombs, MAIN.playerShipData[getShipID(x,
y + AiBombData[bombCase][3], MAIN.playerShipData)])) {
// Marks ship dead
MAIN.playerShipData[getShipID(x, y + AiBombData[bombCase][3],
MAIN.playerShipData)][5] = 1;
//Responds "Ship is down on the ocean floor"
MAIN.response = RESPONSE_DEAD;
for (int i = AiBombData[bombCase][0] - 1; i < AiBombData[bombCase][0] + 1; i++) {
for (int j = AiBombData[bombCase][1] - 1; j < AiBombData[bombCase][1] +
AiBombData[bombCase][3] + AiBombData[bombCase][4] - 2 + 1; j++) {
if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
MAIN.computerBombs[i][j] = true;
}
}
}
bombCase++; // Ends cycle
break AiTurn; // Ends AIs turn
}
} else { // If there wasn't a ship it should continue to next direction
AiBombData[bombCase][2] = AiBombData[bombCase][2] * -1;
break AiTurn; // Ends AIs turn
}
}
}
}
}
//########################################### End of Advanced AI ##################################################
else {
AiBombdX = random.nextInt(10);
AiBombdY = random.nextInt(10);
attempts++;
if (attempts >= 101) {
break AiTurn; // Ends AIs turn
}// If something goes wrong it will stop the endless loop
if (MAIN.computerBombs[AiBombdX][AiBombdY] != true) { // looks if it has bombed that place
MAIN.computerBombs[AiBombdX][AiBombdY] = true; // Marks place to have been bombed
if (MAIN.playerShips[AiBombdX][AiBombdY]) { // If there is a player ship
AiBombData[bombCase][0] = AiBombdX; // stores x and y from the last hit for next move
AiBombData[bombCase][1] = AiBombdY;
guessingOrientation = true;
MAIN.response = RESPONSE_HIT; // Responds Hit
// finds the ship and increases the bomb count
MAIN.playerShipData[getShipID(AiBombdX, AiBombdY, MAIN.playerShipData)][4]++;
// Checks if ship has sunken
if (hasShipSunk(MAIN.computerBombs, MAIN.playerShipData[getShipID(AiBombdX, AiBombdY,
MAIN.playerShipData)])) {
// Marks ship dead
MAIN.playerShipData[getShipID(AiBombdX, AiBombdY, MAIN.playerShipData)][5] = 1;
//Responds "Ship is down on the ocean floor"
MAIN.response = RESPONSE_DEAD;
}
}
t++; // One of the conditions for this loop, so that AI has to make a move and retry if it failed
}
}
}
} else {
int i = 0;
while (i < MAIN.computerShipData.length) {
if (MAIN.computerShipData[i][3] == 0) {
// AI places ships
// Gets X and Y for that ship to fit inn the area
if (random.nextBoolean()) { // If true ship will be placed horizontally
// Otherwise ship will be placed vertically
AiPlacedX = random.nextInt(10 - SHIPS[i]);
AiPlacedY = random.nextInt(10);
AiPlacedH = 1;
AiPlacedW = SHIPS[i];
} else {
AiPlacedX = random.nextInt(10);
AiPlacedY = random.nextInt(10 - SHIPS[i]);
AiPlacedH = SHIPS[i];
AiPlacedW = 1;
}
// Check if ship fits
if (MAIN.isSpaceAroundFree(AiPlacedX, AiPlacedY, AiPlacedW, AiPlacedH, MAIN.computerShips)) {
for (int x = AiPlacedX; x <= AiPlacedX + AiPlacedW - 1; x++) {
for (int y = AiPlacedY; y <= AiPlacedY + AiPlacedH - 1; y++) {
MAIN.computerShips[x][y] = true;
}
} // Adds ships to computerShipData
MAIN.computerShipData[i] = new int[]{AiPlacedX, AiPlacedY,
AiPlacedW, AiPlacedH, 0, 0};
i++; // tells to to next ship
}
//If all ships are placed it marks placing ships done
if (i == 5) {
AiShipsPlaced = true;
}
} else { // If ship exists then moves to another ship
i++;
}
}
}
break; // Ends AIs turn;
}
}
/**
* @return Returns a ship that contains given point. If
* not found, -1 will be returned.
*/
public int getShipID(int checkX, int checkY, int[][] shipData) {
// Iterates thru all the ships
for (int i = 0; i < shipData.length; i++) {
// If the given point lies in the boundaries of that ship
// return the ship's ID.
if (checkX >= shipData[i][0] &&
checkY >= shipData[i][1] &&
checkX < shipData[i][0] + shipData[i][2] &&
checkY < shipData[i][1] + shipData[i][3]) {
return i;
}
}
// If none of the ships contained the point, return -1
return -1;
}
/**
* @return Returns a ship that contains given point. If
* not found, -1 will be returned.
*/
public boolean hasShipSunk(boolean[][] bombs, int[] targetShipData) {
// Bomb counter
int foundBombs = 0;
// Iterate thru each point in the ship and count all the bombs in it
for (int i = targetShipData[0]; i < targetShipData[0] + targetShipData[2]; i++) {
for (int j = targetShipData[1]; j < targetShipData[1] + targetShipData[3]; j++) {
if (bombs[i][j]) {
foundBombs++;
}
}
}
// If the number of found bombs on the ship is equal with the
// length of the ship, the ship has sunk
return foundBombs == Math.max(targetShipData[2], targetShipData[3]);
}
}
// hetkel viimane versioon