【发布时间】:2016-01-28 02:13:50
【问题描述】:
所以,在我正在开发的一款井字游戏中,我决定让 AI 成为一个随机数,开个玩笑。具体如下:
board = []
def createboard():
board.append(['_',' ','_',' ','_'])
board.append(['_',' ','_',' ','_'])
board.append(['_',' ','_',' ','_'])
...
while bot_move_val == False or bot_move_made == False:
bot_move_row = 0
bot_move_col = 0
bot_move_row_rand = randint(0,3)
bot_move_col_rand = randint(0,3)
if bot_move_row_rand == 1:
bot_move_row = 0
bot_move_made = True
elif bot_move_row_rand == 2:
bot_move_row = 2
bot_move_made = True
elif bot_move_row_rand == 3:
bot_move_row = 4
bot_move_made = True
if bot_move_col_rand == 1:
bot_move_col = 0
bot_move_made = True
elif bot_move_col_rand == 2:
bot_move_col = 2
bot_move_made = True
elif bot_move_col_rand == 3:
bot_move_col = 4
bot_move_made = True
if bot_move_row > 4 or bot_move_row < 0 or bot_move_col > 4 or bot_move_col < 0 or board[bot_move_row][bot_move_col] == 'X':
break
else:
bot_move_val = True
代码产生这个错误:
Traceback (most recent call last):
File "C:\Users\[DATA EXPUNGED]\gamestravaganza.py", line 250, in <module>
if bot_move_row > 4 or bot_move_row < 0 or bot_move_col > 4 or bot_move_col < 0 or board[bot_move_row][bot_move_col] == 'X':
IndexError: list index out of range
据我所知,bot_move_row 和 bot_move_col 不在列表 board 中,尽管它们最多与定义的列表一样长。
【问题讨论】:
-
不应该是
2而不是4吗?你有 3 行/列:0、1、2。