File tree

1 file changed

+87
-30
lines changed

1 file changed

+87
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
START_GAME: bool = False
4747
CHECK_FOR_WINNER: bool = False
4848
MAIN_DIAGONAL_IS_WINNER: bool = False
49-
CURENT_BOARD_WON: bool = False
5049
CONTINUE_WITH_NEXT_GAME: str = ''
5150
STEP_COUNTER: int = 0
5251
PLAYER_SWITCH = True
@@ -56,7 +55,7 @@
5655
PLAYER2_NAME: str = ''
5756
PLAYER2_MARKER: str = ''
5857

59-
ROWS, COLS = (3, 3)
58+
ROWS, COLS = (3, 3)
6059
GAME_PROGRESS_ARRAY = [['' for i in range(COLS)] for j in range(ROWS)]
6160
GAME_PROGRESS_ARRAY = np.array(GAME_PROGRESS_ARRAY, dtype=str)
6261
# WINNING_PATTERNS: list = [['00', '10', '20'], ['01', '11', '21'], ['02', '12', '22'], # rows
@@ -69,28 +68,46 @@ def split(word):
6968

7069
def progress_game(key: str, player_marker: str):
7170
'''populated the 'GAME_PROGRESS_ARRAY' and
72-
checks for is winning condition.'''
71+
checks for is winning condition.
72+
73+
PARAMS:
74+
1. key - the button element in the grid that was clicked.
75+
76+
2. player_marker - the marker of the current player.
77+
78+
RETURNS:
79+
continue_with_next_game - 'Yes' / 'No' indicator from the user action
80+
whether to continue with the same players.
81+
'''
7382

7483
global GAME_PROGRESS_ARRAY
75-
global CURENT_BOARD_WON
7684

7785
continue_with_next_game: str = ''
7886

7987
row, column = split(key)
8088
GAME_PROGRESS_ARRAY[row][column] = player_marker
8189

90+
# check if we have a winner in the current state of the board.
8291
if CHECK_FOR_WINNER:
8392
game_won, winning_marker = is_winning()
8493
if game_won:
85-
CURENT_BOARD_WON = True
8694
continue_with_next_game = display_winner_and_continue(winning_marker=winning_marker)
8795

8896
return continue_with_next_game
8997

9098
def is_row_column_diagonal_complete(row_col_num: int = -1, is_row: bool = True,
9199
is_diagonal: bool = False):
92100
'''checks if the given row or column is complete
93-
to proceed with a winner.'''
101+
to proceed with a winner.
102+
103+
PARAMS:
104+
1. row_col_num - the row/column index to check if the row/columns is complete.
105+
2. is_row: True if a ROW needs to be checked for completion else FALSE for a column.
106+
3. is_diagonal: True is any of the diagonal needs to be checked for completion.
107+
108+
RETURNS:
109+
is_complete: BOOLEAN FLAG to indicate of the row/column/diagonal is complete.
110+
'''
94111
is_complete: bool = False
95112

