def evaluate(data): if utct.winner(data['main_board']) == data['next_move'] : return win elif utct.winner(data['main_board']) != data['next_move'] : return -win else: ret_val = 0 for i in range(9): if data['main_board'][i] != utct.TIE and data['main_board'][i] != EMPTY_VALUE: if data['main_board'][i] != data['next_move']: ret_val -= 10 print 'retval -10' elif data['main_board'][i] == data['next_move']: ret_val += 10 print 'retval -10 ' return ret_val
def play_one_match(): main_board = [utct.EMPTY_VALUE for _ in xrange(9)] boards = [[utct.EMPTY_VALUE for _ in xrange(9)] for _ in xrange(9)] next_board = False while utct.winner(main_board) is False: data = { 'main_board': main_board, 'boards': boards, 'next_move': utct.PLAYER_X, 'next_board': next_board } bot1_move = bot1.calculate_next_move(data) if bot1_move is None: break boards[bot1_move['main_board']][bot1_move['boards']] = utct.PLAYER_X next_board = bot1_move['main_board'] if utct.winner(boards[next_board]) is not False: main_board[bot1_move['main_board']] = utct.winner(boards[bot1_move['main_board']]) next_board = False data = { 'main_board': main_board, 'boards': boards, 'next_move': utct.PLAYER_Y, 'next_board': next_board } bot2_move = bot2.calculate_next_move(data) if bot2_move is None: break boards[bot2_move['main_board']][bot2_move['boards']] = utct.PLAYER_Y next_board = bot2_move['main_board'] if utct.winner(boards[next_board]) is not False: main_board[bot2_move['main_board']] = utct.winner(boards[bot2_move['main_board']]) next_board = False return utct.winner(main_board)
def calculate_next_move(data): pprint(data); ''' We are using monte carlo method for calculating best next move. Number of simulations is defined by NUMBER_OF_SIMULATIONS constant. Each starting move has a winning percentage defined, winning percentage is calculated as: (number_of_winning_moves * 2 + number_of_tying_moves) / (number_of_winning_moves * 2 + number_of_typing_moves + number_of_losing_moves) Initialy, each move as a winning percentage of 0.5. ''' player = data['next_move'] winning_moves = [[0 for _ in xrange(9)] for _ in xrange(9)] tying_moves = [[1 for _ in xrange(9)] for _ in xrange(9)] losing_moves = [[1 for _ in xrange(9)] for _ in xrange(9)] game_over = False for i in xrange(NUMBER_OF_SIMULATIONS): if game_over is True: break next_board = data['next_board'] main_board = copy.deepcopy(data['main_board']) boards = copy.deepcopy(data['boards']) on_move = data['next_move'] # we are caculating success for next move next_main_board_move = None next_boards_move = None if utct.winner(main_board) is not False: game_over = True break # until we have a winner while utct.winner(main_board) is False: # generate a random move if there is no restriction on next board if next_board is not False: main_board_move = next_board else: main_board_move = utct.get_rand_move(main_board) winning_move = utct.get_winning_move(boards[main_board_move], on_move) if winning_move == None: boards_move = utct.get_rand_move(boards[main_board_move]) else: boards_move = winning_move boards[main_board_move][boards_move] = on_move next_board = boards_move # if this value is not set, this is our next move if next_main_board_move == None: next_main_board_move = main_board_move next_boards_move = boards_move # if we have a winner or a tie mark it on main board if utct.winner(boards[main_board_move]) is not False: main_board[main_board_move] = utct.winner(boards[main_board_move]) # next player move on_move = utct.PLAYER_X if on_move == utct.PLAYER_Y else utct.PLAYER_Y winning_player = utct.winner(main_board) if winning_player == player: winning_moves[next_main_board_move][next_boards_move] += 1; elif winning_player == utct.TIE: tying_moves[next_main_board_move][next_boards_move] += 1; else: losing_moves[next_main_board_move][next_boards_move] += 1; if game_over is True: return winning_percentages = calculate_winning_percentages(winning_moves, tying_moves, losing_moves) best_move = find_best_move(winning_percentages) print "Best move: ", best_move return best_move
def calculate_next_move(data): player = data['next_move'] winning_moves = [[0 for _ in xrange(9)] for _ in xrange(9)] losing_moves = [[0 for _ in xrange(9)] for _ in xrange(9)] game_over = False for i in xrange(NUMBER_OF_SIMULATIONS): if game_over is True: break next_board = data['next_board'] main_board = copy.deepcopy(data['main_board']) boards = copy.deepcopy(data['boards']) on_move = data['next_move'] # we are caculating success for next move next_main_board_move = None next_boards_move = None if utct.winner(main_board) is not False: game_over = True break # until we have a winner while utct.winner(main_board) is False: # generate a random move if there is no restriction on next board if next_board is not False: main_board_move = next_board else: main_board_move = utct.get_rand_move(main_board) boards_move = utct.get_rand_move(boards[main_board_move]) boards[main_board_move][boards_move] = on_move next_board = boards_move # if this value is not set, this is our next move if next_main_board_move == None: next_main_board_move = main_board_move next_boards_move = boards_move # if we have a winner or a tie mark it on main board if utct.winner(boards[main_board_move]) is not False: main_board[main_board_move] = utct.winner(boards[main_board_move]) # next player move on_move = utct.PLAYER_X if on_move == utct.PLAYER_Y else utct.PLAYER_Y winning_player = utct.winner(main_board) # TODO keep track of ties and use them in some kind of evaulation function (for final result) if winning_player == player: winning_moves[next_main_board_move][next_boards_move] += 1; else: losing_moves[next_main_board_move][next_boards_move] += 1; if game_over is true: return winning_percentages = [[0 for _ in xrange(9)] for _ in xrange(9)] # win percentage = (number of winning moves)/(total number of moves) for main_board in xrange(9): for board in xrange(9): if winning_moves[main_board][board] > 0: winning_percentages[main_board][board] = float(winning_moves[main_board][board]) \ / float(winning_moves[main_board][board] + losing_moves[main_board][board]) else: winning_percentages[main_board][board] = 0 # findes index of a maximum element in a list def max_index_in_list(a): return max(enumerate(a),key=lambda x: x[1])[0] best_moves = [] best_move_values = [] # we find the best and their values move in each grid for boards in winning_percentages: best_move = max_index_in_list(boards) best_moves.append(best_move) best_move_values.append(boards[best_move]) best_big_move = max_index_in_list(best_move_values) best_move = { 'main_board': best_big_move, 'boards': best_moves[best_big_move] } if no_send is False: send_move(best_move['main_board'], best_move['boards'] , data) return best_move
def undo_move(main_board_move, boards_move, data): data['boards'][main_board_move][boards_move] = utct.EMPTY_VALUE data['main_board'][main_board_move] = utct.EMPTY_VALUE if utct.winner(data['boards'][main_board_move]) is False else utct.winner(data['boards'][main_board_move]) data['next_board'] = main_board_move data['next_move'] = utct.PLAYER_X if data['next_move'] == utct.PLAYER_Y else utct.PLAYER_Y return data
def calculate_next_move(data): player = data['next_move'] winning_moves = [[0 for _ in xrange(9)] for _ in xrange(9)] losing_moves = [[0 for _ in xrange(9)] for _ in xrange(9)] game_over = False for i in xrange(NUMBER_OF_SIMULATIONS): if game_over is True: break next_board = data['next_board'] main_board = copy.deepcopy(data['main_board']) boards = copy.deepcopy(data['boards']) on_move = data['next_move'] # we are caculating success for next move next_main_board_move = None next_boards_move = None if utct.winner(main_board) is not False: game_over = True break # until we have a winner while utct.winner(main_board) is False: # generate a random move if there is no restriction on next board if next_board is not False: main_board_move = next_board else: main_board_move = utct.get_rand_move(main_board) boards_move = utct.get_rand_move(boards[main_board_move]) boards[main_board_move][boards_move] = on_move next_board = boards_move # if this value is not set, this is our next move if next_main_board_move == None: next_main_board_move = main_board_move next_boards_move = boards_move # if we have a winner or a tie mark it on main board if utct.winner(boards[main_board_move]) is not False: main_board[main_board_move] = utct.winner( boards[main_board_move]) # next player move on_move = utct.PLAYER_X if on_move == utct.PLAYER_Y else utct.PLAYER_Y winning_player = utct.winner(main_board) # TODO keep track of ties and use them in some kind of evaulation function (for final result) if winning_player == player: winning_moves[next_main_board_move][next_boards_move] += 1 else: losing_moves[next_main_board_move][next_boards_move] += 1 if game_over is true: return winning_percentages = [[0 for _ in xrange(9)] for _ in xrange(9)] # win percentage = (number of winning moves)/(total number of moves) for main_board in xrange(9): for board in xrange(9): if winning_moves[main_board][board] > 0: winning_percentages[main_board][board] = float(winning_moves[main_board][board]) \ / float(winning_moves[main_board][board] + losing_moves[main_board][board]) else: winning_percentages[main_board][board] = 0 # findes index of a maximum element in a list def max_index_in_list(a): return max(enumerate(a), key=lambda x: x[1])[0] best_moves = [] best_move_values = [] # we find the best and their values move in each grid for boards in winning_percentages: best_move = max_index_in_list(boards) best_moves.append(best_move) best_move_values.append(boards[best_move]) best_big_move = max_index_in_list(best_move_values) best_move = { 'main_board': best_big_move, 'boards': best_moves[best_big_move] } if no_send is False: send_move(best_move['main_board'], best_move['boards'], data) return best_move