-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTicTacToe.py
More file actions
244 lines (155 loc) · 5.02 KB
/
TicTacToe.py
File metadata and controls
244 lines (155 loc) · 5.02 KB
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
'''
Tic Tac Toe Requirements:
- Mutiple Players can play
- Players able to win by any winning strategy
- There can be multiple winning strategies
- Entities can have types
- Board can be customised with given size
- Games have specific players and board
'''
from abc import ABC, abstractmethod
###### BOARDS
class Board(ABC):
@abstractmethod
def display_board(self):
pass
@abstractmethod
def set_board(self):
pass
class TicTacToeBoard(Board):
def __init__(self,rows,cols):
self.rows=rows
self.cols=cols
self.board=[["-"]*cols for _ in range(rows)]
self.empty=rows*cols
def set_board(self,row,col,entity):
self.board[row][col]=entity
self.empty-=1
def is_available(self,row,col):
if self.board[row][col]=="-":
return True
return False
def display_board(self):
print()
for row in self.board:
for col in row:
if col!="-":
print(f"{col.value} ",end="")
else:
print(f"{col} ",end="")
print("\n")
###### ENTITIES
from enum import Enum
class Entity(Enum):
CIRCLE = "O"
CROSS = "X"
######## WINNING STRATEGY
class WinningStrategy(ABC):
@abstractmethod
def check_win(self,move, board: Board) -> bool:
pass
class StandardWin(WinningStrategy):
def check_win(self, move, board: Board) -> bool:
entity=board.board[move.row][move.col]
row_check = all([sq==entity for sq in board.board[move.row]])
if row_check: return True
col_check = True
for row in range(board.rows):
if board.board[row][move.col]!=None and board.board[row][move.col]==entity:
continue
else:
col_check=False
break
if col_check: return True
return False
###### PLAYERS
class Player(ABC):
def __init__(self,username) -> None:
self.username=username
self.games={}
self.current_game={}
class TicTacToePlayer(Player):
def __init__(self, username, entity) -> None:
super().__init__(username)
self.entity_selected=entity
###### MOVES
class Move(ABC):
def __init__(self,playerid,row,col) -> None:
self.player=playerid
self.row=row
self.col=col
@abstractmethod
def execute(self,board):
pass
class TicTacToeMove(Move):
def __init__(self, playerid, row, col) -> None:
super().__init__(playerid, row, col)
def execute(self,board):
self.board[self.row][self.col]=self.playerid.entity_selected
####### GAME
class Game(ABC):
def __init__(self,id, board,players,win_strategy) -> None:
self.gameId=id
self.players=players
self.board=board
self.win_strategy=win_strategy
self.winner=None
self.moves=[]
self.current_turn=None
@abstractmethod
def save_move(self,move):
pass
@abstractmethod
def check_winner(self,entity):
pass
@abstractmethod
def make_move(self, player:Player, nextMove, entity):
pass
@abstractmethod
def get_turn(self):
pass
class TicTacToeGame(Game):
def __init__(self, id, board, players, win_strategy) -> None:
super().__init__(id, board, players, win_strategy)
def save_move(self, move):
self.moves.append(move)
def check_winner(self,move):
return self.win_strategy.check_win(move,self.board)
def make_move(self,player,nextMove):
if self.board.is_available(nextMove.row,nextMove.col):
self.board.set_board(nextMove.row,nextMove.col,player.entity_selected)
self.save_move(nextMove)
return True
return False
def get_turn(self):
idx=self.current_turn
if idx==None:
idx=0
if idx==len(self.players)-1:
idx=0
else:
idx+=1
self.current_turn = idx
return self.players[self.current_turn]
####### Driver code
def tic_tac_toe_driver(board:TicTacToeBoard,players):
print("\n\n_____________ Welcome to Tic Tac Toe ___________\n\nStrategise the Board.....")
board.display_board()
game=TicTacToeGame(1,board,players,StandardWin())
while game.winner==None and board.empty>0:
player=game.get_turn()
print("Present Turn :",player.username,end=" ")
moves=input("|| Row Col: ")
row,col=list(map(int,moves.split(" ")))
move=TicTacToeMove(player,row,col)
if not(game.make_move(player,move)):
print("Could not make the move!")
board.display_board()
if game.check_winner(move):
print(f"\n____________{player.username} Won!!!!__________")
return
print(f"Its a draw!!!")
if __name__== "__main__":
board=TicTacToeBoard(3,3)
players=[TicTacToePlayer("AAA",Entity.CIRCLE),TicTacToePlayer("BBB",Entity.CROSS)]
tic_tac_toe_driver(board,players)