我的井字游戏程序帮助不大

Posted

技术标签:

【中文标题】我的井字游戏程序帮助不大【英文标题】:Little assistance with my tic-tac-toe program 【发布时间】:2015-07-09 12:59:00 【问题描述】:

我需要一些关于我用 Python 3 创建的井字游戏的帮助。看看我的有趣程序并尝试一下。之后,请帮我在我的程序中创建一个while 语句。 也就是说,当用户选择的正方形被填满时,你应该继续询问他们,直到他们选择一个空的正方形。 When the choose an empty square, the program continues as before. I am not used to the while statements, please help me on this!

这是我的程序:

from turtle import *

def setUp():
#Set up the screen and turtle
    win = Screen()
    tic = Turtle()
    tic.speed(10)
#Change the coordinates to make it easier to tranlate moves to screen coordinates:
    win.setworldcoordinates(-0.5,-0.5,3.5, 3.5)

#Draw the vertical bars of the game board:
    for i in range(1,3):
       tic.up()
       tic.goto(0,i)
       tic.down()
       tic.forward(3)

#Draw the horizontal bars of the game board:
    tic.left(90)    #Point the turtle in the right direction before drawing
    for i in range(1,3):
        tic.up()
        tic.goto(i,0)
        tic.down()
        tic.forward(3)

    tic.up()        #Don't need to draw any more lines, so, keep pen up

#Set up board:
    board = [["","",""],["","",""],["","",""]]

    return(win,tic,board)

def playGame(tic,board):
#Ask the user for the first 8 moves, alternating between the players X and O:
    for i in range(4):
        x,y = eval(input("Enter x, y coordinates for X's move: "))
        tic.goto(x+.25,y+.25)
        tic.write("X",font=('Arial', 90, 'normal'))
        board[x][y] = "X"

        x,y = eval(input("Enter x, y coordinates for O's move: "))                 
        tic.goto(x+.25,y+.25)
        tic.write("O",font=('Arial', 90, 'normal'))
        board[x][y] = "O"

# The ninth move:
    x,y = eval(input("Enter x, y coordinates for X's move: "))
    tic.goto(x+.25,y+.25)
    tic.write("X",font=('Arial', 90, 'normal'))
    board[x][y] = "X"

def checkWinner(board):
    for x in range(3):
        if board[x][0] != "" and (board[x][0] == board[x][1] == board[x][2]):
            return(board[x][0])  #we have a non-empty row that's identical
    for y in range(3):
        if board[0][y] != "" and (board[0][y] == board[1][y] == board[2][y]):
            return(board[0][y])  #we have a non-empty column that's identical
    if board[0][0] != "" and (board[0][0] == board[1][1] == board[2][2]):
        return(board[0][0])
    if board[2][0] != "" and (board[2][0] == board[1][1] == board[2][0]):
        return(board[2][0])   
    return("No winner")

def cleanUp(tic,win):
#Display an ending message: 
    tic.goto(-0.25,-0.25)
    tic.write("Thank you for playing!",font=('Arial', 20, 'normal'))

    win.exitonclick()#Closes the graphics window when mouse is clicked


def main():
    win,tic,board = setUp()   #Set up the window and game board
    playGame(tic,board)       #Ask the user for the moves and display
    print("\nThe winner is", checkWinner(board))  #Check for winner
    cleanUp(tic,win)    #Display end message and close window


main()

【问题讨论】:

这不行,对吧?你在一个运行四次的循环中要求 X 移动,然后你要求 O 移动一次,因为它在循环之外。好消息是,如果你是 X,你总是会赢。 【参考方案1】:

您可能正在寻找这样的东西:

x,y = None,None
while x == None or y == None or board[x][y] != "";
  x,y = eval(input("Enter x, y coordinates for X's move: "))

只要xy 没有指示板上的空白图块,它将一直询问用户输入。

顺便说一句,您可能会考虑更改处理输入的方式。现在您正在使用eval,这可能很危险,因为可以执行任何输入。手动处理输入可能会更好,如下所示:

x,y = map(int,input("Enter coordinates").split(','))

这会在逗号处拆分输入,将其转换为字符串列表。 map 然后将函数 int 应用于列表中的每个元素,将它们转换为整数。然后将它们解压缩到 xy

【讨论】:

【参考方案2】:
for i in range(4):
    while True:
        move = input("Enter x, y coordinates for X's move: ")
        x,y = int(move[0]), int(move[-1]) # don't use eval()
        if board[x][y] not in ('X', 'O') # if valid
            tic.goto(x+.25,y+.25)
            tic.write("X",font=('Arial', 90, 'normal'))
            board[x][y] = "X"
            break # break out of loop after doing stuff

【讨论】:

【参考方案3】:

您可能希望使用验证函数获取整个输入字符串并在验证函数中对其进行解析,如下面的代码:

def isUserInputValid (s):
    cordinate=s.split(',')
    ...... # Your validation logic goes here



while not isUserInputValid(input("Please put your cell cordinate x, y:")):
        print("Your choice is not valid!")

【讨论】:

以上是关于我的井字游戏程序帮助不大的主要内容,如果未能解决你的问题,请参考以下文章

从示例井字游戏开始的 Chromecast 应用程序 - 无法启动应用程序:请求失败

在井字游戏 C++ 中使用“新游戏”按钮,win32 应用程序初学者

程序员面试金典-面试题 16.04. 井字游戏

Visual Studio 2019 中的一个简单井字游戏程序导致 Windows Defender 报告威胁

为啥我的神经网络不能正确分类这些井字游戏模式?

为啥我的井字游戏代码无法检测到有人中奖了?蟒蛇 3