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 ab7c5e6

Browse files
Update tic_tac_toe_v1.py
Minor modification to the if conditions.
1 parent 35c05d9 commit ab7c5e6

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
2525
3. Exiting the INIT_WINDOW would exit the game.
2626
27+
4. RESET would reset thecurrent game session and
28+
start over a fresh board.
29+
2730
"""
2831

2932
import os
@@ -84,36 +87,35 @@ def progress_game(key: str, player_marker: str):
8487

8588
return continue_with_next_game
8689

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):
8892
'''checks if the given row or column is complete
8993
to proceed with a winner.'''
94+
is_complete: bool = False
9095

9196
if is_diagonal is False and row_col_num != -1:
9297
if is_row:
9398
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] != ''
100102
else:
101103
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] != ''
108107
else:
109108
if GAME_PROGRESS_ARRAY[0][0] != '' and \
110109
GAME_PROGRESS_ARRAY[1][1] != '' and \
111110
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 \
114114
GAME_PROGRESS_ARRAY[1][1] != '' and \
115115
GAME_PROGRESS_ARRAY[0][2] != '':
116-
return True
116+
is_complete = True
117+
118+
return is_complete
117119

118120

119121
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():
174176
if is_row_column_diagonal_complete(row_col_num=row, is_row=True):
175177
if GAME_PROGRESS_ARRAY[row][0] == GAME_PROGRESS_ARRAY[row][1] == GAME_PROGRESS_ARRAY[row][2]:
176178
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])
178179
CHECK_FOR_WINNER = False
179180
return True, GAME_PROGRESS_ARRAY[row][0]
180181

@@ -183,41 +184,38 @@ def is_winning():
183184
if is_row_column_diagonal_complete(row_col_num=col, is_row=False):
184185
if GAME_PROGRESS_ARRAY[0][col] == GAME_PROGRESS_ARRAY[1][col] == GAME_PROGRESS_ARRAY[2][col]:
185186
mark_the_winner(row_is_winner=False, row_column_index=col)
186-
# display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[0][col])
187187
CHECK_FOR_WINNER = False
188188
return True, GAME_PROGRESS_ARRAY[0][col]
189-
189+
190190
# check for the 2 diagonals for a winning sequence.
191191
if is_row_column_diagonal_complete(is_diagonal=True):
192192
if GAME_PROGRESS_ARRAY[0][0] == GAME_PROGRESS_ARRAY[1][1] == GAME_PROGRESS_ARRAY[2][2]:
193193
MAIN_DIAGONAL_IS_WINNER = True
194194
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])
196195
CHECK_FOR_WINNER = False
197196
return True, GAME_PROGRESS_ARRAY[1][1]
198197

199198
elif GAME_PROGRESS_ARRAY[2][0] == GAME_PROGRESS_ARRAY[1][1] == GAME_PROGRESS_ARRAY[0][2]:
200199
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])
202200
CHECK_FOR_WINNER = False
203201
return True, GAME_PROGRESS_ARRAY[1][1]
204-
202+
205203
return False, ''
206204

207205
def display_winner_and_continue(winning_marker: str):
208206
'''display the winner of the current board.'''
209207

210208
if winning_marker == PLAYER1_MARKER:
211209
popup_result = sg.PopupYesNo('The Winner is ' + PLAYER1_NAME + '.\nDo 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))
214212
elif winning_marker == PLAYER2_MARKER:
215213
popup_result = sg.PopupYesNo('The Winner is ' + PLAYER2_NAME + '.\nDo 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))
218216

219217
return popup_result
220-
218+
221219
def init_game_window():
222220
'''Initializes and creates the game options window.'''
223221
init_game_layout = [[sg.Text('Player 1 Name: ', size=(12, 1)),
@@ -271,6 +269,8 @@ def initialize_game_board():
271269
GAME_BOARD_LAYOUT += [[sg.Button(' ', size=(8, 4), key=str(j)+str(i))
272270
for i in range(3)] for j in range(3)]
273271

272+
GAME_BOARD_LAYOUT += [[sg.Button("Reset Game", key="-RESET-", tooltip='Resets the current session.')]]
273+
274274
BOARD = sg.Window('Tic Tac Toe', GAME_BOARD_LAYOUT, icon=GAME_ICON, finalize=True)
275275

276276
BOARD.TKroot.protocol("WM_DESTROY_WINDOW", lambda: BOARD.write_event_value("WIN_CLOSE", ()))
@@ -384,3 +384,7 @@ def initialize_game_board():
384384
# to win the game board is 5.
385385
if STEP_COUNTER == 4:
386386
CHECK_FOR_WINNER = True
387+
388+
if EVENT == '-RESET-':
389+
GAME_BOARD.Close()
390+
reset_game_board()

0 commit comments

Comments
(0)

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