24
24
25
25
3. Exiting the INIT_WINDOW would exit the game.
26
26
27
+ 4. RESET would reset thecurrent game session and
28
+ start over a fresh board.
29
+
27
30
"""
28
31
29
32
import os
@@ -84,36 +87,35 @@ def progress_game(key: str, player_marker: str):
84
87
85
88
return continue_with_next_game
86
89
87
- def is_row_column_diagonal_complete (row_col_num : int = - 1 , is_row : bool = True , is_diagonal : bool = False ):
90
+ def is_row_column_diagonal_complete (row_col_num : int = - 1 , is_row : bool = True ,
91
+ is_diagonal : bool = False ):
88
92
'''checks if the given row or column is complete
89
93
to proceed with a winner.'''
94
+ is_complete : bool = False
90
95
91
96
if is_diagonal is False and row_col_num != - 1 :
92
97
if is_row :
93
98
row = row_col_num
94
- if GAME_PROGRESS_ARRAY [row ][0 ] != '' and \
95
- GAME_PROGRESS_ARRAY [row ][1 ] != '' and \
96
- GAME_PROGRESS_ARRAY [row ][2 ] != '' :
97
- return True
98
- else :
99
- return False
99
+ is_complete = GAME_PROGRESS_ARRAY [row ][0 ] != '' and \
100
+ GAME_PROGRESS_ARRAY [row ][1 ] != '' and \
101
+ GAME_PROGRESS_ARRAY [row ][2 ] != ''
100
102
else :
101
103
col = row_col_num
102
- if GAME_PROGRESS_ARRAY [0 ][col ] != '' and \
103
- GAME_PROGRESS_ARRAY [1 ][col ] != '' and \
104
- GAME_PROGRESS_ARRAY [2 ][col ] != '' :
105
- return True
106
- else :
107
- return False
104
+ is_complete = GAME_PROGRESS_ARRAY [0 ][col ] != '' and \
105
+ GAME_PROGRESS_ARRAY [1 ][col ] != '' and \
106
+ GAME_PROGRESS_ARRAY [2 ][col ] != ''
108
107
else :
109
108
if GAME_PROGRESS_ARRAY [0 ][0 ] != '' and \
110
109
GAME_PROGRESS_ARRAY [1 ][1 ] != '' and \
111
110
GAME_PROGRESS_ARRAY [2 ][2 ] != '' :
112
- return True
113
- elif GAME_PROGRESS_ARRAY [2 ][0 ] != '' and \
111
+ is_complete = True
112
+
113
+ if GAME_PROGRESS_ARRAY [2 ][0 ] != '' and \
114
114
GAME_PROGRESS_ARRAY [1 ][1 ] != '' and \
115
115
GAME_PROGRESS_ARRAY [0 ][2 ] != '' :
116
- return True
116
+ is_complete = True
117
+
118
+ return is_complete
117
119
118
120
119
121
def mark_the_winner (row_is_winner : bool , row_column_index : int = - 1 , diagonal_is_winner : bool = False ):
@@ -174,7 +176,6 @@ def is_winning():
174
176
if is_row_column_diagonal_complete (row_col_num = row , is_row = True ):
175
177
if GAME_PROGRESS_ARRAY [row ][0 ] == GAME_PROGRESS_ARRAY [row ][1 ] == GAME_PROGRESS_ARRAY [row ][2 ]:
176
178
mark_the_winner (row_is_winner = True , row_column_index = row )
177
- # continue_game = display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[row][0])
178
179
CHECK_FOR_WINNER = False
179
180
return True , GAME_PROGRESS_ARRAY [row ][0 ]
180
181
@@ -183,41 +184,38 @@ def is_winning():
183
184
if is_row_column_diagonal_complete (row_col_num = col , is_row = False ):
184
185
if GAME_PROGRESS_ARRAY [0 ][col ] == GAME_PROGRESS_ARRAY [1 ][col ] == GAME_PROGRESS_ARRAY [2 ][col ]:
185
186
mark_the_winner (row_is_winner = False , row_column_index = col )
186
- # display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[0][col])
187
187
CHECK_FOR_WINNER = False
188
188
return True , GAME_PROGRESS_ARRAY [0 ][col ]
189
-
189
+
190
190
# check for the 2 diagonals for a winning sequence.
191
191
if is_row_column_diagonal_complete (is_diagonal = True ):
192
192
if GAME_PROGRESS_ARRAY [0 ][0 ] == GAME_PROGRESS_ARRAY [1 ][1 ] == GAME_PROGRESS_ARRAY [2 ][2 ]:
193
193
MAIN_DIAGONAL_IS_WINNER = True
194
194
mark_the_winner (row_column_index = - 1 , row_is_winner = False , diagonal_is_winner = True )
195
- # display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[1][1])
196
195
CHECK_FOR_WINNER = False
197
196
return True , GAME_PROGRESS_ARRAY [1 ][1 ]
198
197
199
198
elif GAME_PROGRESS_ARRAY [2 ][0 ] == GAME_PROGRESS_ARRAY [1 ][1 ] == GAME_PROGRESS_ARRAY [0 ][2 ]:
200
199
mark_the_winner (row_column_index = - 1 , row_is_winner = False , diagonal_is_winner = True )
201
- # display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[1][1])
202
200
CHECK_FOR_WINNER = False
203
201
return True , GAME_PROGRESS_ARRAY [1 ][1 ]
204
-
202
+
205
203
return False , ''
206
204
207
205
def display_winner_and_continue (winning_marker : str ):
208
206
'''display the winner of the current board.'''
209
207
210
208
if winning_marker == PLAYER1_MARKER :
211
209
popup_result = sg .PopupYesNo ('The Winner is ' + PLAYER1_NAME + '.\n Do you want to play another game with the current players?' ,
212
- title = 'Board Winner!' , text_color = 'darkblue' , icon = GAME_ICON ,
213
- grab_anywhere = True , font = ('Blackadder ITC' , 20 ))
210
+ title = 'Board Winner!' , text_color = 'darkblue' , icon = GAME_ICON ,
211
+ grab_anywhere = True , font = ('Blackadder ITC' , 20 ))
214
212
elif winning_marker == PLAYER2_MARKER :
215
213
popup_result = sg .PopupYesNo ('The Winner is ' + PLAYER2_NAME + '.\n Do you want to play another game with the current players?' ,
216
- title = 'Board Winner!' , text_color = 'darkblue' , icon = GAME_ICON ,
217
- grab_anywhere = True , font = ('Blackadder ITC' , 20 ))
214
+ title = 'Board Winner!' , text_color = 'darkblue' , icon = GAME_ICON ,
215
+ grab_anywhere = True , font = ('Blackadder ITC' , 20 ))
218
216
219
217
return popup_result
220
-
218
+
221
219
def init_game_window ():
222
220
'''Initializes and creates the game options window.'''
223
221
init_game_layout = [[sg .Text ('Player 1 Name: ' , size = (12 , 1 )),
@@ -271,6 +269,8 @@ def initialize_game_board():
271
269
GAME_BOARD_LAYOUT += [[sg .Button (' ' , size = (8 , 4 ), key = str (j )+ str (i ))
272
270
for i in range (3 )] for j in range (3 )]
273
271
272
+ GAME_BOARD_LAYOUT += [[sg .Button ("Reset Game" , key = "-RESET-" , tooltip = 'Resets the current session.' )]]
273
+
274
274
BOARD = sg .Window ('Tic Tac Toe' , GAME_BOARD_LAYOUT , icon = GAME_ICON , finalize = True )
275
275
276
276
BOARD .TKroot .protocol ("WM_DESTROY_WINDOW" , lambda : BOARD .write_event_value ("WIN_CLOSE" , ()))
@@ -384,3 +384,7 @@ def initialize_game_board():
384
384
# to win the game board is 5.
385
385
if STEP_COUNTER == 4 :
386
386
CHECK_FOR_WINNER = True
387
+
388
+ if EVENT == '-RESET-' :
389
+ GAME_BOARD .Close ()
390
+ reset_game_board ()
0 commit comments