File tree

1 file changed

+111
-43
lines changed

1 file changed

+111
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import numpy as np
55
import PySimpleGUI as sg
66

7+
INIT_WINDOW = None
8+
79
CURRENT_WORKING_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
810
X_IMAGE = CURRENT_WORKING_DIRECTORY + '\\X.png'
911
X_RED = CURRENT_WORKING_DIRECTORY + '\\X_Red.png'
@@ -13,6 +15,8 @@
1315

1416
START_GAME: bool = False
1517
CHECK_FOR_WINNER: bool = False
18+
MAIN_DIAGONAL_IS_WINNER: bool = False
19+
CURENT_BOARD_WON: bool = False
1620

1721
ROWS, COLS = (3, 3)
1822
GAME_PROGRESS_ARRAY = [['' for i in range(COLS)] for j in range(ROWS)]
@@ -30,18 +34,20 @@ def progress_game(key: str, player_marker: str):
3034
checks for is winning condition.'''
3135

3236
global GAME_PROGRESS_ARRAY
37+
global CURENT_BOARD_WON
3338

3439
row, column = split(key)
3540
GAME_PROGRESS_ARRAY[row][column] = player_marker
3641

3742
if CHECK_FOR_WINNER:
38-
is_winning()
43+
if is_winning():
44+
CURENT_BOARD_WON = True
3945

4046
def is_row_column_diagonal_complete(row_col_num: int = -1, is_row: bool = True, is_diagonal: bool = False):
4147
'''checks if the given row or column is complete
4248
to proceed with a winner.'''
4349

44-
if is_diagonal == False and row_col_num != -1:
50+
if is_diagonal is False and row_col_num != -1:
4551
if is_row:
4652
row = row_col_num
4753
if GAME_PROGRESS_ARRAY[row][0] != '' and \
@@ -69,70 +75,111 @@ def is_row_column_diagonal_complete(row_col_num: int = -1, is_row: bool = True,
6975
return True
7076

7177

72-
def mark_the_winner(row_column_index: int, row_as_winner: bool):
73-
'''marks the winner row/column by updating
78+
def mark_the_winner(row_is_winner: bool, row_column_index: int = -1, diagonal_is_winner: bool = False):
79+
'''marks the winner row/column by updating
7480
the button row/column.'''
7581

76-
if row_as_winner:
77-
row = row_column_index
78-
if GAME_PROGRESS_ARRAY[row][0] == 'X':
79-
GAME_BOARD[str(row)+str(0)].update(image_filename=X_RED)
80-
GAME_BOARD[str(row)+str(1)].update(image_filename=X_RED)
81-
GAME_BOARD[str(row)+str(2)].update(image_filename=X_RED)
82+
if not diagonal_is_winner and row_column_index != -1:
83+
if row_is_winner:
84+
row = row_column_index
85+
if GAME_PROGRESS_ARRAY[row][0] == 'X':
86+
GAME_BOARD[str(row)+str(0)].update(image_filename=X_RED)
87+
GAME_BOARD[str(row)+str(1)].update(image_filename=X_RED)
88+
GAME_BOARD[str(row)+str(2)].update(image_filename=X_RED)
89+
else:
90+
GAME_BOARD[str(row)+str(0)].update(image_filename=O_RED)
91+
GAME_BOARD[str(row)+str(1)].update(image_filename=O_RED)
92+
GAME_BOARD[str(row)+str(2)].update(image_filename=O_RED)
8293
else:
83-
GAME_BOARD[str(row)+str(0)].update(image_filename=O_RED)
84-
GAME_BOARD[str(row)+str(1)].update(image_filename=O_RED)
85-
GAME_BOARD[str(row)+str(2)].update(image_filename=O_RED)
94+
col = row_column_index
95+
if GAME_PROGRESS_ARRAY[0][col] == 'X':
96+
GAME_BOARD[str(0)+str(col)].update(image_filename=X_RED)
97+
GAME_BOARD[str(1)+str(col)].update(image_filename=X_RED)
98+
GAME_BOARD[str(2)+str(col)].update(image_filename=X_RED)
99+
else:
100+
GAME_BOARD[str(0)+str(col)].update(image_filename=O_RED)
101+
GAME_BOARD[str(1)+str(col)].update(image_filename=O_RED)
102+
GAME_BOARD[str(2)+str(col)].update(image_filename=O_RED)
86103
else:
87-
col = row_column_index
88-
if GAME_PROGRESS_ARRAY[0][col] == 'X':
89-
GAME_BOARD[str(0)+str(col)].update(image_filename=X_RED)
90-
GAME_BOARD[str(1)+str(col)].update(image_filename=X_RED)
91-
GAME_BOARD[str(2)+str(col)].update(image_filename=X_RED)
104+
if MAIN_DIAGONAL_IS_WINNER:
105+
if GAME_PROGRESS_ARRAY[1][1] == 'X':
106+
GAME_BOARD[str(0)+str(0)].update(image_filename=X_RED)
107+
GAME_BOARD[str(1)+str(1)].update(image_filename=X_RED)
108+
GAME_BOARD[str(2)+str(2)].update(image_filename=X_RED)
109+
else:
110+
GAME_BOARD[str(0)+str(0)].update(image_filename=O_RED)
111+
GAME_BOARD[str(1)+str(1)].update(image_filename=O_RED)
112+
GAME_BOARD[str(2)+str(2)].update(image_filename=O_RED)
92113
else:
93-
GAME_BOARD[str(0)+str(col)].update(image_filename=O_RED)
94-
GAME_BOARD[str(1)+str(col)].update(image_filename=O_RED)
95-
GAME_BOARD[str(2)+str(col)].update(image_filename=O_RED)
114+
if GAME_PROGRESS_ARRAY[1][1] == 'X':
115+
GAME_BOARD[str(0)+str(2)].update(image_filename=X_RED)
116+
GAME_BOARD[str(1)+str(1)].update(image_filename=X_RED)
117+
GAME_BOARD[str(2)+str(0)].update(image_filename=X_RED)
118+
else:
119+
GAME_BOARD[str(0)+str(2)].update(image_filename=O_RED)
120+
GAME_BOARD[str(1)+str(1)].update(image_filename=O_RED)
121+
GAME_BOARD[str(2)+str(0)].update(image_filename=O_RED)
96122

