嗨,我是python的新手,我正在创建一个连接4的游戏,我在最后一位,播放功能,允许游戏被玩。问题似乎出在我的while循环上,它运行一次,要求用户输入一列,并在该列中输入计数器,但之后它只会继续询问选择哪一列,而不更改面板:
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()你可以看到,当我运行这段代码时,我得到:
Which column to select? 1
|1|2|3|4|5|6|7|
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| |X| | | | | |
Which column to select? 2
Which column to select?我认为问题在于while循环在询问要选择哪一列之后停止运行。任何帮助都将不胜感激。
我的printBoard函数是:
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我的getValidMoves是:
l = list()
for i in range(7):
if board[0][i] == 0:
l.append(i)
return l
enter code here我的makeMove是:
if who == 1:
for i in [5,4,3,2,1,0]:
if board[i][move] == 0:
break
board[i][move] = 1
elif who == 2:
for i in [5,4,3,2,1,0]:
if board[i][move] == 0:
break
board[i][move] = 2
return board发布于 2017-05-11 16:29:40
只要who不等于"computer“,您的循环就会继续。但是循环中的任何东西都不会改变who,所以它永远不会等于"computer“,循环也永远不会完成。
https://stackoverflow.com/questions/43910152
复制相似问题