-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttt.py
161 lines (124 loc) · 3.81 KB
/
ttt.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/local/bin/python3
import numpy as np
from user_tokens import UserTokens as user
class Board(object):
'''
Board object that the users interact with
'''
def __init__(self):
self.Board = None
self._create_board_()
def tokenize(self):
'''
Change from token to string
'''
board = np.array(self.Board)
printout = ""
for i in range(3):
for j in range(3):
if board[i][j] == user.X:
printout += "X"
elif board[i][j] == user.O:
printout += "O"
else:
printout += "-"
return printout
def set_board_from_string(self, string):
'''
Convert string to token on the board
'''
board = np.array(self.Board).flatten()
for i in range(9):
if string[i] is "X":
board[i] = user.X
elif string[i] is "O":
board[i] = user.O
else:
board[i] = user.available
self.Board = board.reshape(3,3)
def print_board(self):
'''
Prints the board
'''
board = np.array(self.Board)
printout = ""
for i in range(3):
for j in range(3):
if board[i][j] == user.X:
printout += "X"
elif board[i][j] == user.O:
printout += "O"
else:
printout += " "
if j is not 2:
printout += "|"
if i is not 2:
printout += "\n-----\n"
return printout
def game_status(self):
'''
Checks for a winner of the game
'''
if len(self.available_spaces()) == 0:
return user.draw
board = np.array(self.Board)
for r in range(3):
row = board[r]
column = board[:,r]
if np.all(row == user.X):
return user.X
if np.all(row == user.O):
return user.O
if np.all(column == user.X):
return user.X
if np.all(column == user.O):
return user.O
diagonal = np.diagonal(board)
if np.all(diagonal == user.O):
return user.O
if np.all(diagonal == user.X):
return user.X
board_flip = np.fliplr(board)
reverse_diagonal = np.diagonal(board_flip)
if np.all(reverse_diagonal == user.O):
return user.O
if np.all(reverse_diagonal == user.X):
return user.X
return user.available
def take_space(self, user, space):
'''
puts the player on the board
'''
board = np.array(self.Board).flatten()
board[space] = user
self.Board = board.reshape(3,3)
def open_space(self, space):
'''
Check if space is open
'''
board = np.array(self.Board).flatten().tolist()
return user.available == board[space]
def reset_game(self):
'''
Clears the board.
'''
self._create_board_()
def available_spaces(self):
'''
Returns all available spaces
'''
count = 0
spaces = []
board = self.Board.flatten()
for space in np.nditer(board):
if space == -1:
spaces.append(count)
count += 1
return spaces
def _create_board_(self):
'''
Private method sets the board to available.
'''
self.Board = np.matrix([[user.available, user.available, user.available],
[user.available, user.available, user.available],
[user.available, user.available, user.available]])