def test_replay_with_player2_winner(self): initial_board = Board() initial_board.move_pawn((1, 4), PLAYER_1) initial_board.move_pawn((0, 4), PLAYER_2) game = Game([Agent(), Agent()], initial_board) game.play() with open(self.replay_file_name, "w") as file: game.trace.write(file) with open(self.replay_file_name, "r") as file: replay = json.load(file) self.assertEqual(replay['winner'], 1)
def test_replay_without_winner(self): initial_board = Board() trace = Trace(initial_board, [None, None]) with open(self.replay_file_name, "w") as file: trace.write(file) with open(self.replay_file_name, "r") as file: replay = json.load(file) self.assertEqual(replay['winner'], 0)
def test_should_create_trace_with_player_names_when_2_are_supplied(self): agents = [ConsoleViewer(), ConsoleViewer()] board = Board() viewer = None credits = [None, None] player_names = ["Ding", "Dong"] # Est effrayante game = Game(agents, board, viewer, credits, None, player_names) self.assertEqual(player_names, game.trace.player_names)
def test_export_json(self): board = Board() trace = Trace(board, [None, None]) trace.add_action(1, (MOVE, 7, 4), 1.0) with open(self.replay_file_name, "w") as file: trace.write(file) with open(self.replay_file_name, "r") as file: content = file.readline() self.assertNotEqual(content, "")
def test_should_support_being_sent_4_players(self): agents = [ ConsoleViewer(), ConsoleViewer(), ConsoleViewer(), ConsoleViewer() ] board = Board() viewer = None credits = [None, None, None, None] game = Game(agents, board, viewer, credits)
def test_should_create_trace_with_player_names_when_4_are_supplied(self): agents = [ ConsoleViewer(), ConsoleViewer(), ConsoleViewer(), ConsoleViewer() ] board = Board() viewer = None credits = [None, None, None, None] player_names = ["CAESAR", "MARCVS", "AVRELIVS", "ANTONINVS"] game = Game(agents, board, viewer, credits, None, player_names) self.assertEqual(player_names, game.trace.player_names)
def test_export_and_load(self): initial_board = Board() trace = Trace(initial_board, [None, None]) trace.add_action(1, (MOVE, 7, 4), 1.0) with open(self.replay_file_name, "w") as file: trace.write(file) with open(self.replay_file_name, "r") as file2: loaded_trace = load_trace(file2) initial_loaded_game = loaded_trace.get_initial_board() (player, action, t) = loaded_trace.actions[0] pre_move_board = initial_loaded_game.clone() first_move_loaded_game = initial_loaded_game.play_action( action, player) self.assertEqual(pre_move_board.pawns[1], (8, 4)) self.assertEqual(first_move_loaded_game.pawns[1], (7, 4))
def setUp(self): credits = [None, None, None, None] self.agent1 = Agent() self.agent2 = Agent() self.agent3 = Agent() self.agent4 = Agent() self.agents = [self.agent1, self.agent2, self.agent3, self.agent4] for agent in self.agents: agent.initialize = MagicMock() agent.play = MagicMock() self.board2 = Board() self.board4 = Board(player_count=4) self.trace2 = Trace(self.board2, credits[0:2]) self.trace2.set_winner = MagicMock() self.trace2.set_ranking = MagicMock() self.trace4 = Trace(self.board4, credits[0:4]) self.trace4.set_winner = MagicMock() self.trace4.set_ranking = MagicMock() self.viewer = HeadlessViewer() self.viewer.finished = MagicMock() self.game2 = Game(self.agents[0:2], self.board2, self.viewer, credits[0:2], self.trace2) self.game4 = Game(self.agents[0:4], self.board4, self.viewer, credits[0:4], self.trace4)
def setUp(self): credits = [None, None] self.agent1 = Agent() self.agent2 = Agent() agents = [self.agent1, self.agent2] for agent in agents: agent.initialize = MagicMock() agent.play = MagicMock() self.board2 = Board() self.trace2 = Trace(self.board2, credits[0:2]) self.trace2.set_winner = MagicMock() self.trace2.set_ranking = MagicMock() self.board2.is_player_on_goal = MagicMock(return_value=False) self.viewer = HeadlessViewer() self.viewer.finished = MagicMock() self.game2 = Game(agents[0:2], self.board2, self.viewer, credits[0:2], self.trace2)
class TestGamePlay(unittest.TestCase): def setUp(self): credits = [None, None, None, None] self.agent1 = Agent() self.agent2 = Agent() self.agent3 = Agent() self.agent4 = Agent() self.agents = [self.agent1, self.agent2, self.agent3, self.agent4] for agent in self.agents: agent.initialize = MagicMock() agent.play = MagicMock() self.board2 = Board() self.board4 = Board(player_count=4) self.trace2 = Trace(self.board2, credits[0:2]) self.trace2.set_winner = MagicMock() self.trace2.set_ranking = MagicMock() self.trace4 = Trace(self.board4, credits[0:4]) self.trace4.set_winner = MagicMock() self.trace4.set_ranking = MagicMock() self.viewer = HeadlessViewer() self.viewer.finished = MagicMock() self.game2 = Game(self.agents[0:2], self.board2, self.viewer, credits[0:2], self.trace2) self.game4 = Game(self.agents[0:4], self.board4, self.viewer, credits[0:4], self.trace4) def test_play_with_2_players_should_call_initialize(self): self.board2.is_finished = MagicMock(return_value=True) self.game2.play() self.agent1.initialize.assert_called_with(self.board2, [0], None) self.agent2.initialize.assert_called_with(self.board2, [1], None) def test_play_with_2_players_should_play_while_not_finished(self): self.board2.is_finished = MagicMock(side_effect=[False, False, True]) self.agent1.play = MagicMock(return_value=(WALL_H, 1, 2)) self.agent2.play = MagicMock(return_value=(WALL_H, 2, 4)) self.game2.play() self.agent1.play.assert_called_with(self.board2, 0, 1, None) self.agent2.play.assert_called_with(self.board2, 1, 2, None) def test_play_with_4_players_should_call_initialize(self): self.board4.is_finished = MagicMock(return_value=True) self.game4.play() self.agent1.initialize.assert_called_with(self.board4, [0], None) self.agent2.initialize.assert_called_with(self.board4, [1], None) self.agent3.initialize.assert_called_with(self.board4, [2], None) self.agent4.initialize.assert_called_with(self.board4, [3], None) def test_play_with_4_players_should_play_while_not_finished(self): self.board4.is_finished = MagicMock( side_effect=[False, False, False, False, True]) self.agent1.play = MagicMock(return_value=(WALL_H, 1, 2)) self.agent2.play = MagicMock(return_value=(WALL_H, 2, 4)) self.agent3.play = MagicMock(return_value=(WALL_H, 3, 5)) self.agent4.play = MagicMock(return_value=(WALL_H, 2, 4)) self.game4.play() self.agent1.play.assert_called_with(self.board4, 0, 1, None) self.agent2.play.assert_called_with(self.board4, 1, 2, None) self.agent3.play.assert_called_with(self.board4, 2, 3, None) self.agent4.play.assert_called_with(self.board4, 3, 4, None) def test_play_with_2_players_should_correctly_set_the_winner(self): self.board2.is_finished = MagicMock(side_effect=[False, False, True]) self.agent1.play = MagicMock(return_value=(WALL_H, 1, 2)) self.agent2.play = MagicMock(return_value=(MOVE, 7, 4)) self.game2.play() self.trace2.set_winner.assert_called_with(PLAYER_2, "") self.viewer.finished.assert_called_with(2, PLAYER_2, "") def test_play_with_4_players_should_correctly_set_the_winner(self): self.board4.is_finished = MagicMock( side_effect=[False, False, False, False, True]) self.agent1.play = MagicMock(return_value=(WALL_H, 1, 2)) self.agent2.play = MagicMock(return_value=(WALL_H, 5, 7)) self.agent3.play = MagicMock(return_value=(MOVE, 7, 4)) self.agent4.play = MagicMock(return_value=(WALL_H, 2, 2)) self.game4.play() self.trace4.set_winner.assert_called_with(PLAYER_3, "") self.viewer.finished.assert_called_with(4, PLAYER_3, "") def test_play_should_reset_credits_every_turn(self): credits = [5.0, 3.0, 3.0, 3.0] trace4 = Trace(self.board4, credits[0:4]) viewer = HeadlessViewer() game4 = Game(self.agents[0:4], self.board4, viewer, credits, trace4) self.board4.is_finished = MagicMock( side_effect=[False, False, False, False, False, True]) self.agent1.play = MagicMock(return_value=[(WALL_H, 1, 2), (WALL_H, 3, 4)]) self.agent2.play = MagicMock(return_value=(WALL_H, 5, 7)) self.agent3.play = MagicMock(return_value=(MOVE, 7, 4)) self.agent4.play = MagicMock(return_value=(WALL_H, 2, 2)) game4.play() self.assertAlmostEqual(self.agent1.play.call_args[0][3], 5.0) def test_should_return_ranking_with_all_2_players(self): self.board2.is_finished = MagicMock(side_effect=[False, False, True]) self.agent1.play = MagicMock(return_value=(WALL_H, 1, 2)) self.agent2.play = MagicMock(return_value=(MOVE, 7, 4)) self.game2.play() self.trace2.set_winner.assert_called_with(PLAYER_2, "") self.trace2.set_ranking.assert_called_with([PLAYER_2, PLAYER_1]) self.viewer.finished.assert_called_with(2, PLAYER_2, "") def test_should_return_ranking_based_on_order_of_arrival_for_2_players( self): # They're both close to the victory but PLAYER_2 wins self.board2.move_pawn((6, 4), PLAYER_1) self.board2.move_pawn((1, 4), PLAYER_2) self.board2.players_on_goal = MagicMock( side_effect=[[False, False], [False, False], [False, True]]) self.board2.is_finished = MagicMock(side_effect=[False, False, True]) self.board2.is_player_on_goal = MagicMock( side_effect=[False, False, False, True]) self.board2.get_scores = MagicMock(return_value=[(PLAYER_2, 50), (PLAYER_1, 49)]) self.agent1.play = MagicMock(return_value=(MOVE, 7, 4)) self.agent2.play = MagicMock(return_value=(MOVE, 0, 4)) self.game2.play() self.trace2.set_winner.assert_called_with(PLAYER_2, "") self.trace2.set_ranking.assert_called_with([PLAYER_2, PLAYER_1]) self.viewer.finished.assert_called_with(2, PLAYER_2, "") def test_should_return_ranking_based_on_order_of_arrival_for_2_players_with_disconnect( self): # They're both close to the victory but PLAYER_2 disconnect before reaching the end self.board2.move_pawn((6, 4), PLAYER_1) self.board2.move_pawn((1, 4), PLAYER_2) self.board2.players_on_goal = MagicMock( side_effect=[[False, False], [False, False], [False, False]]) self.board2.is_finished = MagicMock(side_effect=[False, False, False]) self.board2.is_player_on_goal = MagicMock( side_effect=[False, False, False]) self.board2.get_scores = MagicMock(return_value=[(PLAYER_1, 50), (PLAYER_2, 49)]) self.agent1.play = MagicMock(return_value=(MOVE, 7, 4)) self.agent2.play = MagicMock(side_effect=[socket.timeout]) self.game2.play() self.trace2.set_winner.assert_called_with( PLAYER_1, "Opponent's have been expelled.") self.trace2.set_ranking.assert_called_with([PLAYER_1, PLAYER_2]) self.viewer.finished.assert_called_with( 2, PLAYER_1, "Opponent's have been expelled.") def test_should_return_ranking_based_on_order_of_arrival_for_4_players( self): # set every player one step away from victory, PLAYER_1 finishes last self.board4.move_pawn((6, 4), PLAYER_1) self.board4.move_pawn((4, 1), PLAYER_2) self.board4.move_pawn((1, 4), PLAYER_3) self.board4.move_pawn((4, 7), PLAYER_4) self.board4.players_on_goal = MagicMock(side_effect=[[ False, False, False, False ], [False, False, False, False], [ False, True, False, False ], [False, True, True, False], [False, True, True, True]]) self.board4.is_finished = MagicMock( side_effect=[False, False, False, False, True]) self.board4.is_player_on_goal = MagicMock( side_effect=[False, False, False, True, False, True, False, True]) self.board4.get_scores = MagicMock( return_value=[(PLAYER_2, 50), (PLAYER_3, 50), (PLAYER_4, 50), (PLAYER_1, 49)]) self.agent1.play = MagicMock(return_value=(MOVE, 7, 4)) # rekt self.agent2.play = MagicMock(return_value=(MOVE, 4, 0)) self.agent3.play = MagicMock(return_value=(MOVE, 0, 4)) self.agent4.play = MagicMock(return_value=(MOVE, 4, 8)) self.game4.play() self.trace4.set_winner.assert_called_with(PLAYER_2, "") # first self.trace4.set_ranking.assert_called_with( [PLAYER_2, PLAYER_3, PLAYER_4, PLAYER_1]) # order is VERY important self.viewer.finished.assert_called_with(4, PLAYER_2, "") def test_should_return_ranking_based_on_order_of_arrival_for_4_players_nonobstant_the_score_order( self): # set every player one step away from victory, PLAYER_1 finishes last self.board4.move_pawn((6, 4), PLAYER_1) self.board4.move_pawn((4, 1), PLAYER_2) self.board4.move_pawn((1, 4), PLAYER_3) self.board4.move_pawn((4, 7), PLAYER_4) self.board4.players_on_goal = MagicMock(side_effect=[[ False, False, False, False ], [False, False, False, False], [ False, True, False, False ], [False, True, True, False], [False, True, True, True]]) self.board4.is_finished = MagicMock( side_effect=[False, False, False, False, True]) self.board4.is_player_on_goal = MagicMock( side_effect=[False, False, False, True, False, True, False, True]) self.board4.get_scores = MagicMock( return_value=[(PLAYER_4, 50), (PLAYER_3, 50), (PLAYER_2, 50), (PLAYER_1, 49)]) self.agent1.play = MagicMock(return_value=(MOVE, 7, 4)) # rekt self.agent2.play = MagicMock(return_value=(MOVE, 4, 0)) self.agent3.play = MagicMock(return_value=(MOVE, 0, 4)) self.agent4.play = MagicMock(return_value=(MOVE, 4, 8)) self.game4.play() self.trace4.set_winner.assert_called_with(PLAYER_2, "") # first self.trace4.set_ranking.assert_called_with( [PLAYER_2, PLAYER_3, PLAYER_4, PLAYER_1]) # order is VERY important self.viewer.finished.assert_called_with(4, PLAYER_2, "") def test_should_return_ranking_based_on_order_of_arrival_for_4_players_player_3_wins( self): # set every player one step away from victory, PLAYER_1 finishes last self.board4.move_pawn((6, 4), PLAYER_1) self.board4.move_pawn((4, 2), PLAYER_2) self.board4.move_pawn((1, 4), PLAYER_3) self.board4.move_pawn((4, 7), PLAYER_4) self.board4.players_on_goal = MagicMock(side_effect=[ [False, False, False, False], [False, False, False, False], [False, False, False, False], [False, False, True, False], [False, False, True, True], [True, False, True, True] ]) self.board4.is_finished = MagicMock( side_effect=[False, False, False, False, False, True]) self.board4.is_player_on_goal = MagicMock(side_effect=[ False, False, False, False, False, True, False, True, False, True ]) self.board4.get_scores = MagicMock( return_value=[(PLAYER_2, 50), (PLAYER_3, 50), (PLAYER_4, 50), (PLAYER_1, 49)]) self.agent1.play = MagicMock(side_effect=[(MOVE, 7, 4), (MOVE, 8, 4)]) self.agent2.play = MagicMock(side_effect=[(MOVE, 4, 1), (MOVE, 4, 0)]) # rekt self.agent3.play = MagicMock(return_value=(MOVE, 0, 4)) self.agent4.play = MagicMock(return_value=(MOVE, 4, 8)) self.game4.play() self.trace4.set_winner.assert_called_with(PLAYER_3, "") # first self.trace4.set_ranking.assert_called_with( [PLAYER_3, PLAYER_4, PLAYER_1, PLAYER_2]) # order is VERY important self.viewer.finished.assert_called_with(5, PLAYER_3, "") def test_should_return_ranking_based_on_order_of_arrival_for_4_players_with_disconnections( self): # set every player one step away from victory, PLAYER_1 finishes last self.board4.move_pawn((6, 4), PLAYER_1) self.board4.move_pawn((4, 2), PLAYER_2) self.board4.move_pawn((1, 4), PLAYER_3) self.board4.move_pawn((4, 7), PLAYER_4) self.board4.players_on_goal = MagicMock(side_effect=[ [False, False, False, False], [False, False, False, False], [False, False, False, False], [False, False, False, False], [False, False, True, False], [True, False, True, False], [True, False, True, False] ]) self.board4.is_finished = MagicMock( side_effect=[False, False, False, False, False, True]) self.board4.is_player_on_goal = MagicMock(side_effect=[ False, False, False, False, False, False, True, False, True ]) self.board4.get_scores = MagicMock( return_value=[(PLAYER_2, 50), (PLAYER_3, 50), (PLAYER_4, 50), (PLAYER_1, 49)]) self.agent1.play = MagicMock(side_effect=[(MOVE, 7, 4), (MOVE, 8, 4)]) self.agent2.play = MagicMock(side_effect=[(MOVE, 4, 1), (MOVE, 4, 0)]) # rekt self.agent3.play = MagicMock(side_effect=socket.timeout) self.agent4.play = MagicMock(return_value=(MOVE, 4, 8)) self.game4.play() self.trace4.set_winner.assert_called_with(PLAYER_4, "") # first self.trace4.set_ranking.assert_called_with( [PLAYER_4, PLAYER_1, PLAYER_2, PLAYER_3]) # order is VERY important self.viewer.finished.assert_called_with(5, PLAYER_4, "") def test_should_not_poke_player_who_won(self): # set every player one step away from victory, PLAYER_1 finishes last self.board4.move_pawn((5, 4), PLAYER_1) self.board4.move_pawn((4, 3), PLAYER_2) self.board4.move_pawn((1, 4), PLAYER_3) self.board4.move_pawn((4, 7), PLAYER_4) self.board4.players_on_goal = MagicMock(side_effect=[ [False, False, False, False], [False, False, False, False], [False, False, False, False], [False, False, True, False], [False, False, True, False], [False, False, True, True], [False, False, True, True], [False, False, True, True], [False, False, True, True], [False, False, True, True], [True, False, True, True] ]) self.board4.is_finished = MagicMock(side_effect=[ False, False, False, False, False, False, False, False, False, True ]) self.board4.is_player_on_goal = MagicMock(side_effect=[ False, False, False, False, False, True, False, True, False, False, False, False, True, True, False, True ]) self.board4.get_scores = MagicMock( return_value=[(PLAYER_2, 50), (PLAYER_3, 50), (PLAYER_4, 50), (PLAYER_1, 49)]) self.agent1.play = MagicMock(side_effect=[(MOVE, 6, 4), (MOVE, 7, 4), (MOVE, 8, 4)]) self.agent2.play = MagicMock(side_effect=[(MOVE, 4, 2), (MOVE, 4, 1), (MOVE, 4, 0)]) # rekt self.agent3.play = MagicMock(side_effect=[(MOVE, 0, 4)]) self.agent4.play = MagicMock(side_effect=[(MOVE, 4, 8)]) self.game4.play() self.trace4.set_winner.assert_called_with(PLAYER_3, "") # first self.trace4.set_ranking.assert_called_with( [PLAYER_3, PLAYER_4, PLAYER_1, PLAYER_2]) # order is VERY important self.viewer.finished.assert_called_with(7, PLAYER_3, "") def test_should_not_infinite_loop_when_2_player_disconnect_and_2_players_are_done( self): # disconnect 2 players, make 2 other win self.board4.move_pawn((7, 4), PLAYER_1) # win self.board4.move_pawn((4, 2), PLAYER_2) # dc self.board4.move_pawn((1, 4), PLAYER_3) # win self.board4.move_pawn((4, 7), PLAYER_4) # dc self.board4.is_finished = MagicMock(return_value=False) self.board4.players_on_goal = MagicMock(side_effect=[[ False, False, False, False ], [True, False, False, False], [ True, False, False, False ], [True, False, True, False], [True, False, True, False]]) self.board4.is_player_on_goal = MagicMock( side_effect=[False, True, False, False, True, False]) self.board4.get_scores = MagicMock( return_value=[(PLAYER_2, 50), (PLAYER_3, 50), (PLAYER_4, 50), (PLAYER_1, 49)]) self.agent1.play = MagicMock(side_effect=[(MOVE, 8, 4)]) # win self.agent2.play = MagicMock(side_effect=socket.timeout) # dc baby self.agent3.play = MagicMock(return_value=(MOVE, 0, 4)) # win self.agent4.play = MagicMock(side_effect=socket.timeout) # dc baby self.game4.play() self.trace4.set_winner.assert_called_with(PLAYER_1, "") # first self.trace4.set_ranking.assert_called_with( [PLAYER_1, PLAYER_3, PLAYER_4, PLAYER_2]) self.viewer.finished.assert_called_with(3, PLAYER_1, "")
trace = load_trace(args.replay) args.replay.close() except (IOError, jsonpickle.UnpicklingError) as e: logging.error("Unable to load trace. Reason: %s", e) exit(1) board = trace.get_initial_board() elif args.board is not None: # load board logging.debug("Loading board from '%s'", args.board.name) try: percepts = load_percepts(args.board) args.board.close() except Exception as e: logging.warning("Unable to load board. Reason: %s", e) parser.error("argument --board: invalid board") board = Board(percepts, len(percepts.pawns)) else: # default board board = Board(player_count=2 if args.agent4 is None else 4, starting_walls=args.wall) # Create viewer if args.headless: args.gui = False viewer = None else: if args.gui: try: viewer = gui.TkViewer() except Exception as e: logging.warning(