gpt4 book ai didi

Python:连接4播放

转载 作者:行者123 更新时间:2023-12-01 03:00:21 24 4
gpt4 key购买 nike

嗨,我是 python 新手,我正在创建一个 connect 4 游戏,我正在最后一点,即允许玩游戏的 play 函数。问题似乎是我的 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

最佳答案

只要 who 不等于“computer”,循环就会继续。但循环内的任何内容都不会改变 who,因此它永远不会等于“computer”,循环也永远不会完成。

关于Python:连接4播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43910152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com