对我的井字游戏计划的帮助很少

时间:2015-04-30 01:07:56

标签: python if-statement while-loop turtle-graphics

我在Python 3中创建的井字游戏需要一些帮助。看看我有趣的程序并尝试一下。在那之后,请帮助我在我的程序中创建while语句。 那就是当用户选择正方形时,你应该继续问他们,直到他们选择一个空方块。当选择一个空方块时,程序会像以前一样继续。我不习惯while语句,请帮我解决这个问题!

这是我的计划:

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()

3 个答案:

答案 0 :(得分:0)

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

//In order to determine if you are at the top you have to check if scrollTop is equal to 0.

var scrollTop = document.getElementById(id).scrollTop;

if(scrollTop === 0)
{
    console.log("I am at the top");
}

//In order to determine if you are at the bottom you have to check if scrollTop + offsetHeight is greater than scrollHeight

var offsetHeight = document.getElementById(id).offsetHeight;
var scrollHeight = document.getElementById(id).scrollHeight;

if(scrollTop + offsetHeight > scrollHeight)
{
    console.log("I am at the bottom");
}

只要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: ")) x未指示主板上的空白图块,就会一直询问用户输入。

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

eval

这会将输入拆分为逗号,将其转换为字符串列表。然后x,y = map(int,input("Enter coordinates").split(',')) 将函数map应用于列表中的每个元素,将它们转换为整数。然后将这些内容解压缩到intx

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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!")