I am trying to write a tik tac toe program in python. But I am stuck on finding a solution to the problem of finding a win condition. Here's my code so far:
tik_tac_toe_board = []
diagonal_one = []
diagonal_two = []
n = 3
for i in range(3):
row = []
for y in range(3):
row.append("")
tik_tac_toe_board.append(row)
x_or_o_selection = input("Are you going to be using x's or o's: ")
print(x_or_o_selection)
while True:
diagonal_one = []
diagonal_two = []
columns = [[], [], []]
rows = [[], [], []]
input_tik = input("x or o, with position(x,y): ")
list_placeholder = input_tik.split(",")
position = list_placeholder[1:]
x_or_o = list_placeholder[0]
if x_or_o != x_or_o_selection:
print("invalid input")
elif x_or_o != "x" and x_or_o != "o":
print("invalid input")
elif x_or_o == "x" or x_or_o == "o":
tik_tac_toe_board[int(position[0])][int(position[1])] = (list_placeholder[0])
columns[0] = [tik_tac_toe_board[0][0], tik_tac_toe_board[1][0], tik_tac_toe_board[2][0]]
columns[1] = [tik_tac_toe_board[0][1], tik_tac_toe_board[1][1], tik_tac_toe_board[2][1]]
columns[2] = [tik_tac_toe_board[0][2], tik_tac_toe_board[1][2], tik_tac_toe_board[2][2]]
rows[0] = [tik_tac_toe_board[0]]
rows[1] = [tik_tac_toe_board[1]]
rows[2] = [tik_tac_toe_board[2]]
for i in range(0, len(tik_tac_toe_board)):
diagonal_one.append(tik_tac_toe_board[i][i])
for i in range(0, len(tik_tac_toe_board)):
diagonal_two.append(tik_tac_toe_board[(n-i)-1][i])
if rows[0][1:] == rows[0][:-1]:
print("winner")
break
print(tik_tac_toe_board[0])
print(tik_tac_toe_board[1])
print(tik_tac_toe_board[2])
I tried to set up a win condition using the to first row of the tik tac toe board to test with, but even when the win condition is met, the code doesn't recognize a winner and doesn't break.
Aucun commentaire:
Enregistrer un commentaire