-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasses.py
More file actions
288 lines (229 loc) · 9.03 KB
/
Copy pathClasses.py
File metadata and controls
288 lines (229 loc) · 9.03 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
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
275
276
277
278
279
280
281
282
283
284
285
# coding UTF-8
from tkinter import *
from tkinter import ttk
import tkinter
class Player:
def __init__(self, id):
self.player = {
'id': id,
'symbol':'',
'GUI_symbol':'',
'name':''
}
print('\n player %d created'% self.player['id'])
@property
def get_id(self):
return self.player['id']
@property
def get_symbol(self):
return self.player['symbol']
@property
def get_GUI_symbol(self):
return self.player['GUI_symbol']
def set_symbol(self, symbol):
self.player['symbol'] = symbol
def set_GUI_symbol(self, symbol):
self.player['GUI_symbol'] = symbol
class Grid:
def __init__(self, size):
self.grid = self.create_grid(size)
print('\n grid size %d x %d created' % (size,size))
print(self.grid)
def create_grid(self, size):
return [[0]*size for row in range(size)]
def print_grid(self):
for line in self.grid:
print(line)
def set_cell(self, row, cell, player):
self.grid[row][cell] = player
def is_player_symbols_aligned(self):
symbols_aligned = False
# horizontal
for line in self.grid:
line_of_3 = any(line.count(element) == 3 for element in line if element != 0)
if line_of_3:
symbols_aligned = line_of_3
# vertical
columns = [[0]*3 for row in range(3)]
for index in range(3):
columns[0][index] = self.grid[index][0]
columns[1][index] = self.grid[index][1]
columns[2][index] = self.grid[index][2]
for column in columns:
column_of_3 = any(column.count(element) == 3 for element in column if element != 0)
if column_of_3:
symbols_aligned = column_of_3
# diagonal left to right
diagonal_l_r = []
for cell in range(3):
diagonal_l_r.append(self.grid[cell][cell])
diagonal_of_3_l_r = any(diagonal_l_r.count(element) == 3 for element in diagonal_l_r if element != 0)
if diagonal_of_3_l_r:
symbols_aligned = diagonal_of_3_l_r
# diagonal right to left
diagonal_r_l = []
diagonal_r_l.append(self.grid[0][2])
diagonal_r_l.append(self.grid[1][1])
diagonal_r_l.append(self.grid[2][0])
diagonal_of_3_r_l = any(diagonal_r_l.count(element) == 3 for element in diagonal_r_l if element != 0)
if diagonal_of_3_r_l:
symbols_aligned = diagonal_of_3_r_l
return symbols_aligned
def is_grid_filled(self):
played = 0
for line in self.grid:
played += line.count(1)
played += line.count(2)
if played == 9:
return True
class Game:
def __init__(self):
self.game = {
'players': 2,
'grid_size': 3,
'symbols': ['x', 'o'],
'current_player_id': 1,
'winner': 0
}
self.board = self.create_gameboard()
print('\n game ready to start!')
@property
def get_players(self):
return self.game['players']
@property
def get_grid_size(self):
return self.game['grid_size']
@property
def get_symbols(self):
return self.game['symbols']
@property
def get_current_player_id(self):
return self.game['current_player_id']
@property
def get_winner(self):
return self.game['winner']
def get_board(self):
return self.board
def __set_current_player_id(self, id):
self.game['current_player_id'] = id
def __set_winner(self, player):
self.game['winner'] = player
def switch_current_player_id(self):
if self.get_current_player_id == 1:
self.__set_current_player_id(2)
else:
self.__set_current_player_id(1)
def is_game_ended(self, cell_played, player_id):
self.board.get_grid().set_cell(cell_played['row'], cell_played['column'], player_id)
if self.board.get_grid().is_player_symbols_aligned():
self.__set_winner(self.get_current_player_id)
return True
if self.board.get_grid().is_grid_filled():
return True
def create_gameboard(self):
return Gameboard(grid= self.get_grid_size, players=self.get_players, symbols=self.get_symbols)
class Gameboard:
def __init__(self, grid, players, symbols):
self.board = {
'grid': Grid(grid),
'players': [],
}
# creating the players
self.create_players(players)
# assigning symbols to players
self.assign_symbols_to_players(symbols)
print('\n gameboard created')
def get_grid(self):
return self.board['grid']
def get_player(self, player):
return self.board['players'][player]
def get_players(self):
return self.board['players']
def set_players(self, player):
self.board['players'].append(player)
def assign_symbols_to_players(self, symbols):
for player in range(len(self.get_players())):
self.get_player(player).set_symbol(symbols[player])
print('\n player', self.get_player(player).get_id, 'plays', self.get_player(player).get_symbol)
def create_players(self, players):
index = 1
for player in range(players) :
self.set_players(Player(index))
index+=1
class GUI_Gameboard():
BLANK = "img/blank.png"
FIRST = "img/apple.png"
SECOND = "img/grappes.png"
def __init__(self, game):
self.game = game
self.board = game.get_board()
self.create_GUI_board()
def create_GUI_board(self):
window = Tk()
# Creating the app window
window.title("audreyntep")
window.iconbitmap("img/antep.ico")
window.configure(background="white")
# Fixing app window dimensions
window_dimension = 500
window.geometry("{side}x{side}+{margin_left}+100".format(side=window_dimension, margin_left=int(window.winfo_screenwidth() / 2 - window_dimension / 2)))
window.resizable(width=False, height=False)
# Filling app window content
title = Label(window, text="Tic Tac Toe", font=("Courrier", 18, "bold"), pady=20).pack(side='top', fill="x")
message = Label(window, text="Let' Go", bg="#5cc7b2", font=14, pady=10).pack(side='top', fill="x")
# Creating a GUI grid with 3 rows, 3 cells each filled with blank card
GUI_grid = Frame(window)
blank = PhotoImage(file=self.BLANK)
for line in range(3):
row = Frame(GUI_grid)
for cell in range(3):
btn = Button(row, image=blank)
btn.config(command=lambda btn=btn: self.on_click(btn))
btn.grid(row=line, column=cell)
row.grid(row=line, column=1)
GUI_grid.pack(expand=YES)
# assigning GUI symbols to players
self.assign_GUI_symbols_to_players()
return window.mainloop()
def assign_GUI_symbols_to_players(self):
# looping on players
for player in self.board.get_players():
if player.get_id == 1:
player.set_GUI_symbol(PhotoImage(file=self.FIRST))
print('\n player', player.get_id,'plays', self.FIRST, player.get_GUI_symbol)
else:
player.set_GUI_symbol(PhotoImage(file=self.SECOND))
print('\n player', player.get_id,'plays', self.SECOND, player.get_GUI_symbol)
def on_click(self, btn):
# getting player with current id
player_id = self.game.get_current_player_id
player = self.board.get_player(player_id-1)
# changing image
if btn.cget('image') == 'pyimage1':
btn.configure(image=player.get_GUI_symbol)
# getting cell position (row and column)
cell = btn.grid_info()
# checking if grid is filled or if player won
if self.game.is_game_ended(cell, player_id):
if self.game.get_winner != 0 :
self.GUI_message_window("Congratulation player %d" % self.game.get_winner)
else :
self.GUI_message_window('no winner!!!')
# changing player
else:
self.game.switch_current_player_id()
def GUI_message_window(self, message):
window = Tk()
# Creating the app window
window.title("audreyntep")
window.iconbitmap("img/antep.ico")
window.configure(background="white")
# Fixing app window dimensions
window_dimension = 400
window.geometry("{side}x{side}+{margin_left}+200".format(side=window_dimension, margin_left=int(window.winfo_screenwidth() / 2 - window_dimension / 2)))
window.resizable(width=False, height=False)
# Filling app window content
content = Frame(window, background="white")
message = Label(content, text=message, bg="white", font=14, padx=20, pady=20).pack()
content.pack(expand=YES)
return window.mainloop()