如何在 Python Turtle 的 Tic-Tac-Toe 游戏中交替 2 个玩家?

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

我正在上计算机科学课,我们的第一个 Python 项目(海龟)是一个小井字游戏。该项目已接近完成,但我正在努力弄清楚如何在 2 个玩家之间切换,请告诉我我是如何处理这个问题的,因为我仍在学习。

我尝试使用一个用于 for 循环的变量,其中值等于 0,然后每次玩家移动时,它都会为该值添加+1,因此理论上,每次他们移动时,它都会添加1,然后 for 循环将不成立,然后切换到另一个玩家的另一个 for 循环。我现在意识到这些都没有任何意义,也不会起作用。看起来是这样的:

if current_player == "o" or current_player == "O":
    while current_player == "o" or current_player == "O":
        alternate_turn = 0
        while alternate_turn == 0:
            user_input = int(input("Type in a number where O would like to move: "))
            if user_input == 1 :
                goto(-134,140)
                alternate_turn = alternate_turn + 1

每次我运行这个,它都会崩溃。所以我现在陷入了最初的困境:

#If the user said "x", code checks for to see if true then runs commands for "x"
if current_player == "x" or current_player == "X" :
    while current_player == "x" or current_player == "X":
        user_input = int(input("Type in a number where X would like to move: "))

#If user said "o", code checks to see if true then runs commands for "o"
if current_player == "o" or current_player == "O":
    while current_player == "o" or current_player == "O":
        user_input = int(input("Type in a number where O would like to move: "))

这是一个可重现的示例:

#Ask user for input on who goes first
user_select = (input("Who is playing first?(X or O?): "))

#Whatever the user said for user_select, it is stored as the current_player
current_player = user_select

#If the user said "x", code checks for to see if true then runs following commands
if current_player == "x" or current_player == "X" :
    while current_player == "x" or current_player == "X":
        user_input = int(input("Type in a number where X would like to move: "))
#If user said "o", code checks to see if true then runs commands for "o"
if current_player == "o" or current_player == "O":
    while current_player == "o" or current_player == "O":
        user_input = int(input("Type in a number where O would like to move: "))

我非常不确定下一步该做什么。我尝试过寻找其他解决方案,但是,它们似乎都不适合我的代码(也许是因为它们不适合我的代码)。我想要实现的是,当它要求“X”或“O”输入时,其中任何一个都会输入一个他们将移动的数字,然后它会转到下一个玩家,然后在那之后做同样的事情,直到平局或有人获胜。我愿意接受任何解决此问题的方法,因为截止日期是本周五。谢谢您,如果我的帖子有点不清楚,我深表歉意,因为这是我第一次发帖。

python tic-tac-toe python-turtle
1个回答
0
投票

您的程序可能会崩溃,因为它将永远运行。在循环中的任何时刻都不会更改播放器。另外,您不需要

while
。我看到的另一个问题是,你试图变得过于自由。 “X”或“x”表示你连自己的球员是什么样的都不知道。当然,玩家可能能够输入“x”或“X”,但这与识别玩家没有任何关系。想象一下,如果这是国际象棋。你真的要检查棋盘一侧的每一个棋子,看看它是否是玩家一号吗?

if current_player == "x" or current_player == "X" :
    while current_player == "x" or current_player == "X":
        user_input = int(input("Type in a number where X would like to move: "))

这是切换玩家方法的一个非常基本的示例。我换球员的地方可能不适合你。我不知道你的代码是什么,这只是一个最小的例子。简而言之:您必须更改

current_player
才能更改
current_player

player_one = input("Input Player One Name: ")
player_two = input("Input Player Two Name: ")

current_player = player_one

while True:
    if current_player == player_one:
        user_input = int(input("Type in a number where X would like to move: "))
        current_player = player_two

    elif current_player == player_two:
        user_input = int(input("Type in a number where O would like to move: "))
        current_player = player_one

    else:
        raise ValueError('unrecognized player')
© www.soinside.com 2019 - 2024. All rights reserved.