from random import choiceimport numpy as npcombo_indices = [ [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]EMPTY_SIGN = '.'AI_SIGN = 'X'OPPONENT_SIGN = 'O'def all_moves_from_board(board, sign): move_list = [] for i,v in enumerate(board): if v == EMPTY_SIGN: move_list.append(board[:i] + sign + board[i+1:]) return move_listdef game_won_by(board): for index in combo_indices: if board[index[0]] == board[index[1]] == board[index[2]] != EMPTY_SIGN: return board[index[0]] return EMPTY_SIGNdef filter_wins(move_list, ai_wins, opponent_wins): for board in move_list: won_by = game_won_by(board) if won_by == AI_SIGN: ai_wins.append(board) move_list.remove(board) elif won_by == OPPONENT_SIGN: opponent_wins.append(board) move_list.remove(board) def init_utility_matrix(board): return [0 if cell == EMPTY_SIGN else -1 for cell in board]def generate_add_score(utilities, i , j, k): def add_score(points): if utilities[i] >= 0: utilities[i] += points if utilities[j] >= 0: utilities[j] += points if utilities[k] >= 0: utilities[k] += points return add_scoredef utility_matrix(board): utilities = init_utility_matrix(board) for [i,j,k] in combo_indices: add_score = generate_add_score(utilities, i, j, k) triple = [ board[i], board[j], board[k]] if triple.count(EMPTY_SIGN) == 1: if triple.count(AI_SIGN) == 2: add_score(1000) elif triple.count(OPPONENT_SIGN) == 2: add_score(100) elif triple.count(EMPTY_SIGN) == 2 and triple.count(AI_SIGN) == 1: add_score(10) elif triple.count(EMPTY_SIGN) == 3: add_score(1) return utilitiesdef best_moves_from_board(board, sign): move_list =[] utilities = utility_matrix(board) max_utility = max(utilities) for i,v in enumerate(board): if utilities[i] == max_utility: move_list.append(board[:i] + sign + board[i+1:]) return move_listdef all_moves_from_board_list(board_list, sign): move_list = [] get_moves = best_moves_from_board if sign == AI_SIGN else all_moves_from_board for board in board_list: move_list.extend(get_moves(board, sign)) return move_list def count_possibilities(): board = EMPTY_SIGN*9 move_list = [board] ai_wins = [] opponent_wins = [] for i in range(9): print(' step ' + str(i) + '. Moves: ' + str(len(move_list))) sign = AI_SIGN if i%2==0 else OPPONENT_SIGN move_list = all_moves_from_board_list(move_list, sign) filter_wins(move_list, ai_wins, opponent_wins) print('First player wins: ' + str(len(ai_wins))) print('Second player wins: ' + str(len(opponent_wins))) print('Draw: ' + str(len(move_list))) print('Total: ' + str(len(ai_wins) + len(opponent_wins) + len(move_list) ))count_possibilities()
这是我上面的代码…
我遇到了这个错误:-
IndexError Traceback (most recent call last)Input In \[3\], in \<cell line: 107\>()103 print('Draw: ' + str(len(move_list)))104 print('Total: ' + str(len(ai_wins) + len(opponent_wins) + len(move_list) ))\--\> 107 count_possibilities()Input In \[3\], in count_possibilities()98 sign = AI_SIGN if i%2==0 else OPPONENT_SIGN99 move_list = all_moves_from_board_list(move_list, sign)\--\> 100 filter_wins(move_list, ai_wins, opponent_wins)101 print('First player wins: ' + str(len(ai_wins)))102 print('Second player wins: ' + str(len(opponent_wins)))Input In \[3\], in filter_wins(move_list, ai_wins, opponent_wins)34 def filter_wins(move_list, ai_wins, opponent_wins):35 for board in move_list:\---\> 36 won_by = game_won_by(board)37 if won_by == AI_SIGN:38 ai_wins.append(board)Input In \[3\], in game_won_by(board)27 def game_won_by(board):28 for index in combo_indices:\---\> 29 if board\[index\[0\]\] == board\[index\[1\]\] == board\[index\[2\]\] != EMPTY_SIGN:30 return board\[index\[0\]\]31 return EMPTY_SIGN
IndexError: 字符串索引超出范围
在调用game_won_by函数并检查索引0,1,2的if状态时,显示字符串索引超出范围…
请帮助我…
错误解决方案:字符串索引超出范围
回答:
在函数all_moves_from_board
中,我认为应该是
move_list = [] for i, v in enumerate(board): if v == EMPTY_SIGN: move_list.append(board[:i] + sign + board[i + 1:]) return move_list
你不小心将board的第一个切片索引写成了1。我没有检查代码是否按预期工作,但现在它可以运行并生成输出:
step 0. Moves: 1 step 1. Moves: 1 step 2. Moves: 8 step 3. Moves: 24 step 4. Moves: 144 step 5. Moves: 83 step 6. Moves: 214 step 7. Moves: 148 step 8. Moves: 172First player wins: 504Second player wins: 12Draw: 91Total: 607