I am writing the code for the game Tic Tac Toe using Python and the tkinter library. Code length: 240 lines, but the problem is that there is a function that checks who won..There's a lot of if and elif statement. And I would like to cut it. At the end of each victory, there is a function (turn off the buttons) and a function (restart the game). It is necessary to reduce the part with if and elif.
def victory_check():
global winner
winner = False
# Is the user with X the winner?
if button_1["text"] == "X" and button_2["text"] == "X" and button_3["text"] == "X":
button_1.config(bg='#EE2923')
button_2.config(bg='#EE2923')
button_3.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for X - won!')
buttons_off()
restart()
elif button_4["text"] == "X" and button_5["text"] == "X" and button_6["text"] == "X":
button_4.config(bg='#EE2923')
button_5.config(bg='#EE2923')
button_6.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for X - won!')
buttons_off()
restart()
elif button_7["text"] == "X" and button_8["text"] == "X" and button_9["text"] == "X":
button_7.config(bg='#EE2923')
button_8.config(bg='#EE2923')
button_9.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for X - won!')
buttons_off()
restart()
elif button_1["text"] == "X" and button_4["text"] == "X" and button_7["text"] == "X":
button_1.config(bg='#EE2923')
button_4.config(bg='#EE2923')
button_7.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for X - won!')
buttons_off()
restart()
elif button_2["text"] == "X" and button_5["text"] == "X" and button_8["text"] == "X":
button_2.config(bg='#EE2923')
button_5.config(bg='#EE2923')
button_8.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for X - won!')
buttons_off()
restart()
elif button_3["text"] == "X" and button_6["text"] == "X" and button_9["text"] == "X":
button_1.config(bg='#EE2923')
button_4.config(bg='#EE2923')
button_7.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for X - won!')
buttons_off()
restart()
elif button_1["text"] == "X" and button_5["text"] == "X" and button_9["text"] == "X":
button_1.config(bg='#EE2923')
button_5.config(bg='#EE2923')
button_9.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for X - won!')
buttons_off()
restart()
elif button_3["text"] == "X" and button_5["text"] == "X" and button_7["text"] == "X":
button_3.config(bg='#EE2923')
button_5.config(bg='#EE2923')
button_7.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for X - won!')
buttons_off()
restart()
# Is the user with O the winner?
elif button_1["text"] == "O" and button_2["text"] == "O" and button_3["text"] == "O":
button_1.config(bg='#EE2923')
button_2.config(bg='#EE2923')
button_3.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for O - won!')
buttons_off()
restart()
elif button_4["text"] == "O" and button_5["text"] == "O" and button_6["text"] == "O":
button_4.config(bg='#EE2923')
button_5.config(bg='#EE2923')
button_6.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for O - won!')
buttons_off()
restart()
elif button_7["text"] == "O" and button_8["text"] == "O" and button_9["text"] == "O":
button_7.config(bg='#EE2923')
button_8.config(bg='#EE2923')
button_9.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for O - won!')
buttons_off()
restart()
elif button_1["text"] == "O" and button_4["text"] == "O" and button_7["text"] == "O":
button_1.config(bg='#EE2923')
button_4.config(bg='#EE2923')
button_7.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for O - won!')
buttons_off()
restart()
elif button_2["text"] == "O" and button_5["text"] == "O" and button_8["text"] == "O":
button_2.config(bg='#EE2923')
button_5.config(bg='#EE2923')
button_8.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for O - won!')
buttons_off()
restart()
elif button_3["text"] == "O" and button_6["text"] == "O" and button_9["text"] == "O":
button_1.config(bg='#EE2923')
button_4.config(bg='#EE2923')
button_7.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for O - won!')
buttons_off()
restart()
elif button_1["text"] == "O" and button_5["text"] == "O" and button_9["text"] == "O":
button_1.config(bg='#EE2923')
button_5.config(bg='#EE2923')
button_9.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for O - won!')
buttons_off()
restart()
elif button_3["text"] == "O" and button_5["text"] == "O" and button_7["text"] == "O":
button_3.config(bg='#EE2923')
button_5.config(bg='#EE2923')
button_7.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for O - won!')
buttons_off()
restart()
if count == 9 and winner == False:
tk.messagebox.showinfo('Tic Tac Toe', 'There is no winner - a tie')
restart()
CodePudding user response:
This is to half the amount. First you'll check the condition for player X then for O.
for player in ['X','O']:
if button_1["text"] == player and button_2["text"] == player and button_3["text"] == player :
button_1.config(bg='#EE2923')
button_2.config(bg='#EE2923')
button_3.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for ' player ' - won!')
buttons_off()
restart()
You can also put the different winning combination-checks in the for-loop. And then combine with or-keywords.
for player in ['X','O']:
condition1 = button_1["text"] == player and button_2["text"] == player and button_3["text"] == player
condition2 = ...
if condition1 or condition2 or condition3:
button_1.config(bg='#EE2923')
button_2.config(bg='#EE2923')
button_3.config(bg='#EE2923')
winner = True
tk.messagebox.showinfo('Tic Tac Toe', 'User playing for ' player ' - won!')
buttons_off()
restart()
CodePudding user response:
Use smart data instead of code.
Put your buttons in a list
or tuple
.
Then you'd have buttons[0]
up to and including buttons[8]
.
Next you should have a nested tuple of winning combinations.
winners = (
(0,1,2),
(3,4,5),
(6,7,8),
...
)
And you could replaces all of your code with a nested loop:
for comb in winners:
for player in ('X', 'O'):
if all(buttons[j]["text"] == player for j in comb):
tk.messagebox.showinfo(f'Tic Tac Toe', 'User playing for "{player}" - won!')
for k in comb:
buttons[k].config(bg='#EE2923')
buttons_off()
restart()