I'm trying to create a TicTacToe game on Python, and I'm using lists in 3 variables for the moves:
board0 = ["-", "-", "-"]
board1 = ["-", "-", "-"]
board2 = ["-", "-", "-"]
So far I've managed to make an AI which choses random moves, but I'm struggling on finding a compact and efficient way of checking for win conditions. I've got a huge bulk of code which does it, but I want to know if there's a way of streamlining it to get the same job done.
if board0[0] == "X":
if board0[1] == "X":
if board0[2] == "X":
print("PLAYER wins!")
print(sys.exit())
if board0[0] == "X":
if board1[1] == "X":
if board2[2] == "X":
print("PLAYER wins!")
print(sys.exit())
if board0[0] == "X":
if board1[0] == "X":
if board2[0] == "X":
print("PLAYER wins!")
print(sys.exit())
if board0[1] == "X":
if board1[1] == "X":
if board2[1] == "X":
print("PLAYER wins!")
print(sys.exit())
if board0[2] == "X":
if board1[1] == "X":
if board2[0] == "X":
print("PLAYER wins!")
print(sys.exit())
if board0[2] == "X":
if board1[2] == "X":
if board2[2] == "X":
print("PLAYER wins!")
print(sys.exit())
if board1[0] == "X":
if board1[1] == "X":
if board1[2] == "X":
print("PLAYER wins!")
print(sys.exit())
if board2[0] == "X":
if board2[1] == "X":
if board2[2] == "X":
print("PLAYER wins!")
print(sys.exit())