I’m a beginner learner of Python and trying to create a tic-tac-toe game. But I have two problems. One problem is that I can't switch the two players. Another problem is that I can't print the words "You won!". I couldn't find out what's wrong.
I would be so glad if you give me a hint. Thank you in advance!!
box = {'1': ' ' , '2': ' ' , '3': ' ' ,'4': ' ' , '5': ' ' , '6': ' ' ,'7': ' ' , '8': ' ' , '9': ' ' }
list = []
for i in box:
list.append(i)
def printbox(box):
print(box['1'] + box['2'] + box['3'])
print(box['4'] + box['5'] + box['6'])
print(box['7'] + box['8'] + box['9'])
#Game setting
def game():
turn = 'O'
count = 0
for i in range(9):
printbox(box)
print("Select the number!")
a = input()
if box[a] == ' ':
box[a] = turn
count += 1
else:
print("Pls select another number.")
continue
if count >= 8:
if box['1'] == box['2'] == box['3']:
printbox(box)
print("You won!")
break
elif box['4'] == box['5'] == box['6']:
printbox(box)
print("You won!")
break
elif box['7'] == box['8'] == box['9']:
printbox(box)
print("You won!")
break
elif box['1'] == box['5'] == box['9']:
printbox(box)
print("You won!")
break
elif box['3'] == box['5'] == box['7']:
printbox(box)
print("You won!")
break
elif box['1'] == box['4'] == box['7']:
printbox(box)
print("You won!")
break
elif box['2'] == box['5'] == box['8']:
printbox(box)
print("You won!")
break
elif box['3'] == box['6'] == box['9']:
printbox(box)
print("You won!")
break
#in case of tie match.
if count == 9:
print("It's a Tie!")
# Changing the two players.
if turn =='O':
turn = 'X'
else:
turn = 'O'
game()
if __name__ == "__main__":
game()
So I sat down and got your program working for myself and noticed a few problems. So the first issue is that you check
if turn == 'O':
Turn is not 'O' it is '0'. You used the number 0 when you assigned turn, not the letter 'O'. This comparison is never true.
The next big issue is that you call "game" at the end of every game. So game (1) runs normally, then you get to the end and stack a new game() call. In this new game call, you set turn to '0' and count to 0.
If you remove the call to game() at the end this fixes your stacking call issue.
Lastly, at the end of game() change:
# Changing the two players.
if turn =='o':
turn = 'X'
else:
turn = 'o'
game()
to
# Changing the two players.
if turn =='0':
turn = 'X'
else:
turn = '0'
Oh, and one last thing, I would change if count >= 8
to if count >= 4
and add != " "
to each of the box combination checks to avoid "winning" with 3 empty spaces in a row.
Edit:
Since you updated your code above, then I want to note that the fix is to remove the call to "game()" from inside of the game function itself or decrease the indent (because you call a new game every time the for loop runs) if you want it to "play again". You also need to change the count >= 8 check like I suggested or force the players to play at least 8 turns even if they win in 5 (which is possible).
I did notice two other issues with the script just now while playing with it.
Declaring box globally works good for the first game but on the second game, it's never reset. Move box inside of the game() function and, lastly,
This does nothing
list = []
for i in box:
list.append(i)