-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.py
267 lines (212 loc) · 6.83 KB
/
TicTacToe.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import random
from itertools import combinations
class Board(object):
def __init__(self):
self.board = {x:None for x in (7,8,9,4,5,6,1,2,3)}
def display(self):
d_board = '\nTIC TAC TOE:\n'
for pos, obj in self.board.items():
if obj == None:
d_board += '_'
elif obj == 'X':
d_board += 'X'
elif obj == 'O':
d_board += 'O'
if pos%3 == 0:
d_board += '\n'
print(d_board)
def getAvailable(self):
available = []
for pos, obj in self.board.items():
if obj == None:
available.append(pos)
return available
class Tic_Tac_Toe(Board):
pieces = ['O', 'X']
def __init__(self):
super().__init__()
self.piece = Tic_Tac_Toe.pieces.pop(random.choice([0,1]))
self.cp_piece = Tic_Tac_Toe.pieces[0]
def usrInput(self, position):
self.board[position] = self.piece
def user_getPiece(self):
return self.piece
def cp_setPiece(self):
self.board[random.choice(self.getAvailable())] = self.cp_piece
def cp_getPiece(self):
return self.cp_piece
def win(self, player):
def at_least_one(A, B):
for i in A:
for j in B:
if i == j:
return True
return False
winningPatern = [(1,2,3),(4,5,6),(7,8,9),
(1,4,7),(2,5,8),(3,6,9),
(3,5,7),(1,5,9)]
spots = [k for k, v in self.board.items() if v == player]
spots.sort()
player_combinations = list(combinations(spots,3))
if at_least_one(player_combinations, winningPatern) == True:
return True
return False
def MainBoard(self):
if None not in self.board.values():
self.display()
print('HOUuSE FuLl')
return True
return False
def main():
# Setup game
game = Tic_Tac_Toe()
input('hELLO fRIEnDs : PRESS ENTER TO BEGIN')
if game.user_getPiece() == 'X':
print('x IS fIRST.')
else:
print('Y IS sECOND.')
game.cp_setPiece()
# Main game loop
while True:
game.display()
position = input('Use the number pad on the lefthand side of your keyboard\nto select your position (1-9):')
try:
position = int(position)
if position in range(1,10):
if position in game.getAvailable():
game.usrInput(position)
else:
print('==>Please input an available position.')
continue
else:
print('==>Please input a number between 1 and 9.')
except ValueError:
print('==>Please input a number.')
continue
# FOR USER
# Check for win
if game.win(game.user_getPiece()) == True:
game.display()
print('VICTORY!')
break
# Check for full board
if game.MainBoard() == True:
break
# FOR COMPUTER
game.cp_setPiece()
# Check for win
if game.win(game.cp_getPiece()) == True:
game.display()
print('Sorry. You lost.')
break
# Check for full board
if game.MainBoard() == True:
break
if __name__ == '__main__':
main()
import random
from itertools import combinations
class Board(object):
def __init__(self):
self.board = {x:None for x in (7,8,9,4,5,6,1,2,3)}
def display(self):
d_board = '\nTIC TAC TOE:\n'
for pos, obj in self.board.items():
if obj == None:
d_board += '_'
elif obj == 'X':
d_board += 'X'
elif obj == 'O':
d_board += 'O'
if pos%3 == 0:
d_board += '\n'
print(d_board)
def getAvailable(self):
available = []
for pos, obj in self.board.items():
if obj == None:
available.append(pos)
return available
class Tic_Tac_Toe(Board):
pieces = ['O', 'X']
def __init__(self):
super().__init__()
self.piece = Tic_Tac_Toe.pieces.pop(random.choice([0,1]))
self.cp_piece = Tic_Tac_Toe.pieces[0]
def usrInput(self, position):
self.board[position] = self.piece
def user_getPiece(self):
return self.piece
def cp_setPiece(self):
self.board[random.choice(self.getAvailable())] = self.cp_piece
def cp_getPiece(self):
return self.cp_piece
def win(self, player):
def at_least_one(A, B):
for i in A:
for j in B:
if i == j:
return True
return False
winningPatern = [(1,2,3),(4,5,6),(7,8,9),
(1,4,7),(2,5,8),(3,6,9),
(3,5,7),(1,5,9)]
spots = [k for k, v in self.board.items() if v == player]
spots.sort()
player_combinations = list(combinations(spots,3))
if at_least_one(player_combinations, winningPatern) == True:
return True
return False
def MainBoard(self):
if None not in self.board.values():
self.display()
print('HoUsE FuLL')
return True
return False
def main():
# Setup game
game = Tic_Tac_Toe()
input('hELLO fRIEnDs : PRESS ENTER TO BEGIN')
if game.user_getPiece() == 'X':
print('x IS fIRST')
else:
print('O IS sECONd')
game.cp_setPiece()
# Main game loop
while True:
game.display()
position = input('Use the number pad on the lefthand side of your keyboard\nto select your position (1-9):')
try:
position = int(position)
if position in range(1,10):
if position in game.getAvailable():
game.usrInput(position)
else:
print('==>Please input an available position.')
continue
else:
print('==>Please input a number between 1 and 9.')
except ValueError:
print('===>Please input a number.')
continue
# FOR USER
# Check for win
if game.win(game.user_getPiece()) == True:
game.display()
print('WINNER WINNER BEEF DINNER :-)')
break
# Check for full board
if game.MainBoard() == True:
break
# FOR COMPUTER
game.cp_setPiece()
# Check for win
if game.win(game.cp_getPiece()) == True:
game.display()
print('You lose.')
break
# Check for full board
if game.MainBoard() == True:
break
if __name__ == '__main__':
main()