97123
def is_winning():
98124
'''evaluated the current state of the gameboard
99125
and checks if there is a winner currently.'''
100126

101127
global GAME_PROGRESS_ARRAY
102128
global CHECK_FOR_WINNER
129+
global MAIN_DIAGONAL_IS_WINNER
103130

104131
# check for the row wise sequence.
105132
for row in range(ROWS):
106133
if is_row_column_diagonal_complete(row_col_num=row, is_row=True):
107134
if GAME_PROGRESS_ARRAY[row][0] == GAME_PROGRESS_ARRAY[row][1] == GAME_PROGRESS_ARRAY[row][2]:
108-
mark_the_winner(row_column_index=row, row_as_winner=True)
109-
sg.popup('winnner', grab_anywhere=True)
135+
mark_the_winner(row_is_winner=True, row_column_index=row)
136+
display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[row][0])
110137
CHECK_FOR_WINNER = False
111138
return True
112139

113140
# check for the column wise sequence.
114141
for col in range(COLS):
115142
if is_row_column_diagonal_complete(row_col_num=col, is_row=False):
116143
if GAME_PROGRESS_ARRAY[0][col] == GAME_PROGRESS_ARRAY[1][col] == GAME_PROGRESS_ARRAY[2][col]:
117-
mark_the_winner(row_column_index=col, row_as_winner=False)
118-
sg.popup('winnner')
144+
mark_the_winner(row_is_winner=False, row_column_index=col)
145+
display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[0][col])
119146
CHECK_FOR_WINNER = False
120147
return True
121148

122149
# check for the 2 diagonals for a winning sequence.
123150
if is_row_column_diagonal_complete(is_diagonal=True):
124151
if GAME_PROGRESS_ARRAY[0][0] == GAME_PROGRESS_ARRAY[1][1] == GAME_PROGRESS_ARRAY[2][2]:
125-
# mark_the_winner(row_column_index=col, row_as_winner=False)
126-
sg.popup('winnner')
127-
CHECK_FOR_WINNER = False
128-
return True
152+
MAIN_DIAGONAL_IS_WINNER = True
153+
mark_the_winner(row_column_index=-1, row_is_winner=False, diagonal_is_winner=True)
154+
display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[1][1])
155+
CHECK_FOR_WINNER = False
156+
return True
129157
elif GAME_PROGRESS_ARRAY[2][0] == GAME_PROGRESS_ARRAY[1][1] == GAME_PROGRESS_ARRAY[0][2]:
130-
# mark_the_winner(row_column_index=col, row_as_winner=False)
131-
sg.popup('winnner')
132-
CHECK_FOR_WINNER = False
133-
return True
158+
mark_the_winner(row_column_index=-1, row_is_winner=False, diagonal_is_winner=True)
159+
display_winner_and_continue(winning_marker=GAME_PROGRESS_ARRAY[1][1])
160+
CHECK_FOR_WINNER = False
161+
return True
134162

163+
def display_winner_and_continue(winning_marker: str):
164+
'''display the winner of the current board.'''
135165

