我正在使用 Python 制作井字游戏

时间:2021-03-08 20:29:45

标签: python python-3.9

这是我第一次发布问题,如果我在问题中遗漏了任何必要的细节,我深表歉意。

所以这是代码,它的目的是用机器玩井字游戏,一切正常,直到机器轮到它,如果随机函数选择那个数字,它会覆盖玩家选择的方格,我几乎确定问题可能来自 MakeListOfFreeFields(board) 函数但找不到任何东西。

我还向一些效率不高的地方调用了自由场函数,看看它是否有效。没有

感谢任何帮助,谢谢!

# 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.

1 个答案:

答案 0 :(得分:-1)

保留代码的一种解决方案是“清除”输出。 例如,使用:'IPython.display.clear_output()'

不要忘记使用“time.sleep()”来临时化它以保持可读性

这是您的代码:

# 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.