96113
if is_diagonal is False and row_col_num != -1:
@@ -117,10 +134,22 @@ def is_row_column_diagonal_complete(row_col_num: int = -1, is_row: bool = True,
117134

118135
return is_complete
119136

120-
121-
def mark_the_winner(row_is_winner: bool, row_column_index: int = -1, diagonal_is_winner: bool = False):
137+
def mark_the_winner(row_is_winner: bool, row_column_index: int = -1,
138+
diagonal_is_winner: bool = False):
122139
'''marks the winner row/column by updating
123-
the button row/column.'''
140+
the button row/column. The button image background
141+
changes to red to mark the winning sequence.
142+
143+
PARAMS:
144+
1. row_is_winner - Is the winner found in a ROW wise sequence.
145+
TRUE for a row & FALSE incase the winner is a column.
146+
147+
2. row_column_index - The winning row/column index.
148+
This default value is -1 to indicate the winner being found
149+
in one of the diagonals.
150+
151+
3. diagonal_is_winner - Is the winner found in one of the diagonals.
152+
'''
124153

125154
if not diagonal_is_winner and row_column_index != -1:
126155
if row_is_winner:
@@ -203,7 +232,11 @@ def is_winning():
203232
return False, ''
204233

205234
def display_winner_and_continue(winning_marker: str):
206-
'''display the winner of the current board.'''
235+
'''display the winner of the current board.
236+
237+
PARAMS:
238+
1. winning_marker - the marker that won the current board.
239+
'''
207240

208241
if winning_marker == PLAYER1_MARKER:
209242
popup_result = sg.PopupYesNo('The Winner is ' + PLAYER1_NAME + '.\nDo you want to play another game with the current players?',
@@ -218,6 +251,7 @@ def display_winner_and_continue(winning_marker: str):
218251

219252
def init_game_window():
220253
'''Initializes and creates the game options window.'''
254+
221255
init_game_layout = [[sg.Text('Player 1 Name: ', size=(12, 1)),
222256
sg.InputText('', key='-P1_NAME-')],
223257
[sg.Text('Player 2 Name: ', size=(12, 1)),
@@ -239,18 +273,36 @@ def reset_game_board():
239273
global STEP_COUNTER
240274
global CONTINUE_WITH_NEXT_GAME
241275
global CHECK_FOR_WINNER
242-
global CURENT_BOARD_WON
243276
global GAME_BOARD
244277
global PLAYER_SWITCH
278+
global MAIN_DIAGONAL_IS_WINNER
245279

246280
GAME_BOARD = initialize_game_board()
247281
GAME_PROGRESS_ARRAY = [['' for i in range(COLS)] for j in range(ROWS)]
248282
GAME_PROGRESS_ARRAY = np.array(GAME_PROGRESS_ARRAY, dtype=str)
249283
STEP_COUNTER = 0
250284
CHECK_FOR_WINNER = False
251285
CONTINUE_WITH_NEXT_GAME = ''
252-
CURENT_BOARD_WON = False
253286
PLAYER_SWITCH = True
287+
MAIN_DIAGONAL_IS_WINNER = False
288+
289+
def start_next_session(user_choice: str):
290+
291+
'''starts the next session as per the user choice.
292+
YES - retain the players and reset the board state.
293+
NO - return to the game init dialog to start over with new set of players.'''
294+
295+
global INIT_WINDOW
296+
global GAME_BOARD
297+
298+
if user_choice == 'Yes':
299+
# retain the players and reset the board state.
300+
GAME_BOARD.Close()
301+
reset_game_board()
302+
elif user_choice == 'No':
303+
# return to the game init dialog to start over with new set of players.
304+
GAME_BOARD.Close()
305+
INIT_WINDOW = init_game_window()
254306

255307
def initialize_game_board():
256308
'''initialize the game board.'''
@@ -291,6 +343,7 @@ def initialize_game_board():
291343

292344
if EVENT == '-START-' and not GAME_BOARD:
293345

346+
# player name validation. Valid names required.
294347
if VALUES['-P1_NAME-'] == '' and VALUES['-P2_NAME-'] == '':
295348
sg.popup_ok("Error initializing players name. Enter both the players name before proceeding.",
296349
title='Tic Tac Toe', icon=GAME_ICON)
@@ -313,14 +366,16 @@ def initialize_game_board():
313366
INIT_WINDOW.close()
314367
GAME_BOARD = initialize_game_board()
315368

316-
if WINDOW == GAME_BOARD and (EVENT in ('WIN_CLOSE', 'Exit')):
369+
if WINDOW == GAME_BOARD and (EVENT in ('WIN_CLOSE', '-EXIT-')):
317370
GAME_BOARD.close()
318371
GAME_BOARD = None
319372
INIT_WINDOW = init_game_window()
320373

321-
if START_GAME:
374+
# We do not want to execute the progress logic in case
375+
# of the reset event.
376+
if START_GAME and EVENT != '-RESET-':
322377

323-
if EVENT not in ('-START-', 'WIN_CLOSE'):
378+
if EVENT not in ('-START-', 'WIN_CLOSE', '-EXIT-'):
324379
CURRENT_MARKER = GAME_BOARD.Element(EVENT).get_text()
325380
GAME_BOARD.Element(EVENT).update(PLAYER1_MARKER if CURRENT_MARKER == ' ' and\
326381
PLAYER_SWITCH is True else PLAYER2_MARKER if CURRENT_MARKER == ' ' and\
@@ -341,6 +396,7 @@ def initialize_game_board():
341396
GAME_BOARD.Element('-P1-').update(text_color='white')
342397
GAME_BOARD.Element('-P2-').update(text_color='darkblue')
343398

399+
# update the button images as per the current players marker.
344400
if PLAYER1_MARKER == 'X':
345401
GAME_BOARD.Element(EVENT).update(image_filename=X_IMAGE)
346402
else:
@@ -349,12 +405,10 @@ def initialize_game_board():
349405
GAME_BOARD.Element(EVENT).update(disabled=True)
350406

351407
CONTINUE_WITH_NEXT_GAME = progress_game(EVENT, PLAYER1_MARKER)
352-
if CONTINUE_WITH_NEXT_GAME == 'Yes':
353-
GAME_BOARD.Close()
354-
reset_game_board()
355-
elif CONTINUE_WITH_NEXT_GAME == 'No':
356-
GAME_BOARD.Close()
357-
INIT_WINDOW = init_game_window()
408+
409+
# start with the same players or new game
410+
# session with different players
411+
start_next_session(CONTINUE_WITH_NEXT_GAME)
358412

359413
elif GAME_BOARD.Element(EVENT).get_text() == PLAYER2_MARKER:
360414
# increase the step counter.
@@ -365,6 +419,7 @@ def initialize_game_board():
365419
GAME_BOARD.Element('-P1-').update(text_color='darkblue')
366420
GAME_BOARD.Element('-P2-').update(text_color='white')
367421

422+
# update the button images as per the current players marker.
368423
if PLAYER2_MARKER == 'X':
369424
GAME_BOARD.Element(EVENT).update(image_filename=X_IMAGE)
370425
else:
@@ -373,18 +428,20 @@ def initialize_game_board():
373428
GAME_BOARD.Element(EVENT).update(disabled=True)
374429

375430
CONTINUE_WITH_NEXT_GAME = progress_game(EVENT, PLAYER2_MARKER)
376-
if CONTINUE_WITH_NEXT_GAME == 'Yes':
377-
GAME_BOARD.Close()
378-
reset_game_board()
379-
elif CONTINUE_WITH_NEXT_GAME == 'No':
380-
GAME_BOARD.Close()
381-
INIT_WINDOW = init_game_window()
431+
432+
# start with the same players or new game
433+
# session with different players
434+
start_next_session(CONTINUE_WITH_NEXT_GAME)
382435

383436
# The minimum number of steps required
384437
# to win the game board is 5.
385438
if STEP_COUNTER == 4:
386439
CHECK_FOR_WINNER = True
387440

388-
if EVENT == '-RESET-':
389-
GAME_BOARD.Close()
390-
reset_game_board()
441+
if EVENT == '-RESET-' and WINDOW == GAME_BOARD:
442+
# reset the current board state.
443+
RESET_GAME = sg.popup_yes_no('Are you sure you want to reset the current board?',
444+
title='Game Reset', icon=GAME_ICON, grab_anywhere=True)
445+
if RESET_GAME == 'Yes':
446+
GAME_BOARD.Close()
447+
reset_game_board()

0 commit comments

Comments
 (0)