This is my first time posting a question so I apologize if I missed any necessary details from my question.
So here is the code, it is intended to play tic-tac-toe with the machine and everything goes right until the machine plays its turn and it overwrites the player's selected square if the random function chooses that number, I am almost sure that the problem might come from the MakeListOfFreeFields(board) function but can't find anything.
I also called the free fields function to some not efficient spots to see if it worked. It didn't
Any help is appreciated, thanks!
# coding=utf-8
from random import randrange
def DisplayBoard(board):
print("+","-"*7,"+","-"*7,"+","-"*7,"+",sep="")
print("|","|","|","|",sep=" ")
print("| ",board[0][0]," | ",board[0][1]," | ",board[0][2]," |",sep="")
print("|","|","|","|",sep=" ")
print("+","-"*7,"+","-"*7,"+","-"*7,"+",sep="")
print("|","|","|","|",sep=" ")
print("| ",board[1][0]," | ",board[1][1]," | ",board[1][2]," |",sep="")
print("|","|","|","|",sep=" ")
print("+", "-"*7,"+","-"*7,"+","-"*7,"+",sep="")
print("|","|","|","|",sep=" ")
print("| ",board[2][0]," | ",board[2][1]," | ",board[2][2]," |",sep="")
print("|","|","|","|",sep=" ")
print("+", "-"*7,"+","-"*7,"+","-"*7,"+",sep="")
return
def EnterMove(board):
MakeListOfFreeFields(board)
while True:
movPlayer = int(input("Insert the box number you wish to select.\n"))
if movPlayer < 1 and movPlayer > 9:
print("Pick another number.\n")
continue
elif movPlayer not in board[0] and movPlayer not in board[1] and movPlayer not in board[2]:
print("Spot taken, pick another one!")
continue
for row in range(0,3):
for column in range(0,3):
if board[row][column] == movPlayer:
board[row][column] = "O"
return
def MakeListOfFreeFields(board):
for row in range(0,3):
for column in range(0,3):
if board[row][column] == "X" or board[row][column] == "O":
continue
else:
emptySquares.append(([row],[column]))
def VictoryFor(board, sign):
if board[0][0] == sign and board[0][1] == sign and board[0][2] == sign:
return True
elif board[1][0] == sign and board[1][1] == sign and board[1][2] == sign:
return True
elif board[2][0] == sign and board[2][1] == sign and board[2][2] == sign:
return True
elif board[0][0] == sign and board[1][0] == sign and board[2][0] == sign:
return True
elif board[0][1] == sign and board[1][1] == sign and board[2][1] == sign:
return True
elif board[0][2] == sign and board[1][2] == sign and board[2][2] == sign:
return True
elif board[0][0] == sign and board[1][1] == sign and board[2][2] == sign:
return True
elif board[2][0] == sign and board[1][1] == sign and board[0][2] == sign:
return True
else:
return
def DrawMove(board):
MakeListOfFreeFields(board)
while True:
row = randrange(3)
column = randrange(3)
if ([row],[column]) not in emptySquares:
continue
else:
board[row][column] = "X"
return
board = [[1,2,3],[4,"X",6],[7,8,9]]
emptySquares = []
moves = 1
player = "O"
CPU = "X"
print("Welcome to Tic-Tac-Toe!")
while moves < 9:
moves += 1
DisplayBoard(board)
EnterMove(board)
DisplayBoard(board)
DrawMove(board)
DisplayBoard(board)
VictoryFor(board, player)
if VictoryFor(board, player) == True:
print("Congratulations! You've won!")
break
VictoryFor(board, CPU)
if VictoryFor(board, CPU) == True:
print("Better luck next time! You've lost. :(")
break
#End of the program.