Python:连接4播放

时间:2017-05-11 08:23:34

标签: python

你好我是python的新手,我正在创建一个连接4游戏,我在最后一点,播放功能,允许游戏播放。问题似乎是我的while循环,它运行一次,询问用户一个列并输入该列中的计数器,但之后它只是继续询问选择哪一列而不是更改板:

NameError

你可以看到我运行这个代码时得到:

who = game['who']
    while who != 'computer':
        x = int(input("Which column to select? "))
        game['board'] = board2
        l = getValidMoves(game['board'])
        if x in l:
            game['board'] = makeMove(board2, x, who)
            printBoard(game['board'])            
            board = game['board']
            if hasWon(game['board'], who) == True:                        
                print("{who} has won.")
                sys.exit()

我认为问题是在询问要选择哪个列之后,while循环正在停止运行。任何帮助将不胜感激。

我的printBoard功能是:

Which column to select? 1
|1|2|3|4|5|6|7|
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| |X| | | | | |

Which column to select? 2

Which column to select?

我的getValidMoves是:

print("|1|2|3|4|5|6|7|")
    print("---------------")        
    for j in range(6):
        for i in range(7):
            if board[j][i] == 1:
                board[j][i] = "X"
            elif board[j][i] == 2:
                board[j][i] = "O"
            elif board[j][i] == 0:
                board[j][i] = " "                           
    for j in range(6):
        print("|"+"|".join(str(board[j][i]) for i in range(7))+"|")   
    return None

我的makeMove是:

l = list()
for i in range(7):
    if board[0][i] == 0:
        l.append(i)   
return l
enter code here

1 个答案:

答案 0 :(得分:2)

只要who不等于“计算机”,您的循环就会继续。但循环中的任何内容都不会发生变化who,因此永远不会等于“计算机”,循环将永远不会完成。