166+
global INIT_WINDOW
167+
168+
if winning_marker == PLAYER1_MARKER:
169+
continue_with_same_player = sg.PopupYesNo('The Winner is ' + PLAYER1_NAME + '.\nDo you want to play another game with the current players?',
170+
title='Board Winner!', text_color='darkblue', icon=GAME_ICON,
171+
grab_anywhere=True, font=('Blackadder ITC', 20))
172+
elif winning_marker == PLAYER2_MARKER:
173+
continue_with_same_player = sg.PopupYesNo('The Winner is ' + PLAYER2_NAME + '.\nDo you want to play another game with the current players?',
174+
title='Board Winner!', text_color='darkblue', icon=GAME_ICON,
175+
grab_anywhere=True, font=('Blackadder ITC', 20))
176+
177+
if continue_with_same_player == 'Yes':
178+
GAME_BOARD.close()
179+
initialize_game_board()
180+
elif continue_with_same_player == 'No' and not INIT_WINDOW:
181+
GAME_BOARD.hide()
182+
INIT_WINDOW = init_game_window()
136183

137184

138185
def init_game_window():
@@ -182,23 +229,31 @@ def init_game_window():
182229
INIT_WINDOW.close()
183230

184231
INIT_WINDOW.close()
232+
INIT_WINDOW = None
185233

186234
STEP_COUNTER: int = 0
187235
PLAYER_SWITCH = True
188236
PLAYER1_MARKED_CELLS: list = []
189237
PLAYER2_MARKED_CELLS: list = []
190238
if START_GAME:
191239

192-
GAME_BOARD_LAYOUT = [[sg.Text('Player 1: ' + PLAYER1_NAME, key='-P1-', text_color='darkblue')],
193-
[sg.Text('Player 2: ' + PLAYER2_NAME, key='-P2-', text_color='white')],
194-
[sg.Text(PLAYER1_NAME + "'s Marker: " + PLAYER1_MARKER)],
195-
[sg.Text(PLAYER2_NAME + "'s Marker: " + PLAYER2_MARKER)],
196-
[sg.Text('')]]
240+
def initialize_game_board():
241+
'''initialize the game board.'''
242+
243+
GAME_BOARD_LAYOUT = [[sg.Text('Player 1: ' + PLAYER1_NAME, key='-P1-', text_color='darkblue')],
244+
[sg.Text('Player 2: ' + PLAYER2_NAME, key='-P2-', text_color='white')],
245+
[sg.Text(PLAYER1_NAME + "'s Marker: " + PLAYER1_MARKER)],
246+
[sg.Text(PLAYER2_NAME + "'s Marker: " + PLAYER2_MARKER)],
247+
[sg.Text('')]]
197248

198-
GAME_BOARD_LAYOUT += [[sg.Button(' ', size=(8, 4), key=str(j)+str(i))
199-
for i in range(3)] for j in range(3)]
249+
GAME_BOARD_LAYOUT += [[sg.Button(' ', size=(8, 4), key=str(j)+str(i))
250+
for i in range(3)] for j in range(3)]
200251

201-
GAME_BOARD = sg.Window('Tic Tac Toe', icon=GAME_ICON).Layout(GAME_BOARD_LAYOUT)
252+
BOARD = sg.Window('Tic Tac Toe', icon=GAME_ICON).Layout(GAME_BOARD_LAYOUT)
253+
254+
return BOARD
255+
256+
GAME_BOARD = initialize_game_board()
202257

203258
while True:
204259

@@ -228,10 +283,17 @@ def init_game_window():
228283
GAME_BOARD['-P1-'].update(text_color='white')
229284
GAME_BOARD['-P2-'].update(text_color='darkblue')
230285

231-
GAME_BOARD[GAME_EVENT].update(image_filename=X_IMAGE)
286+
if PLAYER1_MARKER == 'X':
287+
GAME_BOARD[GAME_EVENT].update(image_filename=X_IMAGE)
288+
else:
289+
GAME_BOARD[GAME_EVENT].update(image_filename=O_IMAGE)
290+
232291
GAME_BOARD[GAME_EVENT].update(disabled=True)
233292

234293
progress_game(GAME_EVENT, PLAYER1_MARKER)
294+
295+
if CURENT_BOARD_WON:
296+
break
235297
elif GAME_BOARD[GAME_EVENT].get_text() == PLAYER2_MARKER:
236298
# increase the step counter.
237299
# The minimum number of steps required to win the game is 5
@@ -242,11 +304,17 @@ def init_game_window():
242304
GAME_BOARD['-P1-'].update(text_color='darkblue')
243305
GAME_BOARD['-P2-'].update(text_color='white')
244306

245-
GAME_BOARD[GAME_EVENT].update(image_filename=O_IMAGE)
307+
if PLAYER2_MARKER == 'X':
308+
GAME_BOARD[GAME_EVENT].update(image_filename=X_IMAGE)
309+
else:
310+
GAME_BOARD[GAME_EVENT].update(image_filename=O_IMAGE)
311+
246312
GAME_BOARD[GAME_EVENT].update(disabled=True)
247313

248314
progress_game(GAME_EVENT, PLAYER2_MARKER)
249315

316+
if CURENT_BOARD_WON:
317+
break
250318
# The minimum number of steps required
251319
# to win the game board is 5.
252320
if STEP_COUNTER == 4:

0 commit comments

Comments
 (0)