I am trying to make a simple tic-tac-toe game. Most of it seems to be working, but drawing certain moves gives you instant victory (even without combining 3 fields) and I cannot figure out why. If you (or the computer) plays the fields 3, 6, 7, 8 or 9, you win. I guess I made a mistake in the part where I define the rules for winning (the function in my code is called victory_for). I know there are lots of tic-tac-toe codes I could use, but I want to learn something out of this as I am just beginning with Python.
This is how my starting board looks (computer always plays first with an X in the middle):
+-------+-------+-------+
| | | |
| 1 | 2 | 3 |
| | | |
+-------+-------+-------+
| | | |
| 4 | X | 6 |
| | | |
+-------+-------+-------+
| | | |
| 7 | 8 | 9 |
| | | |
+-------+-------+-------+
This is the code I wrote. I put a # comment where I think the error lies.
#Computer plays with X, human plays with O.
#I need random numbers for the computer to play its turns
from random import randrange
#This is how the board looks at the beginning, assuming computer played first with an X in the middle
board = [[1, 2, 3], [4, "X", 6], [7, 8, 9]]
#Shows the current state of the board
def display_board(board):
print(
"\n+-------+-------+-------+",
"\n| | | |",
"\n| ",board[0][0]," | ",board[0][1]," | ",board[0][2]," |",
"\n| | | |",
"\n+-------+-------+-------+",
"\n| | | |",
"\n| ",board[1][0]," | ",board[1][1]," | ",board[1][2]," |",
"\n| | | |",
"\n+-------+-------+-------+",
"\n| | | |",
"\n| ",board[2][0]," | ",board[2][1]," | ",board[2][2]," |",
"\n| | | |",
"\n+-------+-------+-------+"
)
#Makes a list of free fields out of which the human and the computer can draw their turns
def make_list_of_free_fields(board):
list_of_free_fields = []
for field in range(1,10):
for row in range(3):
if field in board[row]:
list_of_free_fields.append(field)
return(list_of_free_fields)
#This is how the human enters its move
def enter_move(board):
while True:
user_input = int(input("Choose a field:"))
if user_input in make_list_of_free_fields(board):
break
print("You must choose one of the free fields on the board by typing 1-9")
row = (user_input - 1) // 3
column = (user_input - 1) - row * 3
board[row][column] = "O"
#I think this is where the error lies. It is supposed to check if the current player (defined by the sign) won.
def victory_for(board, sign):
if (board[0][0] and board[0][1] and board[0][2] == sign or
board[1][0] and board[1][1] and board[1][2] == sign or
board[2][0] and board[2][1] and board[2][2] == sign or
board[0][0] and board[1][0] and board[2][0] == sign or
board[0][1] and board[1][1] and board[2][1] == sign or
board[0][2] and board[1][2] and board[2][2] == sign or
board[0][0] and board[1][1] and board[2][2] == sign or
board[0][2] and board[1][1] and board[2][0] == sign):
return True
else:
return False
#This is how the computer plays its turn
def draw_move(board):
while True:
comp_input = randrange(10)
if comp_input in make_list_of_free_fields(board):
break
row = (comp_input - 1) // 3
column = (comp_input - 1) - row * 3
board[row][column] = "X"
#This part is just supposed to connect everything and loop the game until someone wins.
print(
"Welcome to Tic-Tac-Toe by me!",
"\nPut Os on the field by choosing field numbers (1-9)",
"\nComputer plays first by placing an X"
)
display_board(board)
while True:
enter_move(board)
display_board(board)
if victory_for(board, "O") == True:
print("You won!")
break
draw_move(board)
display_board(board)
if victory_for(board, "X") == True:
print("You lost!")
break