【问题标题】:How to fix tic-tac-toe two player bug如何修复井字游戏两人游戏错误
【发布时间】:2021-04-25 02:37:36
【问题描述】:

我正在尝试制作井字游戏,但是当我尝试实现两个玩家时,打印出的结果(当前游戏状态)与预期不符。 例如,当我尝试将字母“x”放入框 1 时,它会在不同的位置添加随机字母/数字。我已经用一个玩家试过了,效果很好。

import random
a = ' '
b = ' '
c = ' '
d = ' '
e = ' '
f = ' '
g = ' '
h = ' '
i = ' '
def gamestateboard(a, b, c, d, e, f, g, h, i):
    x = [a, b, c]
    y = [d, e, f]
    z = [g, h, i]

    print(x)
    print(y)
    print(z)
gamestateboard(a, b, c, d, e, f, g, h, i)

def checkwin():
    if a == 0 and b == 0 and c == 0:
        print('Winner')
    if a == 0 and d == 0 and g == 0:
        print('Winner')
    if g == 0 and h == 0 and i == 0:
        print('Winner')
    if c == 0 and e == 0 and g == 0:
        print('Winner')
    if a == 0 and e == 0 and i == 0:
        print('Winner')
    if c == 0 and f == 0 and i == 0:
        print('Winner')



for i in range(1000000):
    inputs = str(input('Enter a position: '))
    if i%2 == 0:
        if inputs == '1':
            a = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
        if inputs == '2':
            b = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
        if inputs == '3':
            c = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
        if inputs == '4':
            d = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
        if inputs == '5':
            e = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
        if inputs == '6':
            f = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
        if inputs == '7':
            g = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
        if inputs == '8':
            h = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
        if inputs == '9':
            i = 0
            gamestateboard(a, b, c, d, e, f, g, h, i)
            checkwin()
else:
    if inputs == '1':
        a = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()
    if inputs == '2':
        b = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()
    if inputs == '3':
        c = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()
    if inputs == '4':
        d = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()
    if inputs == '5':
        e = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()
    if inputs == '6':
        f = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()
    if inputs == '7':
        g = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()
    if inputs == '8':
        h = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()
    if inputs == '9':
        i = 'X'
        gamestateboard(a, b, c, d, e, f, g, h, i)
        checkwin()

【问题讨论】:

  • 问题是你使用i作为你的循环变量以及第9个方块。看在上帝的份上,伙计,将您的位置存储在 9 个元素的列表中,而不是 9 个单独的变量中。然后你愚蠢的 9 个 if 语句集变成了一组语句。然后你甚至可以检查广场是否被占用。你意识到你不是在检查'X'胜利吗?只有0 获胜。

标签: python python-3.x tic-tac-toe


【解决方案1】:

你使用 i 作为你的循环变量,同时也是你用来在游戏世界中绘制的变量的名称。

我建议不要使用这些变量并使用索引。

gameworld =[ [' ',' ',' '] for i in range(3)]

def draw_world():
    for i in range(3):
        print(gameworld[i])

然后您可以像以前一样询问坐标或除法并使用模数来使用单个值。 4 // 3 = 1 4 % 3 = 1 位置 4 在 [1][1]

这意味着如果 input == list 你不需要你的巨人。

编辑:哦,是的,你可能还想用类似的东西做一个循环

no_winner = True
while no_winner:
    ...

如果 check_win 返回 true,你可以将 no_winner 设置为 False,然后你的游戏不会永远继续

编辑 2: 几乎忘记了你有一个缩进错误, else for i%2==0 虽然我猜这可能只是从你复制并粘贴到你的问题中开始的。您也永远不会检查其他人是否已经占据了一个正方形,因此当您的代码确实有效时,玩家可以覆盖另一个玩家的移动。

【讨论】:

    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    相关资源
    最近更新 更多