【问题标题】:Tic Tac Toe game using python [closed]使用python的井字游戏[关闭]
【发布时间】:2021-03-05 00:21:03
【问题描述】:
  1. 如果您查看第 12 行,我需要做一个限制,这样您就不能相互重叠。 (例如,我不希望“X”出现在“O”已经存在的位置。(是的,我必须使用 while 循环)我尝试了不同的方法,但它似乎不起作用。

这是我目前想要改进的全部代码:

board=[1,2,3,4,5,6,7,8,9]
playerturn="X"

while True:
    for i in range(0,9,3):
        print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")

    choose=int(input("Where do you want to play your" + " " + playerturn + ":"))

    while[Need help making restriction here]
    print("That spot is already occupied")
    choose=int(input("Choose another spot:"))

    board[choose-1]=playerturn
    if playerturn =="X":
        playerturn="O"
    else:
        playerturn="X"

    count=0

    if (board[0]==board[1]) and (board[1]==board[2]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[0]
    print("The winner is", winner)
    break

    elif (board[3]==board[4]) and (board[4]==board[5]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[3]
    print("The winner is", winner)
    break

    elif (board[6]==board[7]) and (board[7]==board[8]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[6]
    print("The winner is", winner)
    break

    elif (board[0]==board[3]) and (board[3]==board[6]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[0]
    print("The winner is", winner)
    break

    elif (board[1]==board[4]) and (board[4]==board[7]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[0]
    print("The winner is", winner)
    break

    elif (board[2]==board[5]) and (board[5]==board[8]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[2]
    print("The winner is", winner)
    break

    elif (board[0]==board[4]) and (board[4]==board[8]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[0]
    print("The winner is", winner)
    break

    elif (board[2]==board[4]) and (board[4]==board[6]):
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
    if (i<6):
        print("------------")
    winner=board[2]
    print("The winner is", winner)
    break

    for item in board:
        if (item != "X") and (item != "O"):
            count +=1

    if count==0:
        for i in range(0,9,3):
            print(board[i], "|", board[i+1], "|", board[i+2])
        if (i<6):
            print("------------")
            print("Tie game")
            break

【问题讨论】:

  • 你的缩进真的搞砸了。请以正确的缩进重新发布,因为无法判断脚本在做什么。
  • while[] 应该是什么?这是无效的语法。
  • 我认为while[] 只是一个占位符,因为这似乎是他们要问的那一行...
  • 请从intro tour 重复on topichow to ask。 “告诉我如何解决这个编码问题”不是堆栈溢出问题。我们希望您做出诚实的尝试,然后然后就您的算法或技术提出一个具体的问题。 Stack Overflow 无意取代现有的文档和教程。
  • "Can Someone Help Me?" is not a valid SO question。这表明对于 Stack Overflow 而言,一系列需求过于广泛。

标签: python


【解决方案1】:

使用while 循环检查选项是否可以播放。

choose=int(input("Where do you want to play your" + " " + playerturn + ":"))
while board[choose-1] in ('X', 'O'): # These are the values when a spot is occupied
    print("That spot is already occupied")
    choose=int(input("Choose another spot:"))

【讨论】:

  • 您介意解释一下这是如何工作的吗?
  • 其中哪一部分需要解释?
  • 你为什么把它放在(X,Y)?那有什么作用?
  • 另外我应该有一个布尔表达式
  • 对不起,那只是个错误
猜你喜欢
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多