微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

tic tac toe python-turtle 交替 x 和 o

如何解决tic tac toe python-turtle 交替 x 和 o

我正在为学校制作井字游戏,并在点击的功能中结束我试图让它看看板上是否有空白空间,如果有,它可以继续放置 x 或 o如果它不是,它会打印位置是否已满以阻止用户在 X 上绘制 O 或其他方式,相反它会一直说该位置已满,即使它没有,并且不会在 x 和 o 之间交替

#cross
board =["","",""]
nextturn = "x" 
def draw_x_o(board):
    x=-300
    y=300
    for stuff in board:
        if stuff=="x":
            x_draw(x,y)
        elif stuff=="o":
            o_draw(x,y)
        else:
            print("nothing here")
        x=x+200
        if x >100:
            x=-300
            y=y-200
def clicked(x,y):
    global board,nextturn
    colum=(300+x)//200
    row=(-y+300 )//200
    square=colum + row*3
    square= int(square)
    print("you clicked",x,",y,"square",square)
    board[square]= nextturn
    if board[square]=="": 
        if nextturn=="x":
            nextturn="o"
        elif nextturn=="o":
            nextturn="x"
    else:
        print("the place is full")
    draw_x_o(board)
onscreenclick(clicked)
mainloop()

解决方法

你的代码会更容易阅读,因为你为你的板使用了一个 int 列表:0 如果它是空的,1 代表交叉,2 代表一轮。

board = [[0]*3]*3 
print(board[0][2] )  # print the top right case,you can access to any
# case with board[y][x] and it allows you to print
# your board in the console with :
sign = [' ','x','o']
for l in board:
     print(*(sign[i] for i in l))  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。