Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da82dc4

Browse files
Update tic_tac_toe_v1.py
1. Game Drawn Scenario added. 2. Minor exception and bug fixes.
1 parent 674827b commit da82dc4

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

‎Sample GUI Implementation/tic-tac-toe/tic_tac_toe_v1.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ def progress_game(key: str, player_marker: str):
9292
game_won, winning_marker = is_winning()
9393
if game_won:
9494
continue_with_next_game = display_winner_and_continue(winning_marker=winning_marker)
95+
else:
96+
# GAME DRAWN - GAME_PROGRESS_ARRAY is full and we do not have a winner.
97+
if np.all((GAME_PROGRESS_ARRAY != '')):
98+
continue_with_next_game = display_winner_and_continue(winning_marker='')
9599

96100
return continue_with_next_game
97101

@@ -240,12 +244,13 @@ def display_winner_and_continue(winning_marker: str):
240244

241245
if winning_marker == PLAYER1_MARKER:
242246
popup_result = sg.PopupYesNo('The Winner is ' + PLAYER1_NAME + '.\nDo you want to play another game with the current players?',
243-
title='Board Winner!', text_color='darkblue', icon=GAME_ICON,
244-
grab_anywhere=True, font=('Blackadder ITC', 20))
247+
title='Board Winner!', text_color='darkblue', icon=GAME_ICON, grab_anywhere=True)
245248
elif winning_marker == PLAYER2_MARKER:
246249
popup_result = sg.PopupYesNo('The Winner is ' + PLAYER2_NAME + '.\nDo you want to play another game with the current players?',
247-
title='Board Winner!', text_color='darkblue', icon=GAME_ICON,
248-
grab_anywhere=True, font=('Blackadder ITC', 20))
250+
title='Board Winner!', text_color='darkblue', icon=GAME_ICON, grab_anywhere=True)
251+
else: # game drawn
252+
popup_result = sg.PopupYesNo('The Game is DRAWN.\nDo you want to play another game with the current players?',
253+
title='Board Drawn!', text_color='darkblue', icon=GAME_ICON, grab_anywhere=True)
249254

250255
return popup_result
251256

@@ -265,7 +270,7 @@ def init_game_window():
265270

266271
return sg.Window('Tic Tac Toe Options', init_game_layout, icon=GAME_ICON, finalize=True)
267272

268-
def reset_game_board():
273+
def reset_game_board(reset_board: str):
269274
'''Resets the current game board and re-initializes all the
270275
game parameters to continue playing the game with the same players.'''
271276

@@ -277,7 +282,9 @@ def reset_game_board():
277282
global PLAYER_SWITCH
278283
global MAIN_DIAGONAL_IS_WINNER
279284

280-
GAME_BOARD = initialize_game_board()
285+
if reset_board == 'Yes':
286+
GAME_BOARD = initialize_game_board()
287+
281288
GAME_PROGRESS_ARRAY = [['' for i in range(COLS)] for j in range(ROWS)]
282289
GAME_PROGRESS_ARRAY = np.array(GAME_PROGRESS_ARRAY, dtype=str)
283290
STEP_COUNTER = 0
@@ -294,14 +301,17 @@ def start_next_session(user_choice: str):
294301

295302
global INIT_WINDOW
296303
global GAME_BOARD
297-
304+
298305
if user_choice == 'Yes':
299306
# retain the players and reset the board state.
300307
GAME_BOARD.Close()
301-
reset_game_board()
308+
GAME_BOARD = None
309+
reset_game_board(reset_board='Yes')
302310
elif user_choice == 'No':
303311
# return to the game init dialog to start over with new set of players.
304312
GAME_BOARD.Close()
313+
GAME_BOARD = None
314+
reset_game_board(reset_board='No')
305315
INIT_WINDOW = init_game_window()
306316

307317
def initialize_game_board():
@@ -376,6 +386,7 @@ def initialize_game_board():
376386
if START_GAME and EVENT != '-RESET-':
377387

378388
if EVENT not in ('-START-', 'WIN_CLOSE', '-EXIT-'):
389+
379390
CURRENT_MARKER = GAME_BOARD.Element(EVENT).get_text()
380391
GAME_BOARD.Element(EVENT).update(PLAYER1_MARKER if CURRENT_MARKER == ' ' and\
381392
PLAYER_SWITCH is True else PLAYER2_MARKER if CURRENT_MARKER == ' ' and\
@@ -444,4 +455,5 @@ def initialize_game_board():
444455
title='Game Reset', icon=GAME_ICON, grab_anywhere=True)
445456
if RESET_GAME == 'Yes':
446457
GAME_BOARD.Close()
447-
reset_game_board()
458+
GAME_BOARD = None
459+
reset_game_board(reset_board='Yes')

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /