-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
53 lines (48 loc) · 1.66 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
from configs import DEPTH
from utils import get_logger
from utils import get_pretty_game_state
from utils import is_end_state
class Game(object):
"""Represents a single game"""
def __init__(self, game_size):
self.logger = get_logger(__name__)
self.game_size = game_size
self._state = None
@property
def state(self):
"""Initializes state attribute to an empty game if necessary, then
returns the state attribute.
"""
if self._state is None:
self._state = self.create_new_game()
return self._state
def create_new_game(self):
"""
Initializes a tic-tac-toe board as a 2D-array
of None values.
"""
rows = cols = self.game_size
return [[None] * cols for row in range(rows)]
def update_state(self, new_state):
"""Updates game state."""
self._state = new_state
def loop(self, first, player, ai):
"""Runs the game loop. Prompts the player and ai
to make moves.
"""
logger = self.logger
is_user_turn = first
while not is_end_state(self.state):
logger.info(get_pretty_game_state(self.state))
# User's turn.
if is_user_turn:
player_state = player.get_user_move(self.state)
self.update_state(player_state)
# AI's turn.
else:
ai_state = ai.get_best_state(self.state, True, DEPTH)
self.update_state(ai_state)
# Switch turns.
is_user_turn = not is_user_turn
logger.info(get_pretty_game_state(self.state))
logger.info('GAME OVER.')