def smartPlays(game, tiles, player): player /= 2 curr_game = game[player] other_game = game[1 - player] actions = curr_game.possible_actions(0) if len(actions) == 1: curr_game.update(actions[0][0]) other_game.update(actions[0][0]) print "I played a " + str(actions[0][0]) + ", yay!" if not actions[0][0] == PASS_DOMINO: tiles[2 * player].remove(actions[0][0]) else: pnm = ProbabilisticNegaMax(curr_game) depth = int(5 * (2**(1. / 3 * int(len(curr_game.dominos_played) / 4)))) print "DEPTH" print depth max_move, max_score = pnm.p_negamax_ab(depth, depth, -float("inf"), float("inf"), 0) # max_move, max_score = pnm.p_negamax(6,0) curr_game.update(max_move[0], placement=max_move[1]) other_game.update(max_move[0], placement=max_move[1]) print "I played a " + str(max_move[0]) + ", yay!" if not max_move[0] == PASS_DOMINO: tiles[2 * player].remove(max_move[0]) return tiles
def smartPlays(game): actions = game.possible_actions(0) if len(actions) == 1: game.update(actions[0][0]) print "I played a " + str(actions[0][0]) + ", yay!" else: pnm = ProbabilisticNegaMax(game) max_move, max_score = pnm.p_negamax(5, 0) game.update(max_move[0], placement=max_move[1]) print "I played a " + str(max_move[0]) + ", yay!"
def smartPlays(game, player): player /= 2 curr_game = game[player] other_game = game[1-player] actions = curr_game.possible_actions(0) if len(actions) == 1: curr_game.update(actions[0][0]) other_game.update(actions[0][0]) print "I played a " + str(actions[0][0]) + ", yay!" else: pnm = ProbabilisticNegaMax(curr_game) depth = int(6*(2**(1./3*int(len(curr_game.dominos_played)/4)))) print "DEPTH" print depth max_move, max_expectation = None, None for a in actions: curr_expectation = calculate_expectation(curr_game, depth, a) if max_move is None or max_expectation < curr_expectation: max_move, max_expectation = a, curr_expectation print 'new max found with expectation : {}'.format(max_expectation) # max_move, max_score = pnm.p_negamax(6,0) curr_game.update(max_move[0], placement=max_move[1]) other_game.update(max_move[0], placement=max_move[1]) print "I played a " + str(max_move[0]) + ", yay!"
def smartPlays(game, tiles): actions = game.possible_actions(1) if len(actions) == 1: game.update(actions[0][0]) # print "I played a " + str(actions[1][0]) + ", yay!" else: pnm = ProbabilisticNegaMax(game) depth = int(5 * (2**(1. / 3 * int(len(game.dominos_played) / 4)))) print "DEPTH" print depth max_move, max_score = pnm.p_negamax_ab(depth, depth, -float("inf"), float("inf"), 1) # max_move, max_score = pnm.p_negamax(7,0) game.update(max_move[1], placement=max_move[1]) # print "I played a " + str(max_move[1]) + ", yay!" return tiles
def smartPlays(game, tiles, player): player /= 2 curr_game = game[player] other_game = game[1 - player] actions = curr_game.possible_actions(0) if len(actions) == 1: curr_game.update(actions[0][0]) other_game.update(actions[0][0]) print "I played a " + str(actions[0][0]) + ", yay!" if not actions[0][0] == PASS_DOMINO: tiles[2 * player].remove(actions[0][0]) else: pnm = ProbabilisticNegaMax(curr_game) depth = int(5 * (2**(1. / 3 * int(len(curr_game.dominos_played) / 4)))) print "DEPTH" print depth # uncomment out the line below for oldSmartPlayer: # max_move, max_score = pnm.p_negamax_ab(depth, depth, -float("inf"), float("inf"), 0) # comment out up to after for loop for newSmartPlayer max_move, max_expectation = None, None for a in actions: curr_expectation = calculate_expectation(curr_game, depth, a) if max_move is None or max_expectation < curr_expectation: max_move, max_expectation = a, curr_expectation print 'new max found with expectation : {}'.format( max_expectation) # max_move, max_score = pnm.p_negamax(6,0) curr_game.update(max_move[0], placement=max_move[1]) other_game.update(max_move[0], placement=max_move[1]) print "I played a " + str(max_move[0]) + ", yay!" if not max_move[0] == PASS_DOMINO: tiles[2 * player].remove(max_move[0]) return tiles
def newSmartPlays(game, tiles, player): curr_game = game[player] actions = curr_game.possible_actions(0) if len(actions) == 1: for g in games: g.update(actions[0][0]) print "I played a " + str(actions[0][0]) + ", yay!" if not actions[0][0] == PASS_DOMINO: tiles[player].remove(actions[0][0]) else: pnm = ProbabilisticNegaMax(curr_game) depth = int(5*(2**(1./3*int(len(curr_game.dominos_played)/4)))) print "DEPTH" print depth max_move, max_expectation = None, None for a in actions: curr_expectation = calculate_expectation(curr_game, depth, a) if max_move is None or max_expectation < curr_expectation: max_move, max_expectation = a, curr_expectation print 'new max found with expectation : {}'.format(max_expectation) # max_move, max_score = pnm.p_negamax(6,0) for g in games: g.update(max_move[0], placement=max_move[1]) print "I played a " + str(max_move[0]) + ", yay!" if not max_move[0] == PASS_DOMINO: tiles[player].remove(max_move[0]) return tiles
def calculate_expectation(game, depth, move, samples=40): exp_total = 0.0 remaining_dominoes = make_dominoes() players = range(4) pnm = ProbabilisticNegaMax(game) game.make_probabilistic_move(0, move) for t in game.dominos_played: if not t == PASS_DOMINO: remaining_dominoes.remove(t) for _ in range(samples): curr_dominoes = list(remaining_dominoes) random.shuffle(curr_dominoes) old_probabilities = deepcopy(game.probabilities) while curr_dominoes: curr_domino = curr_dominoes.pop() curr_domino_probs = game.probabilities[curr_domino] curr_assignment = choice(players, p=curr_domino_probs) game._update_probs(curr_domino, curr_assignment) exp_total += -pnm.p_negamax_ab(depth, depth, -float('inf'), float('inf'), 1)[1] game.probabilities = old_probabilities game.undo_move(0, move) exp_total /= samples return exp_total
''' Just for simulating a game and checking that I am not a total mess ''' game_tiles = [] for i in range(7): for j in range(i, 7): game_tiles.append((i, j)) my_tiles_input = ['12', '34', '22', '00', '55', '33', '23'] my_tiles = [] for t in my_tiles_input: my_tiles.append(tuple([int(num) for num in t])) starter = 3 start_tile = (6, 6) test = Dominoes(game_tiles, my_tiles, starter, start_tile) pnm = ProbabilisticNegaMax(test) max_move, max_score = pnm.p_negamax(6, 0) print max_move # print test.curr_player # c = deepcopy(test) # prob = test.make_probabilistic_move(3, (PASS_DOMINO,None)) # print test.curr_player # prob = test.make_probabilistic_move(0, (Domino(4, 6), None)) # print test.curr_player # test.undo_move(0, (Domino(4,6), None)) # print test.curr_player # test.undo_move(3, (PASS_DOMINO, None)) # print test.curr_player # c.is_equal(test)