如何在 Python 的井字棋游戏中切换两个玩家?

时间:2021-06-02 15:43:16

标签: python turn

我是 Python 的初学者,正在尝试创建井字游戏。 但我有两个问题。一个问题是我不能切换两个播放器。另一个问题是我无法打印“你赢了!”这个词。我不知道出了什么问题。

如果你给我一个提示,我会很高兴。 提前谢谢你!!

box = {'1': ' ' , '2': ' ' , '3': ' ' ,'4': ' ' , '5': ' ' , '6': ' ' ,'7': ' ' , '8': ' ' , '9': ' ' }

list = []

for i in box:
    list.append(i)

def printbox(box):
    print(box['1'] + box['2'] + box['3'])
    print(box['4'] + box['5'] + box['6'])
    print(box['7'] + box['8'] + box['9'])

#Game setting
def game():

    turn = 'O'
    count = 0

    for i in range(9):
        printbox(box)
        print("Select the number!")

        a = input()

        if box[a] == ' ':
            box[a] = turn
            count += 1
        else:
            print("Pls select another number.")
            continue

        if count >= 8:
            if box['1'] == box['2'] == box['3']:
                printbox(box)
                print("You won!")
                break
            elif box['4'] == box['5'] == box['6']:
                printbox(box)
                print("You won!")
                break
            elif box['7'] == box['8'] == box['9']:
                printbox(box)
                print("You won!")
                break
            elif box['1'] == box['5'] == box['9']:
                printbox(box)
                print("You won!")
                break
            elif box['3'] == box['5'] == box['7']:
                printbox(box)
                print("You won!")
                break
            elif box['1'] == box['4'] == box['7']:
                printbox(box)
                print("You won!")
                break
            elif box['2'] == box['5'] == box['8']:
                printbox(box)
                print("You won!")
                break
            elif box['3'] == box['6'] == box['9']:
                printbox(box)
                print("You won!")
                break

        #in case of tie match.
        if count == 9:
                print("It's a Tie!")

        # Changing the two players.
        if turn =='O':
            turn = 'X'
        else:
            turn = 'O'
 
        game()

if __name__ == "__main__":
    game()

2 个答案:

答案 0 :(得分:1)

所以我坐下来让你的程序为自己工作,并注意到了一些问题。所以第一个问题是你检查

if turn == 'O':

转不是'O'而是'0'。您在分配转弯时使用了数字 0,而不是字母“O”。这种比较永远不会成立。

下一个大问题是您在每场比赛结束时都调用“游戏”。所以游戏 (1) 正常运行,然后你到最后并堆叠一个新的 game() 调用。在这个新的游戏调用中,您将轮次设置为“0”并将计数设置为 0。

如果您在最后移除对 game() 的调用,这将解决您的堆叠调用问题。

最后,在游戏结束时() 改变:

# Changing the two players.
    if turn =='o':
        turn = 'X'
    else:
        turn = 'o'

    game()

# Changing the two players.
    if turn =='0':
        turn = 'X'
    else:
        turn = '0'

哦,还有最后一件事,我会将 if count >= 8 更改为 if count >= 4 并将 != " " 添加到每个框组合检查中,以避免“获胜”连续出现 3 个空格.

编辑:

既然你更新了上面的代码,那么我想指出的是,修复是从游戏函数本身内部删除对“game()”的调用或减少缩进(因为你每次调用一个新游戏时for 循环运行),如果您希望它“再次播放”。您还需要像我建议的那样更改计数 >= 8 检查或强制玩家至少玩 8 回合,即使他们在 5 中获胜(这是可能的)。

我在玩脚本时确实注意到了另外两个问题。

全局声明框适用于第一场比赛,但在第二场比赛中,它永远不会重置。在 game() 函数内移动框,最后,

这没有任何作用

list = []

for i in box:
    list.append(i)

答案 1 :(得分:0)

您的代码没有说您赢了的原因是因为您只检查了 count >= 8 时的赢条件。这意味着只有在超过 8 圈后才能进入该方块。您可能希望每次都检查这一点,而不是基于条件。

如果你总是检查,你实际上会遇到另一个错误,因为你的字典起始值总是匹配的。这是因为您只检查值是否相等,而不是它们是“O”或“X”。我不会建议如何解决这个问题,因为您正在学习并且可能会喜欢这个练习!