
五子棋
想实现机械对弈,但是 else后面一直报错。拿ai问显示不在一个棋盘里面。但是报错显示列表超了。
关注引自免费微信小程序:皆我百晓生
在您提供的代码片段中,存在几个错误,导致程序出错。下面是修改后的代码,并解释了每个更改的原因:
# 假设 che_width 和 is_Gaover 已经定义
while is_game:
if step_count % 2: # 判断当前是白子(玩家)还是黑子(AI)走棋
P.ai = AIO
else: # 这里应该使用elif而不是else,因为上一行已经处理了步数为偶数的情况
P.ai2 = AIO
if P.ai in aipe_list or P.ai2 in aipe_list: # 同时检查两个AI的选择是否正确
message = Text(Point(300, 300), 'AI gets a wrong next step.')
message.draw(gw)
is_game = False
else:
# 确保添加正确的AI选择到对应列表
if step_count % 2:
ai_list.append(P.ai)
else:
ai2_list.append(P.ai2)
# 将AI的选择加入错误列表,以便后续不再重复
aipe_list.append(P.ai if step_count % 2 else P.ai2)
piece = Circle(Point(che_width * P.ai[0], che_width * P.ai[1]), 16) if step_count % 2 else Circle(Point(che_width * P.ai2[0], che_width * P.ai2[1]), 16)
piece.setFill('white' if step_count % 2 else 'black')
piece.draw(gw)
# 检查游戏结束条件,注意列表名是否正确
if is_Gaover(ai_list if step_count % 2 else pers_list):
if step_count % 2:
message = Text(Point(100, 100), 'AI白子赢了.')
else:
message = Text(Point(100, 100), '你的黑子赢了.')
message.draw(gw)
is_game = False
step_count += 1
更改点包括:
else 语句,将其替换为 elif,因为之前已经处理了奇数步的情况。P.ai 和 P.ai2 是否在 aipe_list 中。ai_list 或者 ai2_list)。ai_list 或者 pers_list)。这些更改应该解决了您的问题。请确保 che_width 和 is_Gaover 函数已经被正确定义。如果还有其他问题,请提供关于 che_width 和 is_Gaover 的更多信息,或者描述具体错误消息,以便我能进一步帮助您。