1

好的,所以我正在编写一个井字游戏,并且遇到了一个我似乎无法解决的非常糟糕的错误。我创建了一个函数,如果玩家即将获胜,计算机将阻止玩家,但是,如果成功阻止一次,即使满足条件,它也不会再触发。该函数的代码是:

def block():
for t in range(0, 9, 3):
        if slot[t] == user_team and slot[t+1] == user_team and (slot[t+2] \
           != user_team) and (slot[t+2] != computer_team):
            slot[int(t+2)] = computer_team
            return
        elif slot[t+1] == user_team and slot[t+2] == user_team and (slot[t] \
             != user_team) and (slot[t] != computer_team):
            slot[int(t)] = computer_team
            return
        elif slot[t] == user_team and slot[t+2] == user_team and (slot[t+1] \
             != user_team) and (slot[t+1] != computer_team):
            slot[int(t+1)] = computer_team
            return

for t in range(3):
        if slot[t] == user_team and slot[t+3] == user_team and (slot[t + 6] \
           != user_team) and (slot[t+6] != computer_team):
            slot[int(t+6)] = computer_team
            return
        elif slot[t+3] == user_team and slot[t+6] == user_team and (slot[t] \
             != user_team) and (slot[t] != computer_team):
            slot[int(t)] = computer_team
            return
        elif slot[t] == user_team and slot[t+6] == user_team and (slot[t+3] \
             != user_team) and (slot[t+3] != computer_team):
            slot[int(t+3)] = computer_team

此外,user_team 和 computer_team 会返回该玩家是 X 还是 O,并且slot[int()] = computer_team用于将移动放在棋盘上。下面是调用函数的地方(以防我在这里搞砸了。):

else:
    draw_board()
    '''win()'''
    block()
    cmove()
    turn = "user"
    if end_game() == True:
        computer_win += 1
        draw_board()
        print ("The computer has won! But... We already knew that would happen. (:")
        playing = False
    elif end_game() == "Tie":
        tie_win += 1
        draw_board()
        print ("The game is a tie. You're going to have to try harder " \
               + "\n" + "if you wish to beat the computer!" + "\n")
        playing = False
    else:
        pass

如果你们中的任何一个人能告诉我哪里出错了,那我就开心了。C:

板(打印是缩进的,它只是不想在这里。)

def draw_board():
'''Opted to use lists so that the numbers can be replaced with either
    X or O later on and so that testing whether the game is over is simpler'''
print (" " + str(slot[0]) + " | " + str(slot[1]) + " | " + str(slot[2]))
print ("-----------")
print (" " + str(slot[3]) + " | " + str(slot[4]) + " | " + str(slot[5]))
print ("-----------")
print (" " + str(slot[6]) + " | " + str(slot[7]) + " | " + str(slot[8]))
print ("\n")

新错误:

这是我第 4 步后的棋盘

X | ○ | X

○ | 4 | 5

X | 7 | X

移动 4 后计算机的棋盘(两步,替换一个 x)

X | ○ | X

○ | 4 | ○

○ | 7 | X

4

1 回答 1

2

我相信您的问题在于您的block功能逻辑。

这是你的董事会:

0 1 2
3 4 5
6 7 8

浏览第一对嵌套for循环,让我们看看你的代码做了什么:

for t in range(0,9,3):
    for y in range(1, 9, 3):

这将为您提供以下对t, y:(0,1), (0,4), (0,7), (3,1), (3,4), (3,7), (6,1) , (6,4) 和 (6,7)。马上,我不认为这是你的本意。据我所知,您正在尝试检查玩家是否连续有两个标记。

这个问题很容易解决——你不需要两个for循环。相反,只需使用tt+1t+2

接下来,考虑一行:

0 1 2

需要检查三个条件,玩家在 0 和 1、0 和 2 或 1 和 2 处有标记。您只需检查其中两个条件 - 0 和 1,以及 1 和 2。

此外,该if声明并没有做你认为它正在做的事情:

if ... and slot[y+1] != user_team and computer_team:

这相当于:

if ... and (slot[y+1] != user_team) and computer_team:

我假设computer_team'x'or 'o',在这种情况下,python 会像Trueif语句中一样使用它。你想要的是这样的:

if ... and (slot[y+1] != user_team) and (slot[y+1] != computer_team):

这也可能是您的代码只能工作一次的原因 - 下次它去评估它之前找到的同一行或列时,该if语句将True再次评估,并且它将再次设置相同的空间,这在您看来好像它什么也没做。

您检查列的代码有同样的问题。希望我指出的问题足以让您弄清楚如何修复您的代码。祝你好运!

于 2013-10-04T22:05:29.533 回答