-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
716 lines (676 loc) · 28.9 KB
/
main.py
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import pickle
import copy
import json
import random
import re
import sys
from enum import Enum
class Orientation(Enum):
horizontal = 0
vertical = 1
class GameData():
def __init__(self, **entries):
self.last_coordinates = ""
self.player1_turn = True
self.ships_player1 = {"aircraftcarrier":{"start":"", "end":"", "orientation":Orientation.horizontal, "hits":[]},\
"battleship": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"cruiser": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"destroyer1": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"destroyer2": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"submarine1": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"submarine2": {"coords": [], "orientation": Orientation.horizontal, "hits":[]}\
}
self.ships_player2 = {"aircraftcarrier":{"coords": [], "orientation":Orientation.horizontal, "hits":[]},\
"battleship": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"cruiser": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"destroyer1": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"destroyer2": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"submarine1": {"coords": [], "orientation": Orientation.horizontal, "hits":[]},\
"submarine2": {"coords": [], "orientation": Orientation.horizontal, "hits":[]}\
}
self.player1_coordinates = []
self.player2_coordinates = []
self.player1_board = []
self.player2_board = []
self.availableCoordsP1 = []
self.availableCoordsP2 = []
self.botMode = False
self.tasksMode = False
self.octopusCount = 10
self.numOfSeaMines = 5
self.seaMineCoordinates = []
self.ready = False
self.__dict__.update(entries)
"""
A Custom JSONEncoder used to encode the GameData class
"""
class GameDataJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, GameData):
return obj.__dict__
if isinstance(obj, Enum):
return obj.value
else:
return json.JSONEncoder.default(self, obj)
"""Python dictionaries are unsorted by default, if you have to sort the Keys see def board(coordinates)
"""
coordinates = {'A1':' ', 'A2':' ', 'A3':' ', 'A4':' ', 'A5':' ',\
'A6':' ', 'A7':' ', 'A8':' ', 'A9':' ', 'A0': ' ',\
'B1':' ', 'B2':' ', 'B3':' ', 'B4':' ', 'B5':' ',\
'B6':' ', 'B7':' ', 'B8':' ', 'B9':' ', 'B0': ' ',\
'C1':' ', 'C2':' ', 'C3':' ', 'C4':' ', 'C5':' ',\
'C6':' ', 'C7':' ', 'C8':' ', 'C9':' ', 'C0': ' ',\
'D1':' ', 'D2':' ', 'D3':' ', 'D4':' ', 'D5':' ',\
'D6':' ', 'D7':' ', 'D8':' ', 'D9':' ', 'D0': ' ',\
'E1':' ', 'E2':' ', 'E3':' ', 'E4':' ', 'E5':' ',\
'E6':' ', 'E7':' ', 'E8':' ', 'E9':' ', 'E0': ' ',\
'F1':' ', 'F2':' ', 'F3':' ', 'F4':' ', 'F5':' ',\
'F6':' ', 'F7':' ', 'F8':' ', 'F9':' ', 'F0': ' ',\
'G1':' ', 'G2':' ', 'G3':' ', 'G4':' ', 'G5':' ',\
'G6':' ', 'G7':' ', 'G8':' ', 'G9':' ', 'G0': ' ',\
'H1':' ', 'H2':' ', 'H3':' ', 'H4':' ', 'H5':' ',\
'H6':' ', 'H7':' ', 'H8':' ', 'H9':' ', 'H0': ' ',\
'I1':' ', 'I2':' ', 'I3':' ', 'I4':' ', 'I5':' ',\
'I6':' ', 'I7':' ', 'I8':' ', 'I9':' ', 'I0': ' ',\
'J1':' ', 'J2':' ', 'J3':' ', 'J4':' ', 'J5':' ',\
'J6':' ', 'J7':' ', 'J8':' ', 'J9':' ', 'J0': ' '\
}
"""Do NOT change any lines of this function!
Use following symbols:
ship: ◯ (U+25EF)
hit: ╳ (U+2573)
miss: ≈ (U+2248)
"""
def board(coordinates):
printboard = (" ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐\n")
last_key = "A"
line = " "
line = line + last_key
line = line + " │"
keylist = list(coordinates.keys())
for key in sorted(keylist):
if key[0] == last_key:
line = line + " " + coordinates[key] + " " + "│"
else:
printboard += line
printboard += "\n ├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤\n"
line = " "
last_key = key[0]
line = line + last_key
line = line + " │"
line = line + " " + coordinates[key] + " " + "│"
printboard += line
printboard += "\n └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘"
printboard += "\n 0 1 2 3 4 5 6 7 8 9 "
return printboard
"""
Prints all error messages of player
"""
def printErrorMessage(message):
if gameData.player1_turn == True:
print(message)
"""
Saves hole gameData object as a binary file
"""
def saveGame():
if gameData.ready:
pickle.dump(gameData, open("gameData.txt", "wb"))
print("Game saved.")
else:
print("Game cannot be saved before all ships are placed.")
"""
Loads hole gameData object from a binary file
"""
def loadGame():
global gameData
try:
gameData = pickle.load(open("gameData.txt", "rb"))
startGame()
print("Game loaded.")
except FileNotFoundError:
print("No saved game could be found.")
"""
Check if entered coordinate(s) is/are valid
"""
def isCoordInputValid(str, numOfFields):
if gameData.tasksMode:
if str == "save":
saveGame()
return False
if str == "load":
loadGame()
return False
if numOfFields > 1:
if str.count(' ') != 1:
printErrorMessage("Please specify 2 coordinates.")
return False
else:
result = re.match(r'([A-J][0-9][ ][A-J][0-9])', str)
else:
if str.count(' ') != 0:
printErrorMessage("Please specify 1 coordinate.")
return False
else:
result = re.match(r'([A-J][0-9])', str)
if result is None:
printErrorMessage("You entered an invalid coordinate.");
return False
else:
if " ".join(result.groups()) != str:
printErrorMessage("You entered an invalid coordinate.");
return False
else:
return True
"""
Calculates the distance in fields between the entered coordinatess
"""
def distanceBetweenCoords(x, y):
if x[0] != y[0]:
if ord(x[0]) < ord(y[0]):
result = ord(y[0]) - ord(x[0])
elif ord(x[0]) > ord(y[0]):
result = ord(x[0]) - ord(y[0])
else:
if ord(x[1]) < ord(y[1]):
result = ord(y[1]) - ord(x[1])
elif ord(x[1]) > ord(y[1]):
result = ord(x[1]) - ord(y[1])
return result + 1
"""
Checks if the second entered coordinate is valid
"""
def isEndCoordValid(coords, numOfFields):
if numOfFields > 1:
coordX = list(coords[0])
coordY = list(coords[1])
if (coordX[0] != coordY[0] and coordX[1] != coordY[1]) or distanceBetweenCoords(coordX, coordY) != numOfFields:
printErrorMessage("Your end coordinate is invalid.")
return False
else:
return True
"""
Checks if the coordinate is horizontal or vertical
"""
def getDirection(coords):
coordX = list(coords[0])
coordY = list(coords[1])
if coordX[0] != coordY[0]:
return Orientation.vertical
else:
return Orientation.horizontal
"""
Input valid coordinates
"""
def inputCoordinates(inputText, numOfFields):
userInput = input(inputText)
if isCoordInputValid(userInput, numOfFields) == True:
if numOfFields > 1:
coords = userInput.split(" ")
if isEndCoordValid(coords, numOfFields):
return coords;
else:
return userInput
"""
Sets ship on specific field
"""
def setShip(coords, numOfFields, playerCoordinates):
if coords is not None:
if numOfFields > 1:
for coord in coords:
playerCoordinates[coord] = '◯'
else:
playerCoordinates[coords] = '◯'
"""
Get all coordinates as a list which are in the given range
"""
def getCoordsRange(coords, numOfFields):
coordsRange = []
if coords is not None:
coord1 = list(coords[0])
coord2 = list(coords[1])
orientation = getDirection(coords)
if orientation == Orientation.horizontal:
number = int(coord1[1])
for x in range(numOfFields):
coordsRange.append(coord1[0] + str(number))
number = number + 1
else:
character = coord1[0]
for x in range(numOfFields):
coordsRange.append(character + coord1[1])
if coord1[0] < coord2[0]:
character = chr(ord(character) + 1)
else:
character = chr(ord(character) - 1)
return coordsRange
"""
Get all surrounded coordinates of a ship
"""
def getSurroundingCoords(coords, numOfFields):
surroundingCoords = []
if numOfFields > 1:
surroundingCoords.append(coords[0])
surroundingCoords.append(coords[1])
orientation = getDirection(coords)
iterations = 1
for coord in coords:
y = coord[0]
x = coord[1]
if orientation == Orientation.vertical:
if int(x) > 0:
surroundingCoords.append(y + str((int(x) - 1)))
if int(x) < 9:
surroundingCoords.append(y + str((int(x) + 1)))
if iterations == 1:
if y > 'A':
surroundingCoords.append(chr(ord(y) - 1) + x)
if int(x) > 0:
surroundingCoords.append(chr(ord(y) - 1) + str((int(x) - 1)))
if int(x) < 9:
surroundingCoords.append(chr(ord(y) - 1) + str((int(x) + 1)))
if iterations == len(coords):
if y < 'J':
surroundingCoords.append(chr(ord(y) + 1) + x)
if int(x) > 0:
surroundingCoords.append(chr(ord(y) + 1) + str((int(x) - 1)))
if int(x) < 9:
surroundingCoords.append(chr(ord(y) + 1) + str((int(x) + 1)))
else:
if y > 'A':
surroundingCoords.append(chr(ord(y) - 1) + x)
if y < 'J':
surroundingCoords.append(chr(ord(y) + 1) + x)
if iterations == 1:
if int(x) > 0:
surroundingCoords.append(y + str((int(x) - 1)))
if y > 'A':
surroundingCoords.append(chr(ord(y) - 1) + str((int(x) - 1)))
if y < 'J':
surroundingCoords.append(chr(ord(y) + 1) + str((int(x) - 1)))
if iterations == len(coords):
if int(x) < 9:
surroundingCoords.append(y + str((int(x) + 1)))
if y > 'A':
surroundingCoords.append(chr(ord(y) - 1) + str((int(x) + 1)))
if y < 'J':
surroundingCoords.append(chr(ord(y) + 1) + str((int(x) + 1)))
iterations = iterations + 1
else:
surroundingCoords.append(coords)
y = coords[0]
x = coords[1]
if y > 'A':
surroundingCoords.append(chr(ord(y) - 1) + x)
if int(x) > 0:
surroundingCoords.append(chr(ord(y) - 1) + str((int(x) - 1)))
if int(x) < 9:
surroundingCoords.append(chr(ord(y) - 1) + str((int(x) + 1)))
if y < 'J':
surroundingCoords.append(chr(ord(y) + 1) + x)
if int(x) > 0:
surroundingCoords.append(chr(ord(y) + 1) + str((int(x) - 1)))
if int(x) < 9:
surroundingCoords.append(chr(ord(y) + 1) + str((int(x) + 1)))
if int(x) > 0:
surroundingCoords.append(y + str((int(x) - 1)))
if int(x) < 9:
surroundingCoords.append(y + str((int(x) + 1)))
return surroundingCoords
"""
Checks if the placement of the ship is valid
"""
def isPlacementValid(coords, playerCoordinates):
for coord in coords:
if playerCoordinates[coord] != ' ':
return False
return True
"""
Get random coordinate(s) where characters are A-J and numbers are 0-9
"""
def getRandomCoords(numOfFields):
coords = []
orientation = random.randint(0,1)
coordY = random.choice("ABCDEFGHIJ")
coordX = random.randint(0, 9)
if orientation == Orientation.horizontal.value:
coords.append(coordY + str(coordX))
if numOfFields > 1:
coords.append(coordY + str(coordX + (numOfFields - 1)))
else:
coords.append(coordY + str(coordX))
if numOfFields > 1:
coords.append(chr(ord(coordY) + (numOfFields - 1)) + str(coordX))
return coords
"""
Get valid coordinates from user/bot
"""
def getCoordsFromPlayer(text, numOfFields, shipType, prevShipType, playerCoordinates):
success = False
while success == False:
if gameData.player1_turn and gameData.botMode == False:
coords = inputCoordinates(text, numOfFields)
else:
coords = getRandomCoords(numOfFields)
if numOfFields > 1:
coordsAsString = ' '.join(coords)
else:
coordsAsString = coords[0]
coords = coordsAsString
if isCoordInputValid(coordsAsString, numOfFields) == False:
coords = None
if coords is not None:
if numOfFields > 1:
coordsRange = getCoordsRange(coords, numOfFields)
if isPlacementValid(getSurroundingCoords(coordsRange, numOfFields), playerCoordinates):
setShip(coordsRange, numOfFields, playerCoordinates)
success = True
else:
printErrorMessage("Ship overlaps or adjoins " + prevShipType + ". Reposition " + shipType + ".")
else:
if isPlacementValid(getSurroundingCoords(coords, numOfFields), playerCoordinates):
setShip(coords, numOfFields, playerCoordinates)
success = True
else:
printErrorMessage("Ship overlaps or adjoins " + prevShipType + ". Reposition " + shipType + ".")
if gameData.player1_turn:
if numOfFields > 1:
gameData.ships_player1[shipType]["coords"] = coordsRange
gameData.ships_player1[shipType]["orientation"] = getDirection(coords)
else:
coordsArray = []
coordsArray.append(coords)
gameData.ships_player1[shipType]["coords"] = coordsArray
else:
if numOfFields > 1:
gameData.ships_player2[shipType]["coords"] = coordsRange
gameData.ships_player2[shipType]["orientation"] = getDirection(coords)
else:
coordsArray = []
coordsArray.append(coords)
gameData.ships_player2[shipType]["coords"] = coordsArray
"""
Set sea mines
"""
def setSeaMines():
availableCoorinates = copy.deepcopy(coordinates)
for x in range(0, gameData.numOfSeaMines):
coord = random.choice(list(availableCoorinates.keys()))
gameData.seaMineCoordinates.append(coord)
"""
Check if coordinate is a sea mine
"""
def isSeaMine(coord):
for seaMine in gameData.seaMineCoordinates:
if coord == seaMine:
return True
return False
"""
Destroy all surrounding filds
"""
def destroyAllSurroundingFields(coord, playerCoordinates, playerBoard, playerShips, availableCoords):
coordsToDestroy = []
coordsToDestroy.append(coord)
coordsToDestroy.append(chr(ord(coord[0]) - 1) + coord[1])
coordsToDestroy.append(coord[0] + str(int(coord[1]) + 1))
coordsToDestroy.append(chr(ord(coord[0]) + 1) + coord[1])
coordsToDestroy.append(coord[0] + str(int(coord[1]) - 1))
coordsToDestroy.append(chr(ord(coord[0]) + 1) + str(int(coord[1]) + 1))
coordsToDestroy.append(chr(ord(coord[0]) + 1) + str(int(coord[1]) - 1))
coordsToDestroy.append(chr(ord(coord[0]) - 1) + str(int(coord[1]) + 1))
coordsToDestroy.append(chr(ord(coord[0]) - 1) + str(int(coord[1]) - 1))
for c in coordsToDestroy:
if c in list(availableCoords.keys()):
if isHit(c, playerCoordinates):
ship = getShip(c, playerShips)
ship["hits"].append(c)
if len(ship["hits"]) == len(ship["coords"]):
print("Sea mine sank " + ship["shipType"] + " (" + c + ").")
else:
print("Sea mine hit " + ship["shipType"] + " (" + c + ").")
playerBoard[c] = '╳'
playerCoordinates[c] = '╳'
else:
print("Sea mine hit the water (" + c + ").")
playerBoard[c] = '≈'
playerCoordinates[c] = '≈'
del availableCoords[c]
"""
Inits game and sets all necessary game data
"""
def initGame():
gameData.player1_coordinates = copy.deepcopy(coordinates)
gameData.player2_coordinates = copy.deepcopy(coordinates)
gameData.player1_board = copy.deepcopy(coordinates)
gameData.player2_board = copy.deepcopy(coordinates)
gameData.availableCoordsP1 = copy.deepcopy(coordinates)
gameData.availableCoordsP2 = copy.deepcopy(coordinates)
print(board(gameData.player1_coordinates))
print("Please place your ships...")
"""
Read player 1 (User) coordinates
"""
getCoordsFromPlayer("set coordinates for aircraftcarrier (5 fields): ", 5, "aircraftcarrier", "", gameData.player1_coordinates)
print(board(gameData.player1_coordinates))
getCoordsFromPlayer("set coordinates for battleship (4 fields): ", 4, "battleship", "aircraftcarrier", gameData.player1_coordinates)
print(board(gameData.player1_coordinates))
getCoordsFromPlayer("set coordinates for cruiser (3 fields): ", 3, "cruiser", "battleship", gameData.player1_coordinates)
print(board(gameData.player1_coordinates))
getCoordsFromPlayer("set coordinates for destroyer1 (2 fields): ", 2, "destroyer1", "cruiser", gameData.player1_coordinates)
print(board(gameData.player1_coordinates))
getCoordsFromPlayer("set coordinates for destroyer2 (2 fields): ", 2, "destroyer2", "destroyer1", gameData.player1_coordinates)
print(board(gameData.player1_coordinates))
getCoordsFromPlayer("set coordinate for submarine1 (1 field): ", 1, "submarine1", "destroyer2", gameData.player1_coordinates)
print(board(gameData.player1_coordinates))
getCoordsFromPlayer("set coordinate for submarine2 (1 field): ", 1, "submarine2", "submarine1", gameData.player1_coordinates)
print(board(gameData.player1_coordinates))
print("Bot opponent is placing ships...")
"""
Switch player to bot
"""
gameData.player1_turn = False
"""
Read player 2 (bot) coordinates
"""
getCoordsFromPlayer("", 5, "aircraftcarrier", "", gameData.player2_coordinates)
getCoordsFromPlayer("", 4, "battleship", "aircraftcarrier", gameData.player2_coordinates)
getCoordsFromPlayer("", 3, "cruiser", "battleship", gameData.player2_coordinates)
getCoordsFromPlayer("", 2, "destroyer1", "cruiser", gameData.player2_coordinates)
getCoordsFromPlayer("", 2, "destroyer2", "destroyer1", gameData.player2_coordinates)
getCoordsFromPlayer("", 1, "submarine1", "destroyer2", gameData.player2_coordinates)
getCoordsFromPlayer("", 1, "submarine2", "submarine1", gameData.player2_coordinates)
if gameData.tasksMode:
setSeaMines()
"""
Checks if specific coordinate is a hit
"""
def isHit(coord, playerCoordinates):
for key in playerCoordinates:
if key == coord:
if playerCoordinates[key] == '◯':
return True
else:
return False
"""
Get the ship by any coordinate of the ship
"""
def getShip(coord, ships):
for key, value in ships.items():
for c in value["coords"]:
if c == coord:
value["shipType"] = key
return value
"""
Checks if a player has won by comparing all coordinates with the hits of all ships
"""
def hasWon(ships):
won = True
for key, value in ships.items():
if len(value["hits"]) != len(value["coords"]):
won = False
return won
"""
Starts the real gameplay
"""
def startGame():
gameData.ready = True
gameData.player1_turn = True
print("Match starts...")
print(board(gameData.player1_coordinates))
print(board(coordinates))
roundNumber = 0
hasPlyer1Won = False
hasPlyer2Won = False
validCoord = True
checkWinner = False
player2LastHit = ''
player2LastHitCounter = 0
while hasPlyer1Won == False and hasPlyer2Won == False:
if gameData.player1_turn:
if gameData.botMode:
coord = random.choice(list(gameData.availableCoordsP1.keys()))
print("Enter target coordinate: " + coord)
del gameData.availableCoordsP1[coord]
else:
coord = inputCoordinates("Enter target coordinate: ", 1)
if coord in list(gameData.availableCoordsP1.keys()):
del gameData.availableCoordsP1[coord]
else:
validCoord = False
if coord is not None:
print("Coordinate has already been selected. Please select another coordinate.")
if coord is not None and validCoord:
if isHit(coord, gameData.player2_coordinates):
ship = getShip(coord, gameData.ships_player2)
ship["hits"].append(coord)
if len(ship["hits"]) == len(ship["coords"]):
print("You sank " + ship["shipType"] + ".")
else:
print("You hit a ship.")
gameData.player1_board[coord] = '╳'
gameData.player2_coordinates[coord] = '╳'
else:
print("You only hit the water.")
gameData.player1_board[coord] = '≈'
gameData.player2_coordinates[coord] = '≈'
if gameData.tasksMode and isSeaMine(coord):
print("You hit a sea mine (" + coord + ").")
destroyAllSurroundingFields(coord, gameData.player1_coordinates, gameData.player2_board, gameData.ships_player1, gameData.availableCoordsP2)
gameData.player1_turn = False
roundNumber = roundNumber + 1
else:
if player2LastHit != '':
if player2LastHitCounter == 0:
coord = chr(ord(player2LastHit[0]) - 1) + player2LastHit[1]
if coord not in gameData.availableCoordsP2:
player2LastHitCounter = player2LastHitCounter + 1
if player2LastHitCounter == 1:
coord = player2LastHit[0] + str(int(player2LastHit[1]) + 1)
if coord not in gameData.availableCoordsP2:
player2LastHitCounter = player2LastHitCounter + 1
if player2LastHitCounter == 2:
coord = chr(ord(player2LastHit[0]) + 1) + player2LastHit[1]
if coord not in gameData.availableCoordsP2:
player2LastHitCounter = player2LastHitCounter + 1
if player2LastHitCounter == 3:
coord = coord[0] + str(int(player2LastHit[1]) - 1)
if coord not in gameData.availableCoordsP2:
coord = random.choice(list(gameData.availableCoordsP2.keys()))
player2LastHit = ''
player2LastHitCounter = 0
player2LastHitCounter = player2LastHitCounter + 1
else:
coord = random.choice(list(gameData.availableCoordsP2.keys()))
del gameData.availableCoordsP2[coord]
if isHit(coord, gameData.player1_coordinates):
player2LastHit = coord
player2LastHitCounter = 0
ship = getShip(coord, gameData.ships_player1)
ship["hits"].append(coord)
if len(ship["hits"]) == len(ship["coords"]):
print("Bot sank " + ship["shipType"] + " (" + coord + ").")
else:
print("Bot hit a ship (" + coord + ").")
gameData.player2_board[coord] = '╳'
gameData.player1_coordinates[coord] = '╳'
else:
print("Bot only hit the water (" + coord + ").")
gameData.player2_board[coord] = '≈'
gameData.player1_coordinates[coord] = '≈'
if gameData.tasksMode and isSeaMine(coord):
print("Bot hit a sea mine (" + coord + ").")
destroyAllSurroundingFields(coord, gameData.player2_coordinates, gameData.player1_board, gameData.ships_player2, gameData.availableCoordsP1)
gameData.player1_turn = True
roundNumber = roundNumber + 1
checkWinner = True
if roundNumber % gameData.octopusCount == 0 and roundNumber != 0 and gameData.tasksMode:
setAvailableCoordsP1 = set(gameData.availableCoordsP1.keys())
setAvailableCoordsP2 = set(gameData.availableCoordsP2.keys())
resultAvailableCoords = list(setAvailableCoordsP1.intersection(setAvailableCoordsP2))
if len(resultAvailableCoords) > 0:
coord = random.choice(resultAvailableCoords)
if coord is not None:
print("Octopus appeard on field " + coord + ".")
if isHit(coord, gameData.player1_coordinates):
ship = getShip(coord, gameData.ships_player1)
ship["hits"].append(coord)
if len(ship["hits"]) == len(ship["coords"]):
print("Octopus sank your " + ship["shipType"] + " (" + coord + ").")
else:
print("Octopus hit your ship (" + coord + ").")
gameData.player1_coordinates[coord] = '+'
gameData.player1_board[coord] = '+'
else:
print("Octopus hit the water on your field (" + coord + ").")
gameData.player1_coordinates[coord] = '-'
gameData.player1_board[coord] = '-'
del gameData.availableCoordsP1[coord]
if isHit(coord, gameData.player2_coordinates):
ship = getShip(coord, gameData.ships_player2)
ship["hits"].append(coord)
if len(ship["hits"]) == len(ship["coords"]):
print("Octopus sank bot's " + ship["shipType"] + ".")
else:
print("Octopus hit bot's ship (" + coord + ").")
gameData.player2_coordinates[coord] = '+'
gameData.player2_board[coord] = '+'
else:
print("Octopus hit the water on bot's field (" + coord + ").")
gameData.player2_coordinates[coord] = '-'
gameData.player2_board[coord] = '-'
del gameData.availableCoordsP2[coord]
checkWinner = True
if checkWinner:
print(board(gameData.player1_coordinates))
print(board(gameData.player1_board))
if hasWon(gameData.ships_player2):
hasPlyer1Won = True
if hasWon(gameData.ships_player1):
hasPlyer2Won = True
if hasPlyer1Won and hasPlyer2Won:
print("The match ended in a draw.")
elif hasPlyer1Won == True and hasPlyer2Won == False:
print("Congratulations! You won the match.")
elif hasPlyer1Won == False and hasPlyer2Won == True:
print("Your opponent won the match.")
validCoord = True
checkWinner = False
userInput = input("Do you want to play a rematch? [y/n] ")
if userInput == "y":
main()
else:
print("Exiting the game. Goodbye.")
"""
Main method will be called at the beginning of the program
"""
def main():
global gameData
gameData = GameData()
if sys.argv is not None:
if "-b" in sys.argv:
gameData.botMode = True
if "-t" in sys.argv:
gameData.tasksMode = True
initGame()
startGame()
if __name__ == "__main__":
main()