问题描述
嗨,我在大学里用 Python 学习 AI,我的 Connect 4 游戏代码有问题。 我使用了极大极小和 alphaBetaPruning 算法。
我的程序运行,但是当轮到人时,当我想在一个已经有 3 个筹码的列中添加一个筹码时,我收到一个错误:“TypeError:列表索引必须是整数或切片,而不是 nonetype”
def make_move(s,r,c):
s[0][r][c] = s[2] # marks the board =>the line of the error
s[3] -= 1 # one less empty cell
s[2] = COmpuTER + HUMAN - s[2] # switches turns
if winning_move(s,HUMAN):
s[1] = LOSS
return
if winning_move(s,COmpuTER):
s[1] = VIC
return
else:
# my heuristic
threesH = check_threes(s,HUMAN) * 1000
twosH = check_twos(s,HUMAN) * 10
threesC = check_threes(s,COmpuTER) * 1000
twosC = check_twos(s,COmpuTER) * 10
scores = threesH + twosH - threesC - twosC
s[1] += scores
if s[3] == 0:
s[1] = TIE
def getNext(s):
valid_locations = []
for col in range(COLUMN_COUNT):
if is_valid_location(s,col):
tmp = copy.deepcopy(s)
r=get_next_open_row(s,col)
make_move(tmp,col)
valid_locations += [tmp]
return valid_locations
我想也许函数 get_next_open_row 为 r 返回了错误的值。
def get_next_open_row(s,col):
for r in range(ROW_COUNT-1,-1):
if s[0][r][col] == 0:
return r
游戏状态由 4 个项目的列表表示:
感谢您的帮助和时间:-)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)