Reputation: 21
I guess this must be some simple fix, but I am still becoming familiar with coding, so every once in a while I get stuck in some silly things (will catch up, eventually lol)
I have been trying to stop my Tic Tac Toe game once a winning condition is met (horizontal, vertical or diagonal), and I successfully have the message of the winner printed on the screen once one of conditions is met.
The thing is that after showing the message, I still can't stop the game. I would like to know what's the problem with my interruption.
This is the code I am using for the function that sets the symbols and that identifies the winner:
def player_char():
marker = ''
player1 = ''
player2 = ''
# Keep asking Player 1 to choose X or O, until entry is correct:
while marker != ('x').lower() and marker != ('o').lower():
marker = input("\n PLAYER 1 - CHOOSE X OR O : ")
# Assign opposite marker to Player 2
player1 = marker.upper()
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
print (f"\n P1 playing with: {player1}" + f"\n P2 playing with: {player2}")
markers = [player1, player2]
return markers
def check_victory(board, markers):
player1_win = False
player2_win = False
draw = False
# Check if players win the game in one of the lines, columns or diagonals available
for n in range(len(markers)):
if board[7] == board[8] == board[9] == markers[n]:
if n == 0:
player1_win = True
else:
player2_win = True
break
(I HAVE IMPLEMENTED ALL THE OTHER LINES AND COLUMNS THAT COULD BE A WIN. DID NOT PASTE THEM HERE TO MAKE QUESTION SHORTER, BUT THEY ALL FOLLOW THE LOGIC ABOVE)
I call the function while players are placing their markers and once one winning condition is met, I have the message printed:
if player1_win:
print("\n >>> GAME FINISHED! PLAYER 1 WINS!")
if player2_win:
print("\n >>> GAME FINISHED! PLAYER 2 WINS!")
But then the game is not interrupted (I was guessing it would be, due to my "break" after each condition). What could be going on within my IF ? As I said, guessing it must be pretty simple, but got stuck for now.
So this is my function that executes the game. "display_board" was implemented to print the board, so I didn't add it here, because I am simply calling it and printing the board, it doesn't return anything else. "free" is a parameter to print a second board next to the actual game board, showing available slots for a play
def play_game(board, markers, winner):
moves = 1
while winner[0] or winner[1] or winner[2] != True:
if moves % 2 == 1:
display_board(game_board, free)
print(game_board)
p1 = (input("\n >>> ( P1 ) Enter number for your move: "))
board[position_p1] = markers[0]
moves += 1
check_victory(board, markers)
(Next play follows same format for player 2, when "moves" is a pair number. I can verify if player 2 wins, but obviously game does not stop too)
Upvotes: 1
Views: 1187
Reputation: 71
break
in the inner if condition will just break out of that if
condition.
Instead, you can have one more condition at the end which will check if any of the following condition is True, then break out of the for loop.
player1_win = True or player2_win = True or draw = True
Upvotes: 1