“连接4”代码似乎检测不到游戏pi

2024-04-19 00:16:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python开发一个“connect4”游戏,我的代码似乎没有检测到游戏块的存在。我不明白为什么。我能想到的唯一一件事是,我的代码在某个地方得到了这样一个想法:尽管显示为已更改,但仍然有一个“O”。你知道吗

import random

a = ["A", "O", "O", "O", "O", "O", "O", "O"]
b = ["B", "O", "O", "O", "O", "O", "O", "O"]
c = ["C", "O", "O", "O", "O", "O", "O", "O"]
d = ["D", "O", "O", "O", "O", "O", "O", "O"]
e = ["E", "O", "O", "O", "O", "O", "O", "O"]
f = ["F", "O", "O", "O", "O", "O", "O", "O"]
n = [" ", "1", "2", "3", "4", "5", "6", "7"]

play = True
turn = random.randint(0, 1)

while play:
    print a
    print b
    print c
    print d
    print e
    print f
    print n

    turn = 0

    if turn == 0:
        playerChoice = int(raw_input("Enter the column you'd like to drop your piece in:"))
        if f[playerChoice] == "O":
            f[playerChoice] = "@"
        elif f[playerChoice] != "O":
            if e[playerChoice] == "0":
                e[playerChoice] = "@"
            elif e[playerChoice] != "O":
                if d[playerChoice] == "O":
                     d[playerChoice] = "@"
                elif d[playerChoice] != "O":
                     if c[playerChoice] == "O":
                         c[playerChoice] = "@"
                     elif c[playerChoice] != "O":
                         if b[playerChoice] == "O":
                             b[playerChoice] = "@"
                         elif b[playerChoice] != "0":
                             if a[playerChoice] == "O":
                                 a[playerChoice] = "@"
                             elif a[playerChoice] != "O":
                                print "This column is full!"
                                play = False



raw_input("press a key to continue")

输出结果如下:

['A', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['B', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['C', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['D', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['E', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['F', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
[' ', '1', '2', '3', '4', '5', '6', '7']
Enter the column you'd like to drop your piece in:1
['A', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['B', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['C', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['D', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['E', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['F', '@', 'O', 'O', 'O', 'O', 'O', 'O']
[' ', '1', '2', '3', '4', '5', '6', '7']
Enter the column you'd like to drop your piece in:1
['A', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['B', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['C', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['D', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['E', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['F', '@', 'O', 'O', 'O', 'O', 'O', 'O']
[' ', '1', '2', '3', '4', '5', '6', '7']

Tags: thetoyouplayyourpieceifcolumn
1条回答
网友
1楼 · 发布于 2024-04-19 00:16:16

有两个条件是0(数字0),而不是O(字母O)。你知道吗

if e[playerChoice] == "0":
....
elif b[playerChoice] != "0":

尽管如此,我不明白为什么你的逻辑不能是:

playerChoice = int(raw_input("Enter the column you'd like to drop your piece in:"))

if f[playerChoice] == "O":
    f[playerChoice] = "@"
elif e[playerChoice] == "O":
    e[playerChoice] = "@"
elif d[playerChoice] == "O":
    d[playerChoice] = "@"
elif c[playerChoice] == "O":
    c[playerChoice] = "@"
elif b[playerChoice] == "O":
    b[playerChoice] = "@" 
elif a[playerChoice] == "O":
    a[playerChoice] = "@" 
else 
    print "This column is full!"

相关问题 更多 >