Tic Tac toe程序错误(Python)

问题描述 投票:1回答:1

下面显示的代码是Tic tac toe游戏,其中有两个玩家(Player1和Player2是人类)。我在“#Player 1 Playys now”和“#Player 2 Playys now”部分的if else语句中遇到了问题。确切地说,计算机总是认为每个盒子已经填充了除1,2,3,4,5,6,7,8,9之外的某些值,并且它一直显示消息“该单元格已被标记。请尝试在嵌套的else语句中定义的另一个单元格。有人可以告诉我如何解决这个问题吗?

board = [0,1,2,3,4,5,6,7,8,9]
print type(board[1])
def board_invoke():
print "| ",board[1], " | ", board[2], " | ", board[3], " | ", "\n", "-------------------", "\n", "| ", board[4], " | ",board[5], " | ", board[6], " | ", "\n", "-------------------", "\n", "| ",  board[7], " | ",board[8], " | ", board[9], " | "

def game_start():
Player1= raw_input("Select Player 1 between X or O : ")
if Player1 not in ('X','x','O','o'):
    print "That is not an expected player"
    game_start()
else:
    print "\nSince you have selected Player 1 as %s" %Player1
    print "\nThe Player 2 is assigned :",
    if Player1 in ('X','x'):
        Player2 = ("O")
    else:
        Player2 = ("X")
    print Player2
    print "\nThe game Starts now....\n"
    board_invoke()
    while (1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9) in board:

        # Winning Condition
        if board[1]==board[2]==board[3]==Player1:
            print Player1," wins"
            break
        elif board[4]==board[5]==board[6]==Player1:
            print Player1, " wins"
            break
        elif board[7]==board[8]==board[9]==Player1:
            print Player1, " wins"
            break
        elif board[1]==board[2]==board[3]==Player2:
            print Player2, " wins"
            break
        elif board[4]==board[5]==board[6]==Player2:
            print Player2, " wins"
            break
        elif board[7]==board[8]==board[9]==Player2:
            print Player2, " wins"
            break
        elif board[1]==board[5]==board[9]==Player1:
            print Player1, " wins"
            break
        elif board[3]==board[5]==board[7]==Player1:
            print Player1, " wins"
            break
        elif board[1]==board[5]==board[9]==Player2:
            print Player2, " wins"
            break
        elif board[3]==board[5]==board[7]==Player2:
            print Player2, " wins"
            break
        elif board[1]==board[4]==board[7]==Player1:
            print Player1, " wins"
            break
        elif board[2]==board[5]==board[8]==Player1:
            print Player1, " wins"
            break
        elif board[3]==board[6]==board[9]==Player1:
            print Player1, " wins"
            break
        elif board[1]==board[4]==board[7]==Player2:
            print Player2, " wins"
            break
        elif board[2]==board[5]==board[8]==Player2:
            print Player2, " wins"
            break
        elif board[3]==board[6]==board[9]==Player2:
            print Player2, " wins"
            break

        # Player 1 Plays now

        Cell_no = raw_input("Player 1 : Please select a number you want to mark....")
        Cell_no = int(Cell_no)

        if Cell_no not in board:
            print "Please enter a cell number within the scope of available cells"
        else:
            if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
                print "That cell is already marked. Please try another cell"
                continue
            else:
                board[Cell_no] = Player1
                board_invoke()

        # Player 2 Plays now

        Cell_no = raw_input("Player 2 : Please select a number you want to mark....")
        Cell_no = int(Cell_no)

        if Cell_no not in board:
            print "Please enter a cell number within the scope of available cells"
        else:
            if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
                print "That cell is already marked. Please try another cell"
                continue
            else:
                board[Cell_no] = Player2
                board_invoke()

    print "Do you want to play again ?"
    user_decision = raw_input("Please type Yes or No : ")
    if user_decision == ('YES' or 'Yes' or 'yes'):
        game_start()
    else:
        print "Ok. I take it that we will wrap up !"
        print "See you again !"



game_start()
python-2.7 if-statement syntax-error tic-tac-toe
1个回答
1
投票

你的if语句不完全正确。你正在评估:

else:
if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
    print "That cell is already marked. Please try another cell"

这总是如此。 or关键字在逻辑上结合了每个比较。那么第一个比较语句就是'X',它不是null或0,因此总是如此。由于第一个陈述是正确的,整个if语句都是正确的。

我经常看到人们开始学习编程。 If语句不能像人类句子一样编写。您必须将每个字符与变量进行比较。这是一个简单的(不是python特定的,非常丑陋)解决您的问题的方法:

else:
 if 'X' == board[Cell_no] or 'x' == board[Cell_no] or 'O' == board[Cell_no] or 'o' == board[Cell_no]:
      print "That cell is already marked. Please try another cell"

然而,更好的方法是将该检查转换为与列表进行比较:

else:
 if board[Cell_no] in ['X','x', 'O', 'o']:
      print "That cell is already marked. Please try another cell"
© www.soinside.com 2019 - 2024. All rights reserved.