I am working on a tic tac toe project for school and am running into an issue with my winning detection. For some reason, it is registering as a win, when no one is getting 3 in a row. I have lists that hold the players past moves and have if statements to check the lists. Thanks for the help in advanced.
import time
import random
import os
board = """
1 2 3
| |
a - | - | -
_____|_____|_____
| |
b - | - | -
_____|_____|_____
| |
c - | - | -
| | """
player1SymbolCharacter = 'X'
player2SymbolCharacter = '0'
index1 = 40
index2 = 46
index3 = 52
index4 = 97
index5 = 103
index6 = 109
index7 = 154
index8 = 160
index9 = 166
def win1():
print(board)
print('\nPlayer 1 Won!')
return True
def win2():
print(board)
print('\nPlayer 2 Won!')
return True
player1tiles = []
player2tiles = []
print(board)
while True:
player1 = input("Player 1, Enter Your Position (letter, number):")
if (player1 == "a1"):
board = board[:index1] + player1SymbolCharacter + board[index1+1:]
player1tiles.append("a1")
elif (player1 == "a2"):
board = board[:index2] + player1SymbolCharacter + board[index2+1:]
player1tiles.append("a2")
elif (player1 == "a3"):
board = board[:index3] + player1SymbolCharacter + board[index3+1:]
player1tiles.append("a3")
elif (player1 == "b1"):
board = board[:index4] + player1SymbolCharacter + board[index4+1:]
player1tiles.append("b1")
elif (player1 == "b2"):
board = board[:index5] + player1SymbolCharacter + board[index5+1:]
player1tiles.append("b2")
elif (player1 == "b3"):
board = board[:index6] + player1SymbolCharacter + board[index6+1:]
player1tiles.append("b3")
elif (player1 == "c1"):
board = board[:index7] + player1SymbolCharacter + board[index7+1:]
player1tiles.append("c1")
elif (player1 == "c2"):
board = board[:index8] + player1SymbolCharacter + board[index8+1:]
player1tiles.append("c2")
elif (player1 == "c3"):
board = board[:index9] + player1SymbolCharacter + board[index9+1:]
player1tiles.append("c3")
os.system('clear')
print(player1tiles)
print(player2tiles)
if "a1" and "a2" and "a3" in player1tiles:
if win1():
break
elif "b1" and "b2" and "b3" in player1tiles:
if win1():
break
elif "c1" and "c2" and "c3" in player1tiles:
if win1():
break
elif "a1" and "b1" and "c1" in player1tiles:
if win1():
break
elif "a2" and "b2" and "c2" in player1tiles:
if win1():
break
elif "a3" and "b3" and "c3" in player1tiles:
if win1():
break
elif "a1" and "b2" and "c3" in player1tiles:
if win1():
break
elif "a3" and "b2" and "c1" in player1tiles:
if win1():
break
print(board)
player2 = input("Player 2, Enter Your Position (number, letter):")
if (player2 == "a1"):
board = board[:index1] + player2SymbolCharacter + board[index1+1:]
player2tiles.append("a1")
elif (player2 == "a2"):
board = board[:index2] + player2SymbolCharacter + board[index2+1:]
player2tiles.append("a2")
elif (player2 == "a3"):
board = board[:index3] + player2SymbolCharacter + board[index3+1:]
player2tiles.append("a3")
elif (player2 == "b1"):
board = board[:index4] + player2SymbolCharacter + board[index4+1:]
player2tiles.append("b1")
elif (player2 == "b2"):
board = board[:index5] + player2SymbolCharacter + board[index5+1:]
player2tiles.append("b2")
elif (player2 == "b3"):
board = board[:index6] + player2SymbolCharacter + board[index6+1:]
player2tiles.append("b3")
elif (player2 == "c1"):
board = board[:index7] + player2SymbolCharacter + board[index7+1:]
player2tiles.append("c1")
elif (player2 == "c2"):
board = board[:index8] + player2SymbolCharacter + board[index8+1:]
player2tiles.append("c2")
elif (player2 == "c3"):
board = board[:index9] + player2SymbolCharacter + board[index9+1:]
player2tiles.append("c3")
os.system('clear')
print(player1tiles)
print(player2tiles)
if "a1" and "a2" and "a3" in player2tiles:
if win2():
break
elif "b1" and "b2" and "b3" in player2tiles:
if win2():
break
elif "c1" and "c2" and "c3" in player2tiles:
if win2():
break
elif "a1" and "b1" and "c1" in player2tiles:
if win2():
break
elif "a2" and "b2" and "c2" in player2tiles:
if win2():
break
elif "a3" and "b3" and "c3" in player2tiles:
if win2():
break
elif "a1" and "b2" and "c3" in player2tiles:
if win2():
break
elif "a3" and "b2" and "c1" in player2tiles:
if win2():
break
print(board)