-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.py
40 lines (33 loc) · 1.65 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
class Game:
def __init__(self):
self.board = [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']] # don't use [['-'] * 3] * 3
self._last_character_turned = None # to track which player has given the last turn
def reset_game(self):
self.__init__()
def game_state(self):
for i in range(0, 3):
row_sum = ord(self.board[i][0]) + ord(self.board[i][1]) + ord(self.board[i][2])
if (row_sum == (ord('x') * 3)) or (row_sum == (ord('o') * 3)):
return 'w' # won
column_sum = ord(self.board[0][i]) + ord(self.board[1][i]) + ord(self.board[2][i])
if (column_sum == (ord('x') * 3)) or (column_sum == (ord('o') * 3)):
return 'w' # won
diagonal_sum = ord(self.board[0][0]) + ord(self.board[1][1]) + ord(self.board[2][2])
if (diagonal_sum == (ord('x') * 3)) or (diagonal_sum == (ord('o') * 3)):
return 'w' # won
diagonal_sum = ord(self.board[0][2]) + ord(self.board[1][1]) + ord(self.board[2][0])
if (diagonal_sum == (ord('x') * 3)) or (diagonal_sum == (ord('o') * 3)):
return 'w' # won
for i in range(0, 3):
for j in range(0, 3):
if self.board[i][j] == '-':
return # ongoing
return 'd' # draw
def add_turn_internal(self, character, posX, posY):
if self._last_character_turned == character:
return "Please wait for your opponent!"
elif self.board[posX][posY] != '-':
return "Illegal turn!"
else:
self.board[posX][posY] = character
self._last_character_turned = character