-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
44 lines (36 loc) · 1.22 KB
/
board.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
# Board handler class for internal games and the global game
from eval import Evaluator
class Gamestates:
PLAY = 1
DRAW = 2
X_WIN = 3
O_WIN = 4
class Board:
def __init__(self, position):
self.position = position
self.gamestate = Gamestates.PLAY
self.pos = [str(x) for x in range(1, 10)]
# Run a cycle of tic-tac-toe
def tick(self, square: int, turn: bool) -> bool:
try:
if self.pos[square - 1] in ["X", "O"]:
return False
except TypeError:
return False
# Update the pos array
if not turn:
# Player X
self.pos[square - 1] = "X"
else:
# Player O
self.pos[square - 1] = "O"
# Check if a win or draw circumstance has happened
# Ensure this check only happens when it needs to
if self.gamestate == Gamestates.PLAY:
if not (winner := Evaluator.who_won(self)) and Evaluator.is_board_full(self):
self.gamestate = Gamestates.DRAW
elif winner == "X":
self.gamestate = Gamestates.X_WIN
elif winner == "O":
self.gamestate = Gamestates.O_WIN
return True