-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
68 lines (48 loc) · 1.33 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
MAX_PLAYERS = 2
class Error(Exception):
pass
class TooManyPlayersError(Error):
pass
class Game(object):
def __init__(self, game_id):
self.creator = None
self.game_id = game_id
self.players = []
self.is_started = False
self.move_request = None
self.current_player_index = 0
self.moves = 0
self.winner_index = None
# Pre-game setup
def AddPlayer(self, player):
if self.IsFull():
raise TooManyPlayersError()
if self.creator is None:
self.creator = player
self.players.append(player)
def IsFull(self):
return len(self.players) >= MAX_PLAYERS
def IsStarted(self):
return self.is_started
# Running the game
def Start(self):
assert self.IsFull()
assert self.move_request is None
self.move_request = PassOrWinMoveRequest()
self.is_started = True
def Play(self, move):
self.move_request = self.move_request.UpdateGame(self, move)
# State queries
def GetCurrentPlayer(self):
return self.players[self.current_player_index]
class PassOrWinMoveRequest(object):
def __init__(self):
pass
def UpdateGame(self, game, win):
if win:
game.winner_index = game.current_player_index
return None
else:
game.current_player_index = (game.current_player_index + 1) % 2
game.moves += 1
return PassOrWinMoveRequest()