-1

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.
V1c70r
  • 1
  • 2
  • 1
    How about you figure out for sure which part is not working as it should, and post a [mre]? – khelwood Mar 08 '21 at 20:33
  • 2
    You probably need to reset the `emptySquares` list each time you call the function. – 001 Mar 08 '21 at 20:34
  • I think you'd be better off skipping `emptySquares` entirely and just looking in the board to see if a space is available. – khelwood Mar 08 '21 at 21:09
  • 2
    @AlainT. please don't recommend users post code that doesn't work properly to Code Review, since such code is not [on-topic](https://codereview.stackexchange.com/help/on-topic) there. – Sᴀᴍ Onᴇᴌᴀ Mar 08 '21 at 21:15

1 Answers1

-1

One solution to keep your code is to 'clear' your output. For example, use : ‘IPython.display.clear_output()‘

Don't forget to temporize it to keep it readable with a ‘time.sleep()‘

Here is your code:

# coding=utf-8
import time
from random import randrange
from IPython.display import clear_output


def clear():
    clear_output(wait=True)

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
    clear()
    DisplayBoard(board)
    time.sleep(0.3)
    clear()
    EnterMove(board)
    time.sleep(0.3)
    clear()
    DrawMove(board)
    time.sleep(0.3)
    clear()
    DisplayBoard(board)
    time.sleep(0.3)
    clear()
    VictoryFor(board, player)
    time.sleep(.3)

    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.
Lumber Jack
  • 602
  • 3
  • 9
  • Hello everyone, thanks for your so quick response and sorry for my delay at getting back to you. @Lumber Jack Thanks for your answer, it indeed corrected the problem but is not precisely what I was looking for. – V1c70r Mar 10 '21 at 22:17
  • What @Johnny Mopp suggested is more what I wanted so I tried moving the emptySquares list to the free fields function as a global variable and now I got another problem though I will solve that one on my own or die trying. Thanks a lot y'all. – V1c70r Mar 10 '21 at 22:17
  • Fixed. Worked! Thanks! – V1c70r Mar 10 '21 at 22:24
  • Great! Happy coding – Lumber Jack Mar 10 '21 at 22:25