def test_other_player_has_more_than_seven(self): player_list = [RandomPlayer(1), RandomPlayer(2), RandomPlayer(3)] for player in player_list: player.resources = {'w': 0, 'b': 0, 'l': 3, 'g': 2, 'o': 0} deck = Deck() board = Board(player_list, False) board.round_num = 3 board.active_player = player_list[0] player_list[1].resources = {'w': 5, 'b': 5, 'l': 3, 'g': 2, 'o': 1} move_made = player_list[0].make_move(Move(Move.ROLL_DICE, roll=7), board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(board.seven_roller, player_list[0]) self.assertEqual(board.active_player, player_list[1]) legal_moves = board.active_player.get_legal_moves(board, deck) for move in legal_moves: self.assertEqual(move.move_type, Move.DISCARD_HALF) decided = board.active_player.decide_move(board, deck, player_list) self.assertEqual(decided.move_type, Move.DISCARD_HALF) move_made = board.active_player.make_move(decided, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(board.seven_roller, None) self.assertEqual(board.active_player, player_list[0]) self.assertNotEqual(player_list[1].resources, { 'w': 5, 'b': 5, 'l': 3, 'g': 2, 'o': 1 }) self.assertEqual(sum(player_list[1].resources.values()), 8)
def graph(subdir, title, file_name, training_games_per_epoch=50, test_games_per_epoch_vs_test_players=500): """ Plots the results of the NFSP models saved in the given subdirectory vs. test cases. :param subdir: subdirectory in which the NFSP models are saved. :param title: figure title. :param file_name: name of file to save the figure. :param training_games_per_epoch: number of training games per epoch used in training of the models. :param test_games_per_epoch_vs_test_players: number of test games for each model against each test case. """ filenames = os.listdir(os.path.join(nfsp_saved_models_dir, subdir)) cumulative_training_games_per_epoch = list(range(training_games_per_epoch, training_games_per_epoch * len(filenames) + 1, training_games_per_epoch)) loss_ratio_vs_1_random = [] loss_ratio_vs_3_random = [] trained = TrainedNFSPPlayer(hand_size, 'trained') random1 = RandomPlayer(hand_size, 'random-1') random2 = RandomPlayer(hand_size, 'random-2') random3 = RandomPlayer(hand_size, 'random-3') for filename in filenames: trained.load_model(os.path.join(subdir, filename)) loss_ratios = do_test_games([trained, random1], test_games_per_epoch_vs_test_players) loss_ratio_vs_1_random.append(loss_ratios[trained.name]) loss_ratios = do_test_games([trained, random1, random2, random3], test_games_per_epoch_vs_test_players) loss_ratio_vs_3_random.append(loss_ratios[trained.name]) learning_player_info = {"VS. 1 random": ( colors[1], loss_ratio_vs_1_random), "VS. 3 randoms": ( colors[2], loss_ratio_vs_3_random)} plot(cumulative_training_games_per_epoch, learning_player_info, title, "Number of training games", "Loss Ratio", True) plt.savefig(file_name)
def test_did_it_play(self): player = RandomPlayer() board = Board() numberOfNullsBeforePlay = len(board.get_empty_cells()) board = player.random_move(board, Role.X) numberOfNullsAfterPlay = len(board.get_empty_cells()) self.assertEqual(numberOfNullsBeforePlay, numberOfNullsAfterPlay-1)
def train_against_prev_iter(subdir, epochs=100, training_games_per_epoch=50): """ Trains a NFSP agent against a previous iteration of itself. :param subdir: subdirectory in which to save the models while training. :param epochs: number of training epochs. :param training_games_per_epoch: number of training games per epoch. """ full_subdir_path = os.path.join(nfsp_saved_models_dir, subdir) if os.path.exists(full_subdir_path): shutil.rmtree(full_subdir_path) os.mkdir(full_subdir_path) learning_player = NFSPPlayer(hand_size, 'NFSP-PLAYER-1', 'cpu') learning_players = [learning_player] random1 = RandomPlayer(hand_size, 'random-1') training_players = [random1, RandomPlayer(hand_size, 'random-2')] trainer = NFSPTrainer(learning_players, training_players) cumulative_training_games_per_epoch = list(range(training_games_per_epoch, training_games_per_epoch * epochs + 1, training_games_per_epoch)) for epoch in range(epochs): print("------------------------- Epoch", epoch + 1, "out of", epochs, "-------------------------") print("Training for", training_games_per_epoch, "games (total number of training games: " + str(cumulative_training_games_per_epoch[epoch]) + ")") trainer.train(training_games_per_epoch) learning_player.save_network(os.path.join(subdir, 'epoch-' + str(epoch + 1))) prev_iter1 = TrainedNFSPPlayer(hand_size, 'prev-iter1') prev_iter1.load_model(os.path.join(subdir, 'epoch-' + str(random.randint(max(1, epoch-10), epoch + 1)))) training_players = [random1, prev_iter1] trainer = NFSPTrainer(learning_players, training_players)
def playMinimax(self): p1 = RandomPlayer("X") p2 = MinimaxPlayer("O") random.seed() currentPlayer = p1 currentBoard = p1.randomMove( self.board) # p1 makes a random move and updates current board solution = [] while True: solution.append(currentBoard) if currentBoard.winner(): # stop when there's a winner if currentBoard.winner() == "TIE": print(currentBoard.winner()) else: print(currentBoard.winner(), "wins") break else: if currentPlayer.equals(p1): # change turn currentPlayer = p2 currentBoard = currentPlayer.minimaxMove( currentBoard ) # p2 makes a minimax move and updates current board else: # change turn currentPlayer = p1 currentBoard = currentPlayer.randomMove(currentBoard) return solution
def __init__(self, config): super().__init__(config) # initialize the Yaniv game players = [] # if player_config not specified in config, we initialize with two random player if not ('player_config' in config): players = [RandomPlayer("RandomPlayer1"), RandomPlayer("RandomPlayer2")] # otherwise we will find the player_config to generate players else: for i in range(len(config['player_config'])): player_type = config['player_config'][i] if player_type == 'DQN': players.append(RandomPlayer('RDQNPlayer' + str(i))) elif player_type == 'Random': players.append(RandomPlayer('RandomPlayer' + str(i))) elif player_type == 'HC': players.append(HillClimbPlayer('HC' + str(i))) elif player_type == 'Minimax': players.append(MinimaxPlayer('Minimax' + str(i))) elif player_type == 'SA': players.append(SimulatedAnnealingPlayer('SA' + str(i))) self.game = GameEngine(players) self.state_shape = [4, 54]
def __init__(self): wrp = RandomPlayer('w') wsnmp = SimpleNegamax('w') brp = RandomPlayer('b') bsnmp = SimpleNegamax('b') wnab = Negamaxalphabeta('w') bnab = Negamaxalphabeta('b') bbep = Basic('b') wbep = Basic('w') wsf = StockFishEngine('w') bsf = StockFishEngine('b') wmclp = MonteCarloEasy('w') bmclp = MonteCarloEasy('b') wmcp = MonteCarlo('w') bmcp = MonteCarlo('b') self.__players = { "0w": wrp, "1w": wsnmp, "0b": brp, "1b": bsnmp, "2w": wnab, "2b": bnab, "3b": bbep, "3w": wbep, "sfw": wsf, "sfb": bsf, "4w": wmclp, "4b": bmclp, "4.5w": wmcp, "4.5b": bmcp }
def test_cycles_work(self): # Set up a player in an almost win state and see if the cycles can # find the win state. The nnet used should just return a random value # between -1 and 1 class RandomNNet(): def __init__(self): pass def predict(self, canonicalBoard): v = random.uniform(-0.999999999999, 0.999999999999) p = [random.random() for i in range(3151)] p = np.array(p) / sum(p) return p, v player_list = [ MCTSNNPlayer(1, 50, RandomNNet()), RandomPlayer(2), RandomPlayer(3) ] player_list[0].dev_cards[Card.VICTORY_POINT] = 8 player_list[0].resources = {'w': 2, 'b': 4, 'l': 4, 'g': 2, 'o': 0} player_list[0].trades_tried = 2 player_list[0].has_rolled = True deck = Deck() board = Board(player_list, False) board.round_num = 3 player_list[0].make_move(Move(Move.BUY_SETTLEMENT, coord=(0, 1)), board, deck, player_list) player_list[0].make_move( Move(Move.BUY_ROAD, road=frozenset([(0, 1), (1, 1)])), board, deck, player_list) player_list[0].make_move( Move(Move.BUY_ROAD, road=frozenset([(0, 0), (1, 1)])), board, deck, player_list) print(player_list[0].decide_move(board, deck, player_list))
def play_random_game(_): players = [ HumanPlayer("Hans"), RandomPlayer("Opponent1"), RandomPlayer("Teammate"), RandomPlayer("Opponent2") ] game = Game(players) return game.play_game(choice(players)).total_points
def move(self, board): if board.game_over(): return else: random_number = np.random.randint(10) if random_number > 1: score, action = self._max(board) return board.move(action[0], action[1], self.side) randomPlayer = RandomPlayer(side=(self.side)) return randomPlayer.move(board)
def test_longest_path_single_source(self): player_list = [RandomPlayer(1), RandomPlayer(2)] deck = Deck() board = Board(player_list, False) # One road player_list[0].add_road(board, frozenset([(1, 0), (2, 0)])) board.build_road((1, 0), (2, 0), player_list[0]) path, seen, reach = board.DFS((1, 0), player_list[0]) self.assertEqual(len(path), 2) # Branching paths of equal length player_list[0].add_road(board, frozenset([(2, 0), (3, 0)])) board.build_road((2, 0), (3, 0), player_list[0]) player_list[0].add_road(board, frozenset([(2, 0), (3, 1)])) board.build_road((2, 0), (3, 1), player_list[0]) path, seen, reach = board.DFS((1, 0), player_list[0]) self.assertEqual(len(path), 3) # Branching paths where one is longer than other player_list[0].add_road(board, frozenset([(3, 1), (4, 1)])) board.build_road((3, 1), (4, 1), player_list[0]) path, seen, reach = board.DFS((1, 0), player_list[0]) self.assertEqual(len(path), 4) # A cycle in the paths player_list[0].add_road(board, frozenset([(3, 0), (4, 0)])) board.build_road((3, 0), (4, 0), player_list[0]) player_list[0].add_road(board, frozenset([(4, 0), (5, 1)])) board.build_road((4, 0), (5, 1), player_list[0]) player_list[0].add_road(board, frozenset([(4, 1), (5, 1)])) board.build_road((4, 1), (5, 1), player_list[0]) player_list[0].add_road(board, frozenset([(4, 0), (5, 0)])) board.build_road((4, 0), (5, 0), player_list[0]) path, seen, reach = board.DFS((1, 0), player_list[0]) self.assertEqual(len(path), 8) # Another player builds a settlement path, seen, reach = board.longest_road_single_source((2, 0), player_list[0]) self.assertEqual(len(path), 8) player_list[1].settlements.append((2, 0)) board.add_settlement(player_list[1], (2, 0)) path, seen, reach = board.longest_road_single_source((1, 0), player_list[0]) self.assertEqual(len(path), 2) path, seen, reach = board.longest_road_single_source((2, 0), player_list[0]) self.assertEqual(len(path), 7) path, seen, reach = board.longest_road_single_source((2, 0), player_list[1]) self.assertEqual(len(path), 1) self.assertEqual(reach, {(2, 0)})
def test_robber_prevents_resource_allocation(self): player_list = [RandomPlayer(1), RandomPlayer(2)] deck = Deck() board = Board(player_list, False) player_list[0].settlements.append((0, 0)) board.add_settlement(player_list[0], (0, 0)) board.move_robber((0, 0)) board.allocate_resources(11) self.assertEqual(player_list[0].resources['w'], 2) self.assertEqual(player_list[0].resources['o'], 0) self.assertEqual(player_list[0].resources['l'], 4) self.assertEqual(player_list[0].resources['b'], 4) self.assertEqual(player_list[0].resources['g'], 2)
def run_against_3_randoms(params_filename, games): """ Runs test games against 3 random players and returns the loss ration in range [0, 1] :param params_filename: path of the file containing the parameters of the model to test. :param games: number of test games. :return: loss ratio of the player in range [0, 1] """ with tf.compat.v1.Session() as sess: player1 = from_path(sess, DurakEnv.HAND_SIZE, "Trained2_PPO_" + params_filename, os.path.join(ppo_saved_models_dir, params_filename)) player2 = RandomPlayer(DurakEnv.HAND_SIZE, "RandomPlayer1") player3 = RandomPlayer(DurakEnv.HAND_SIZE, "RandomPlayer2") player4 = RandomPlayer(DurakEnv.HAND_SIZE, "RandomPlayer3") loss_ratios = do_test_games([player1, player2, player3, player4], games) return loss_ratios[player1.name] / 100
def test_all_players_more_than_seven(self): player_list = [RandomPlayer(1), RandomPlayer(2), RandomPlayer(3)] for player in player_list: player.resources = {'w': 5, 'b': 5, 'l': 3, 'g': 2, 'o': 1} deck = Deck() board = Board(player_list, False) board.round_num = 3 board.active_player = player_list[0] move_made = player_list[0].make_move(Move(Move.ROLL_DICE, roll=7), board, deck, player_list) # Player 1 now needs to discard half self.assertEqual(move_made, 1) self.assertEqual(board.seven_roller, player_list[0]) self.assertEqual(board.active_player, player_list[0]) decided = board.active_player.decide_move(board, deck, player_list) self.assertEqual(decided.move_type, Move.DISCARD_HALF) move_made = board.active_player.make_move(decided, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(board.seven_roller, player_list[0]) self.assertEqual(board.active_player, player_list[1]) # Player 2 needs to discard half decided = board.active_player.decide_move(board, deck, player_list) self.assertEqual(decided.move_type, Move.DISCARD_HALF) move_made = board.active_player.make_move(decided, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(board.seven_roller, player_list[0]) self.assertEqual(board.active_player, player_list[2]) # Player 3 needs to discard half decided = board.active_player.decide_move(board, deck, player_list) self.assertEqual(decided.move_type, Move.DISCARD_HALF) move_made = board.active_player.make_move(decided, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(board.seven_roller, None) self.assertEqual(board.active_player, player_list[0]) for player in player_list: self.assertEqual(sum(player.resources.values()), 8) # Make sure were at player 1 and they now need to move the robber legal_moves = board.active_player.get_legal_moves(board, deck) for move in legal_moves: self.assertEqual(move.move_type, Move.MOVE_ROBBER)
def test_draw_dev_play_dev_same_turn(self): player_list = [RandomPlayer(1)] deck = Deck() board = Board(player_list, False) board.active_player = player_list[0] board.round_num = 2 player_list[0].resources = {'w': 1, 'b': 0, 'l': 0, 'g': 1, 'o': 1} player_list[0].has_rolled = True # After buying a dev card, we cannot play it. move_made = player_list[0].make_move( Move(Move.BUY_DEV, card_type=0, player=1), board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(player_list[0].dev_drawn, 0) legal_moves = player_list[0].get_legal_moves(board, deck) is_legal = player_list[0].check_legal_move( Move(Move.PLAY_DEV, card_type=0, coord=(0, 0)), board, deck) self.assertEqual(is_legal, False) self.assertEqual(len(legal_moves), 1) self.assertNotEqual(legal_moves[0].move_type, Move.PLAY_DEV) self.assertEqual(player_list[0].dev_cards[0], 1) # Check that if we have another dev card that is the same as the one drawn, # we can play it player_list[0].dev_cards[0] += 1 legal_moves = player_list[0].get_legal_moves(board, deck) self.assertGreater(len(legal_moves), 1) self.assertEqual((legal_moves[1].move_type, legal_moves[1].card_type), (Move.PLAY_DEV, 0)) is_legal = player_list[0].check_legal_move( Move(Move.PLAY_DEV, card_type=0, coord=(0, 0)), board, deck) self.assertEqual(is_legal, True) move_made = player_list[0].make_move( Move(Move.PLAY_DEV, card_type=0, coord=(0, 0)), board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(player_list[0].dev_cards[0], 1) self.assertEqual(player_list[0].dev_played, 1) # Now, end the turn and check that on the next turn, we can play the knight that is left over player_list[0].make_move(Move(Move.END_TURN), board, deck, player_list) legal_moves = player_list[0].get_legal_moves(board, deck) self.assertIn(Move(Move.PLAY_DEV, card_type=0, coord=(1, 0)), legal_moves) self.assertNotIn(Move(Move.PLAY_DEV, card_type=0, coord=(0, 0)), legal_moves) self.assertEqual(player_list[0].dev_drawn, -1) self.assertEqual(player_list[0].dev_played, 0) is_legal = player_list[0].check_legal_move( Move(Move.PLAY_DEV, card_type=0, coord=(1, 0)), board, deck) self.assertEqual(is_legal, True) is_legal = player_list[0].check_legal_move( Move(Move.PLAY_DEV, card_type=0, coord=(0, 0)), board, deck) self.assertEqual(is_legal, False) move_made = player_list[0].make_move( Move(Move.PLAY_DEV, card_type=0, coord=(1, 0)), board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(player_list[0].dev_played, 1)
def test_hexes_created_properly(self): player_list = [RandomPlayer(1)] board = Board(player_list, False) self.assertEqual( { 2: [(4, 1)], 3: [(2, 1), (3, 3)], 4: [(1, 0), (2, 3)], 5: [(1, 2), (4, 0)], 6: [(1, 1), (4, 2)], 7: [], 8: [(2, 4), (3, 0)], 9: [(0, 2), (3, 2)], 10: [(1, 3), (3, 1)], 11: [(0, 0), (2, 2)], 12: [(0, 1)] }, board.hex) board = Board(player_list, True) self.assertEqual(len(board.hex[2]), 1) self.assertEqual(len(board.hex[3]), 2) self.assertEqual(len(board.hex[4]), 2) self.assertEqual(len(board.hex[5]), 2) self.assertEqual(len(board.hex[6]), 2) self.assertEqual(len(board.hex[7]), 0) self.assertEqual(len(board.hex[8]), 2) self.assertEqual(len(board.hex[9]), 2) self.assertEqual(len(board.hex[10]), 2) self.assertEqual(len(board.hex[11]), 2) self.assertEqual(len(board.hex[12]), 1)
def getCorrectPlayer(x, name): if x == "r": return RandomPlayer() elif x == "f": return FinalPlayer( True, name) if name is not None else FinalPlayer(False, name) elif x == "m": return MiniMaxPlayer() elif x == "n": return NaturalPlayer()
def test_correct_possible_trades(self): player_list = [RandomPlayer(1), RandomPlayer(2), RandomPlayer(3)] deck = Deck() board = Board(player_list, False) board.round_num = 2 board.active_player = player_list[0] player_list[0].has_rolled = True player_list[0].resources = {'w': 0, 'b': 1, 'l': 0, 'g': 0, 'o': 0} player_list[1].resources = {'w': 2, 'b': 3, 'l': 3, 'g': 0, 'o': 0} player_list[2].resources = {'w': 1, 'b': 1, 'l': 0, 'g': 1, 'o': 0} legal_moves = player_list[0].get_legal_moves(board, deck) self.assertNotIn( Move(Move.PROPOSE_TRADE, give_resource=('b', 1), resource=('o', 1), player=player_list[0].player_num), legal_moves) self.assertNotIn( Move(Move.PROPOSE_TRADE, give_resource=('b', 1), resource=('w', 3), player=player_list[0].player_num), legal_moves) self.assertNotIn( Move(Move.PROPOSE_TRADE, give_resource=('b', 1), resource=('b', 1), player=player_list[0].player_num), legal_moves) self.assertIn( Move(Move.PROPOSE_TRADE, give_resource=('b', 1), resource=('l', 3), player=player_list[0].player_num), legal_moves) self.assertIn( Move(Move.PROPOSE_TRADE, give_resource=('b', 1), resource=('w', 2), player=player_list[0].player_num), legal_moves) self.assertIn( Move(Move.PROPOSE_TRADE, give_resource=('b', 1), resource=('w', 1), player=player_list[0].player_num), legal_moves)
def test_randomized_deck(self): player_list = [RandomPlayer(1), RandomPlayer(2), RandomPlayer(3)] deck = Deck() board = Board(player_list, False) # Give player 1 some knights for i in range(4): player_list[0].dev_cards[deck.take_card(0)] += 1 # Give player 2 a knight and year of plenty player_list[1].dev_cards[deck.take_card(0)] += 1 player_list[1].dev_cards[deck.take_card(len(deck.cards_left) - 1)] += 1 # Give player 3 victory points for i in range(5): player_list[2].dev_cards[deck.take_card(9)] += 1 self.assertEqual(len(deck.cards_left), 14) self.assertEqual(player_list[0].dev_cards[Card.KNIGHT], 4) self.assertEqual(player_list[1].dev_cards[Card.KNIGHT], 1) self.assertEqual(player_list[1].dev_cards[Card.YEAR_OF_PLENTY], 1) self.assertEqual(player_list[2].dev_cards[Card.VICTORY_POINT], 5) original_deck = list(deck.cards_left) AI = MCTSNN(board, 100, deck, board.active_player.player_num, None, None) # Make sure that the original deck is unchanged self.assertEqual(original_deck, deck.cards_left) # Check to make sure that the randomization left the correct number of cards self.assertEqual(len(original_deck), len(AI.nodes[0].state.deck.cards_left)) self.assertEqual(player_list[0].dev_cards, AI.nodes[0].state.board.players[0].dev_cards) self.assertEqual(sum(player_list[1].dev_cards.values()), \ sum(AI.nodes[0].state.board.players[1].dev_cards.values())) self.assertEqual(sum(player_list[2].dev_cards.values()), \ sum(AI.nodes[0].state.board.players[2].dev_cards.values()))
def setPlayer(self, senkouName, koukouName): if senkouName == u'人間': self.isSenkouHuman = True elif senkouName == u'ランダム君': self.senkouP = RandomPlayer(senkouName, True) elif senkouName == u'強いランダム君': self.senkouP = AlphaRandomPlayer(senkouName, True) # elif senkouName == u'モンテカルロ君': # self.senkouP = MonteCalroPlayer(senkouName,True) elif senkouName == 'DQN': self.senkouP = DQN.DQNPlayer(senkouName, True) if koukouName == u'人間': self.isKoukouHuman = True elif koukouName == u'ランダム君': self.koukouP = RandomPlayer(koukouName, False) elif koukouName == u'強いランダム君': self.koukouP = AlphaRandomPlayer(koukouName, False) # elif koukouName == u'モンテカルロ君': # self.koukouP = MonteCalroPlayer(koukouName,False) elif koukouName == 'DQN': self.koukouP = DQN.DQNPlayer(koukouName, False)
def heuristic_play(self, board, role): """ 1 Do Not Lose 2 Win """ # Closing opponenet's winning moves winning_moves = board.get_winning_cells_for(Role.get_opponent(role)) if len(winning_moves) > 0: i, j = random.sample(winning_moves, 1)[0] board.set_cell(i, j, role) return board # Choosing a heuristic winnning cell winning_moves = board.get_winning_cells_for(role) if len(winning_moves) > 0: i, j = random.sample(winning_moves, 1)[0] board.set_cell(i, j, role) return board # If there is no heuristc win cell; Random play it. randomPlayer = RandomPlayer() return randomPlayer.random_move(board, role)
def test_less_than_seven_cards(self): player_list = [RandomPlayer(1), RandomPlayer(2), RandomPlayer(3)] for player in player_list: player.resources = {'w': 0, 'b': 0, 'l': 3, 'g': 2, 'o': 0} deck = Deck() board = Board(player_list, False) board.round_num = 3 board.active_player = player_list[0] move_made = player_list[0].make_move(Move(Move.ROLL_DICE, roll=7), board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(board.seven_roller, None) self.assertEqual(board.active_player, player_list[0]) self.assertEqual(player_list[0].has_rolled, True) self.assertEqual(player_list[0].resources, { 'w': 0, 'b': 0, 'l': 3, 'g': 2, 'o': 0 }) self.assertEqual(player_list[1].resources, { 'w': 0, 'b': 0, 'l': 3, 'g': 2, 'o': 0 }) self.assertEqual(player_list[2].resources, { 'w': 0, 'b': 0, 'l': 3, 'g': 2, 'o': 0 }) legal_moves = player_list[0].get_legal_moves(board, deck) for move in legal_moves: self.assertEqual(move.move_type, Move.MOVE_ROBBER)
def test_random_board_correct_number_dice_resources_spots(self): trials = [True, False] for i in range(len(trials)): player_list = [RandomPlayer(1)] board = Board(player_list, trials[i]) self.assertIn((0, 0), board.resources) self.assertIn((0, 1), board.resources) self.assertIn((0, 2), board.resources) self.assertIn((1, 0), board.resources) self.assertIn((1, 1), board.resources) self.assertIn((1, 2), board.resources) self.assertIn((1, 3), board.resources) self.assertIn((2, 0), board.resources) self.assertIn((2, 1), board.resources) self.assertIn((2, 2), board.resources) self.assertIn((2, 3), board.resources) self.assertIn((2, 4), board.resources) self.assertIn((3, 0), board.resources) self.assertIn((3, 1), board.resources) self.assertIn((3, 2), board.resources) self.assertIn((3, 3), board.resources) self.assertIn((4, 0), board.resources) self.assertIn((4, 1), board.resources) self.assertIn((4, 2), board.resources) board_items = board.resources.values() resource_count = {"l": 0, "g": 0, "w": 0, "b": 0, "o": 0, "n": 0} number_count = {} for element in board_items: resource_count[element[0]] += 1 if (element[1], element[2]) not in number_count: number_count[(element[1], element[2])] = 1 else: number_count[(element[1], element[2])] += 1 self.assertEqual(resource_count["l"], 4) self.assertEqual(resource_count["g"], 4) self.assertEqual(resource_count["w"], 4) self.assertEqual(resource_count["b"], 3) self.assertEqual(resource_count["o"], 3) self.assertEqual(resource_count["n"], 1) self.assertEqual(number_count[(2, 1)], 1) self.assertEqual(number_count[(-1, 0)], 1) self.assertEqual(number_count[(3, 2)], 2) self.assertEqual(number_count[(4, 3)], 2) self.assertEqual(number_count[(5, 4)], 2) self.assertEqual(number_count[(6, 5)], 2) self.assertEqual(number_count[(8, 5)], 2) self.assertEqual(number_count[(9, 4)], 2) self.assertEqual(number_count[(10, 3)], 2) self.assertEqual(number_count[(11, 2)], 2) self.assertEqual(number_count[(12, 1)], 1)
def test_places_for_road(self): player_list = [RandomPlayer(1)] deck = Deck() board = Board(player_list, False) player_list[0].settlements.append((0, 0)) board.add_settlement(player_list[0], (0, 0)) player_list[0].add_road(board, frozenset([(0, 0), (1, 0)])) board.build_road((0, 0), (1, 0), player_list[0]) board.round_num = 2 board.active_player = player_list[0] player_list[0].has_rolled = True player_list[0].resources = {'w': 0, 'b': 1, 'l': 1, 'g': 0, 'o': 0} legal_moves = player_list[0].get_legal_moves(board, deck) self.assertIn(Move(Move.BUY_ROAD, road=frozenset([(2, 0), (1, 0)])), legal_moves) self.assertIn(Move(Move.BUY_ROAD, road=frozenset([(0, 0), (1, 1)])), legal_moves)
def test_random_ports(self): player_list = [RandomPlayer(1)] board = Board(player_list, True) edge_vertices = [] num_ports = 0 for coord in board.coords.keys(): if len(board.coords[coord].resource_locs) < 3: edge_vertices.append(coord) self.assertEqual(len(edge_vertices), 30) for vertex in edge_vertices: if len(board.coords[vertex].ports) != 0: num_ports += 1 self.assertEqual(len(board.coords[vertex].ports), 1) port = list(board.coords[vertex].ports)[0] num_neighbors_with_ports = 0 for neighbor in board.coords[vertex].neighbours: if len(board.coords[neighbor].ports) != 0: num_neighbors_with_ports += 1 self.assertEqual(port, list(board.coords[neighbor].ports)[0]) self.assertEqual(num_neighbors_with_ports, 1) self.assertEqual(num_ports, 18)
def declare_player_types(self): """ # After the choices have been made for P1 and P2, do the appropriate init for them. :return: """ player = None for i, choice in enumerate(self.__choices__): if choice == "random": player = RandomPlayer() elif choice == "sequential": print(f"Player {i+1} chose Sequential, decide your sequence") sequence = [action for action in input("Sequence: ").split(", ")] player = Sequential(sequence) elif choice == "mostcommon": player = MostCommon() elif choice == "historian": print(f"Player {i+1} chose Sequential, decide your remembering (1,2,3)") r = int(input("Remembering: ")) player = Historian(r=r) self.__players__.append(player)
card_vals.append(3) elif i % 5 == 0: card_vals.append(2) else: card_vals.append(1) deck = list(range(1, 105)) wins = [0, 0, 0, 0, 0, 0] total_scores = [0, 0, 0, 0, 0, 0] num_games = 1000000 for i in range(num_games): cards = random.sample(deck, 10*6 + 4) players = [RandomPlayer(cards[:10], 0), SmallPlayer(cards[10:20], 1), BigPlayer(cards[20:30], 2), HeuristicSmallPlayer(cards[30:40], 3), HeuristicBigPlayer(cards[40:50], 4), ClosePlayer(cards[50:60], 5)] scores = [0, 0, 0, 0, 0, 0] table = [[cards[50]], [cards[51]], [cards[52]], [cards[53]]] for turn in range(10): to_play = sorted([(x.play(table), x.name) for x in players]) for card, name in to_play: sorted_table = sorted(table, key=lambda x: x[-1], reverse=True) row = None for r in sorted_table:
def test_trade_bank(self): player_list = [RandomPlayer(1)] deck = Deck() board = Board(player_list, False) board.active_player = player_list[0] board.round_num = 2 player_list[0].resources = {'w': 2, 'b': 2, 'l': 3, 'g': 3, 'o': 3} player_list[0].has_rolled = True # Cannot trade legal_moves = player_list[0].get_legal_moves(board, deck) for move in legal_moves: self.assertNotEqual(move.move_type, Move.TRADE_BANK) # Can only trade w player_list[0].ports.append('2 w') legal_moves = player_list[0].get_legal_moves(board, deck) for move in legal_moves: if move.move_type == Move.TRADE_BANK: self.assertEqual( player_list[0].check_legal_move(move, board, deck), True) self.assertEqual(move.give_resource, 'w') self.assertIn(Move(Move.TRADE_BANK, give_resource='w', resource='b'), legal_moves) self.assertIn(Move(Move.TRADE_BANK, give_resource='w', resource='l'), legal_moves) self.assertIn(Move(Move.TRADE_BANK, give_resource='w', resource='g'), legal_moves) self.assertIn(Move(Move.TRADE_BANK, give_resource='w', resource='o'), legal_moves) self.assertNotIn( Move(Move.TRADE_BANK, give_resource='w', resource='w'), legal_moves) # can trade things with 2, 3, and 4 trades_l = 0 trades_g = 0 trades_o = 0 player_list[0].ports = ['3', '2 l'] player_list[0].resources = {'w': 0, 'b': 0, 'l': 2, 'g': 3, 'o': 4} legal_moves = player_list[0].get_legal_moves(board, deck) for move in legal_moves: if move.move_type == Move.TRADE_BANK: self.assertEqual( player_list[0].check_legal_move(move, board, deck), True) self.assertIn(move.give_resource, ['l', 'g', 'o']) if move.give_resource == 'l': trades_l += 1 self.assertIn(move.resource, ['w', 'b', 'g', 'o']) elif move.give_resource == 'g': trades_g += 1 self.assertIn(move.resource, ['w', 'b', 'l', 'o']) elif move.give_resource == 'o': trades_o += 1 self.assertIn(move.resource, ['w', 'b', 'l', 'g']) self.assertEqual(trades_l, 4) self.assertEqual(trades_o, 4) self.assertEqual(trades_g, 4) player_list[0].make_move( Move(Move.TRADE_BANK, give_resource='l', resource='w'), board, deck, player_list) self.assertEqual(player_list[0].resources['w'], 1) self.assertEqual(player_list[0].resources['l'], 0) player_list[0].make_move( Move(Move.TRADE_BANK, give_resource='g', resource='w'), board, deck, player_list) self.assertEqual(player_list[0].resources['w'], 2) self.assertEqual(player_list[0].resources['g'], 0) player_list[0].make_move( Move(Move.TRADE_BANK, give_resource='o', resource='w'), board, deck, player_list) self.assertEqual(player_list[0].resources['w'], 3) self.assertEqual(player_list[0].resources['o'], 1) # Test trading 4 player_list[0].ports = ['2 b'] player_list[0].trades_tried = 0 player_list[0].resources = {'w': 0, 'b': 0, 'l': 2, 'g': 3, 'o': 5} trades = 0 legal_moves = player_list[0].get_legal_moves(board, deck) for move in legal_moves: if move.move_type == Move.TRADE_BANK: self.assertEqual( player_list[0].check_legal_move(move, board, deck), True) self.assertEqual(move.give_resource, 'o') trades += 1 self.assertEqual(trades, 4) player_list[0].make_move( Move(Move.TRADE_BANK, give_resource='o', resource='w'), board, deck, player_list) self.assertEqual(player_list[0].resources['w'], 1) self.assertEqual(player_list[0].resources['o'], 1) self.assertEqual( player_list[0].check_legal_move( Move(Move.TRADE_BANK, give_resource='b', resource='w'), board, deck), False)
from SmartPlayer import SmartPlayer from RandomPlayer import RandomPlayer from GreedyPlayer import GreedyPlayer from Othello import Othello from Player import playGame from HumanPlayer import HumanPlayer from pybrain.tools.customxml.networkreader import NetworkReader from TacticalPlayer import TacticalPlayer import random import time nn = NetworkReader.readFrom("othelloNetwork.xml") player1 = SmartPlayer(nn,8) #change this to change the opponent to be testing against player2 = RandomPlayer() othello = Othello() othello.resetGame() player1.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER])) player2.newGame(othello,player1.enemy) while not othello.isGameOver(): if (player1.color == othello.WHITE_PLAYER): print "Neural Network is white" else: print "Neural Network is black" othello.printBoard() time.sleep(1) if (othello.whoGoesNext() == player1.color): move = player1.getMove() othello.makeMove(move[0],move[1],player1.color)
def test_play_dev_human(self): user_input = [ Move.PLAY_DEV, Card.KNIGHT, '0 0', # Knight Move.PLAY_DEV, Card.VICTORY_POINT, # Victory Point Move.PLAY_DEV, Card.ROAD_BUILDING, '0 0', '1 0', '0 0', '1 1', # Road Building Move.PLAY_DEV, Card.MONOPOLY, 'w', # Monopoly Move.PLAY_DEV, Card.YEAR_OF_PLENTY, 'o', 'o', '5', '4', 'w', 'l' # Year of Plenty ] with patch('builtins.input', side_effect=user_input): player_list = [Human(1), RandomPlayer(2)] deck = Deck() board = Board(player_list, False) board.round_num = 2 board.active_player = player_list[0] player_list[0].has_rolled = True player_list[0].dev_cards[Card.KNIGHT] = 1 player_list[0].dev_cards[Card.VICTORY_POINT] = 1 player_list[0].dev_cards[Card.ROAD_BUILDING] = 1 player_list[0].dev_cards[Card.MONOPOLY] = 2 player_list[0].dev_cards[Card.YEAR_OF_PLENTY] = 2 # Knight move = player_list[0].decide_move(board, deck, player_list) self.assertEqual( player_list[0].make_move(move, board, deck, player_list), 1) self.assertEqual(player_list[0].num_knights_played, 1) self.assertEqual(board.robber, (0, 0)) self.assertEqual(player_list[0].dev_played, 1) # Victory Point player_list[0].dev_played = 0 move = player_list[0].decide_move(board, deck, player_list) self.assertEqual(move, -1) self.assertEqual(player_list[0].dev_played, 0) # Road Building move = player_list[0].decide_move(board, deck, player_list) self.assertEqual(move.road, frozenset([(1, 0), (0, 0)])) self.assertEqual(move.road2, frozenset([(0, 0), (1, 1)])) move_made = player_list[0].make_move(move, board, deck, player_list) self.assertEqual(move_made, -1) player_list[0].settlements.append((0, 0)) move_made = player_list[0].make_move(move, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(player_list[0].dev_played, 1) # Monopoly player_list[0].dev_played = 0 move = player_list[0].decide_move(board, deck, player_list) move_made = player_list[0].make_move(move, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(player_list[0].dev_played, 1) self.assertEqual(player_list[0].resources['w'], 4) player_list[0].dev_played = 0 move_made = player_list[0].make_move(move, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(player_list[0].dev_played, 1) self.assertEqual(player_list[0].resources['w'], 4) self.assertEqual(player_list[0].resources['o'], 0) self.assertEqual(player_list[0].resources['l'], 4) self.assertEqual(player_list[0].resources['b'], 4) self.assertEqual(player_list[0].resources['g'], 2) # Year of Plenty player_list[0].dev_played = 0 move = player_list[0].decide_move(board, deck, player_list) move_made = player_list[0].make_move(move, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(player_list[0].dev_played, 1) self.assertEqual(player_list[0].resources['w'], 4) self.assertEqual(player_list[0].resources['o'], 2) self.assertEqual(player_list[0].resources['l'], 4) self.assertEqual(player_list[0].resources['b'], 4) self.assertEqual(player_list[0].resources['g'], 2) player_list[0].dev_played = 0 move = player_list[0].decide_move(board, deck, player_list) move_made = player_list[0].make_move(move, board, deck, player_list) self.assertEqual(move_made, 1) self.assertEqual(player_list[0].dev_played, 1) self.assertEqual(player_list[0].resources['w'], 5) self.assertEqual(player_list[0].resources['o'], 2) self.assertEqual(player_list[0].resources['l'], 5) self.assertEqual(player_list[0].resources['b'], 4) self.assertEqual(player_list[0].resources['g'], 2)
def test_player_takes_longest_road(self): player_list = [RandomPlayer(1), RandomPlayer(2)] deck = Deck() board = Board(player_list, False) # Longest road player_list[0].add_road(board, frozenset([(1, 1), (0, 0)])) board.build_road((1, 1), (0, 0), player_list[0]) player_list[0].add_road(board, frozenset([(0, 0), (1, 0)])) board.build_road((0, 0), (1, 0), player_list[0]) player_list[0].add_road(board, frozenset([(1, 0), (2, 0)])) board.build_road((1, 0), (2, 0), player_list[0]) player_list[0].add_road(board, frozenset([(2, 0), (3, 0)])) board.build_road((2, 0), (3, 0), player_list[0]) player_list[0].add_road(board, frozenset([(3, 0), (4, 0)])) board.build_road((3, 0), (4, 0), player_list[0]) player_list[0].add_road(board, frozenset([(4, 0), (5, 0)])) board.build_road((4, 0), (5, 0), player_list[0]) # Another long road that is shorter than longest road player_list[0].add_road(board, frozenset([(8, 0), (9, 0)])) board.build_road((8, 0), (9, 0), player_list[0]) player_list[0].add_road(board, frozenset([(9, 0), (8, 1)])) board.build_road((9, 0), (8, 1), player_list[0]) player_list[0].add_road(board, frozenset([(8, 1), (9, 1)])) board.build_road((8, 1), (9, 1), player_list[0]) player_list[0].add_road(board, frozenset([(9, 1), (8, 2)])) board.build_road((9, 1), (8, 2), player_list[0]) player_list[0].add_road(board, frozenset([(8, 2), (9, 2)])) board.build_road((8, 2), (9, 2), player_list[0]) self.assertEqual(board.longest_road_player, player_list[0]) self.assertEqual(board.longest_road_size, 6) self.assertEqual(player_list[0].longest_road, 2) self.assertEqual(player_list[1].longest_road, 0) # Player 2 breaks player 1's longest road, but player 1 # will still hold longest road from its other road player_list[1].settlements.append((2, 0)) board.add_settlement(player_list[1], (2, 0)) self.assertEqual(board.longest_road_player, player_list[0]) self.assertEqual(board.longest_road_size, 5) self.assertEqual(player_list[0].longest_road, 2) self.assertEqual(player_list[1].longest_road, 0) # Player 2 will now take longest road player_list[1].add_road(board, frozenset([(2, 0), (3, 1)])) board.build_road((2, 0), (3, 1), player_list[1]) player_list[1].add_road(board, frozenset([(3, 1), (2, 1)])) board.build_road((3, 1), (2, 1), player_list[1]) player_list[1].add_road(board, frozenset([(2, 1), (3, 2)])) board.build_road((2, 1), (3, 2), player_list[1]) player_list[1].add_road(board, frozenset([(3, 2), (2, 2)])) board.build_road((3, 2), (2, 2), player_list[1]) player_list[1].add_road(board, frozenset([(2, 2), (3, 3)])) board.build_road((2, 2), (3, 3), player_list[1]) self.assertEqual(board.longest_road_player, player_list[0]) self.assertEqual(board.longest_road_size, 5) self.assertEqual(player_list[0].longest_road, 2) self.assertEqual(player_list[1].longest_road, 0) player_list[1].add_road(board, frozenset([(3, 3), (2, 3)])) board.build_road((3, 3), (2, 3), player_list[1]) self.assertEqual(board.longest_road_player, player_list[1]) self.assertEqual(board.longest_road_size, 6) self.assertEqual(player_list[0].longest_road, 0) self.assertEqual(player_list[1].longest_road, 2)