-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe.py
274 lines (221 loc) · 7.44 KB
/
tic_tac_toe.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
268
269
270
271
272
273
274
# Tic-tac-toe's game v0.0.3
#
# Python 3.8.3
#
#
import math
from abc import ABCMeta, abstractmethod
from random import randint
class Piece:
"""Represents a piece on the board"""
__counter = -1
def __init__(self):
"""Init piece
Args:
None
Returns:
None
"""
# The piece values could be -1 or 1
Piece.__counter *= -1
self.value = Piece.__counter
self.symbol = ['O', 'X'][(self.value == -1) * -1 + 1]
def symbol(self):
"""Represent a piece
Args:
None
Returns:
str: symbol than represents piece [ X or O ]
"""
return self.symbol
def piece(self):
"""Represent a piece
Args:
None
Returns:
int: value from piece
"""
return self.value
def __str__(self):
"""The piece
Args:
None
Returns:
str: represents the piece ('O' or 'X')
"""
return self.symbol
class PlayerFactory:
@staticmethod
def new(is_type):
#self.new(is_type.lower())
if is_type == 'human':
return Human()
elif is_type == 'computer':
return Computer()
else:
raise Exception("Sorry, you must setting a human or computer player!")
class Player(metaclass=ABCMeta):
"""Abstract class to represents a Player"""
__counter = 0
@abstractmethod
def __init__(self):
self.piece = None
self.name = None
@staticmethod
def new_player():
"""The player
Args:
None
Returns:
int: Number's player
"""
assert(Player.__counter < 2), "Player limit reached!"
Player.__counter += 1
return Player.__counter
def add_piece(self, piece):
if isinstance(piece, Piece):
self.piece = piece
else:
raise Exception("Sorry, you cannot set piece!")
def __str__(self):
"""The player
Args:
None
Returns:
str: Name of player
"""
return self.name
class Human(Player):
"""Concrete class to represents a Human Player"""
def __init__(self):
super().__init__()
self.name = "Human " + str(self.new_player())
# WARNING!
# This implementation class is not work YET!
#
class Computer(Player):
"""Concrete class to represents a Computer Player"""
def __init__(self):
super().__init__()
self.name = "Computer " + str(self.new_player())
def next_turn(self, board, maximizing_value):
return self.minimax(board, maximizing_value)
def minimax(self, board, maximizing_value):
return 1
class Board:
"""Tic-tac-toe Board"""
def __init__(self):
self.grid = [0] * 9
def board_is_full(self):
return not self.grid.count(0)
def board_is_empty(self):
return self.grid.count(0) == 9
def cell_is_empty(self, position):
return self.grid[position] == 0
def check_winner(self):
sum3 = self.grid[0] + self.grid[4] + self.grid[8] # Diagonal 1
sum4 = self.grid[2] + self.grid[4] + self.grid[6] # Diagonal 2
if abs(sum3) == 3 or abs(sum4) == 3:
return (sum3 == -3 or sum4 == -3)*-1 or (sum3 == 3 or sum4 == 3)*1
for x in range(3):
sum1 = self.grid[x*3] + self.grid[x*3+1] + self.grid[x*3+2] # Horizontal
sum2 = self.grid[x] + self.grid[x+3] + self.grid[x+6] # Vertical
if abs(sum1) == 3 or abs(sum2) == 3:
return (sum1 == -3 or sum2 == -3)*-1 or (sum1 == 3 or sum2 == 3)*1
return 0
def __str__(self):
string = ""
for x in range(9):
if self.grid[x] == 0:
string = string + str(x+1)
else:
string = string + ['O', 'X'][(self.grid[x] + 1) // 2]
if x != 0 and (x+1) % 3 == 0 and (x+1) != 9:
string = string + "\n-----\n"
elif (x+1) != 9:
string = string + "|"
return string
class Game:
"""Game's logic"""
def __init__(self):
self.board = Board()
self.player = []
self.current_player = None
self.winner = None
def turn(self, player, position=None):
if isinstance(player, Computer):
position = player.next_turn(self.board, player.piece)
if self.cell_is_empty(position):
self.board.grid[position] = player.piece.value
return True
return False
def play(self):
print("*" * 10 + ' Welcome TicTacToe ' + "*" * 10)
auxiliary = 0
watchdog = True
while watchdog:
print('(1) Human x Computer, (2) Human x Human or (3) Computer x Computer')
auxiliary = input()
if auxiliary.isdigit():
# if 0 < int(auxiliary) < 4:
# watchdog = False
if auxiliary == '1':
print("Not implemented yet!")
# self.new_player('human')
# self.new_player('computer')
elif auxiliary == '2':
watchdog = False
self.new_player('human')
self.new_player('human')
elif auxiliary == '3':
print("Not implemented yet!")
# self.new_player('computer')
# self.new_player('computer')
self.first_player() # select who will play first
print("First player will be " + self.player_name() +
' - Your piece is (' + self.player_piece() + ')')
watchdog = True
while watchdog and self.is_board_full:
print(self.board)
print(self.player_name(), "( ", self.player_piece(), " ) make your move, choose the board place (1-9): ")
user_input = input()
if user_input.isdigit():
position = int(user_input)
if 1 <= position <= 9:
position -= 1
if self.cell_is_empty(position):
self.turn(self.current_player, position)
if self.board.check_winner():
self.winner = self.player_name()
watchdog = False
else:
self.next_player()
else:
print('Invalid choose!')
print(self.board)
print("Game over!")
if not not self.winner:
print("The winner is ", self.winner)
else:
print('Draw!')
def is_board_full(self):
return self.board.board_is_full()
def cell_is_empty(self, position):
return self.board.cell_is_empty(position)
def player_name(self):
return self.current_player.name
def player_piece(self):
return self.current_player.piece.symbol
def next_player(self):
self.current_player = self.player[1] if self.current_player == self.player[0] else self.player[0]
def new_player(self, is_type):
self.player.append(PlayerFactory.new(is_type))
self.player[len(self.player) - 1].add_piece(Piece())
def first_player(self):
"""Choose first player randomily
Args:
None
Returns:
None
"""
self.current_player = self.player[randint(0, 100) % 2]