-
Notifications
You must be signed in to change notification settings - Fork 1
/
tictactoe_v3_game_object.py
80 lines (62 loc) · 1.98 KB
/
tictactoe_v3_game_object.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
import sys
from enum import Enum
class Player(Enum):
X = "X"
O = "O"
NA = " "
def next_player(self):
next_players = {
self.X: self.O,
self.O: self.X,
}
return next_players[self]
# STORAGE-START
class Board():
def __init__(self):
self.board = [[Player.NA]*3 for _ in range(3)]
self.player = Player.X
# STORAGE-END
def __str__(self):
return "--+---+--\n".join(
" | ".join(play.value for play in row) + "\n"
for row in self.board
)
# ACTION-START
def do_move(self):
move = input(f"Player {self.player.value} move (x y)? ")
x, y = move.split()
if self.board[int(x)][int(y)] == Player.NA:
self.board[int(x)][int(y)] = self.player
self.player = self.player.next_player()
# ACTION-END
# BUG-START
def do_move_buggy(self):
move = input(f"Player {self.player.value} move (x y)? ")
x, y = move.split()
if self.board[int(x)][int(y)] == Player.NA:
self.board[int(x)][int(y)] = self.player
self.player = self.player.next_player()
# BUG-END
def is_finished(self):
for row in self.board:
if row[0] != Player.NA and row[0] == row[1] == row[2]:
return True
for column in range(3):
if self.board[0][column] != Player.NA and self.board[0][column] == self.board[1][column] == self.board[2][column]:
return True
if self.board[0][0] != Player.NA and self.board[0][0] == self.board[1][1] == self.board[2][2]:
return True
if self.board[2][0] != Player.NA and self.board[2][0] == self.board[1][1] == self.board[0][2]:
return True
return False
# LOOP-START
def main():
board = Board()
while not board.is_finished():
print(board)
board.do_move()
print("Game Over!")
print(board)
# LOOP-END
if __name__ == "__main__":
sys.exit(main())