-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
68 lines (55 loc) · 2.24 KB
/
Game.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
from Board import Board
from ComputerPlayers import AI
class Game:
def __init__(self, playerBoard: Board, computerBoard: Board, computerPlayer: AI):
self._playerBoard = playerBoard
self._computerBoard = computerBoard
self._computerPlayer = computerPlayer
self._computerPlayer.place()
self._yvalues = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7}
def player_place(self, x, y, shipType, orientation):
if not x.isnumeric() or y not in self._yvalues:
raise ValueError("Bad coordinates")
x = int(x)
if x not in range(8):
raise ValueError("Bad coordinates")
y = self._yvalues[y]
if shipType not in ['cruiser', 'destroyer', 'battleship']:
raise ValueError("Bad ship type")
if orientation not in ['V', 'H']:
raise ValueError("Bad orientation")
self._playerBoard.place(x, y, shipType, orientation)
def is_finished(self):
return self._computerBoard.hits == 9 or self._playerBoard.hits == 9
def player_hit(self, x, y):
if not x.isnumeric() or y not in self._yvalues:
raise ValueError("Coords should be int")
x = int(x)
y = self._yvalues[y]
return self._computerBoard.hit(x, y)
def computer_hit(self):
self._computerPlayer.take_shot()
def boards(self):
class Boards:
def __init__(self, myBoard, hitBoard):
self._myBoard = myBoard
self._hitBoard = hitBoard
@property
def myBoard(self):
return self._myBoard
@property
def hitBoard(self):
return self._hitBoard
return Boards(self._playerBoard.myBoard(), self._computerBoard.board)
def printable_boards(self):
class Boards:
def __init__(self, myBoard, hitBoard):
self._myBoard = myBoard
self._hitBoard = hitBoard
@property
def myBoard(self):
return self._myBoard
@property
def hitBoard(self):
return self._hitBoard
return Boards(str(self._playerBoard), str(self._computerBoard.display_for_enemy()))