def _create_game(self) -> None:
        height = randint(30, 70)
        width = randint(30, 70)

        player_count = randint(3, 6)
        players = []
        occupied_coordinates = []
        for i in range(1, player_count + 1):
            next_coordinate = (randint(0, width - 1), randint(0, height - 1))
            while next_coordinate in occupied_coordinates:
                next_coordinate = (randint(0,
                                           width - 1), randint(0, height - 1))
            occupied_coordinates.append(next_coordinate)
            player = Player(i, next_coordinate[0], next_coordinate[1],
                            Direction.get_random_direction(), 1, True, str(i))
            players.append(player)

        cells = [[Cell() for _ in range(width)] for _ in range(height)]
        for player in players:
            cells[player.y][player.x] = Cell([player])

        self._game = Game(width, height, cells, players, 1, True,
                          datetime.now() + timedelta(5, 15))
        self._game_round = 0

        self._ais = []

        if self.__evaluation_type == 1:
            self.__generate_ais_for_first_evaluation(player_count, players)
        elif self.__evaluation_type == 2:
            self.__generate_ais_for_second_evaluation(player_count, players)
    def test_raise_exception_for_winner_in_running_game(self):
        player = Player(1, 0, 0, Direction.up, 0, True, "Name")
        cells = [[Cell([player]), Cell()]]
        game = Game(2, 1, cells, [player], 1, True, datetime.now())

        with self.assertRaises(Exception):
            game.get_winner()
    def test_player_with_id_should_be_returned(self):
        player1 = Player(1, 0, 0, Direction.up, 0, True, "Name")
        player2 = Player(2, 1, 0, Direction.up, 0, True, "Name")
        cells = [[Cell([player1]), Cell([player2])]]
        game = Game(2, 1, cells, [player1, player2], 1, True, datetime.now())

        self.assertEqual(player1, game.get_player_by_id(1))
    def _create_game(self) -> None:
        player1 = Player(1, 5, 5, Direction.down, 1, True, "Human Player 1")
        player2 = Player(2, 25, 5, Direction.down, 1, True, "AI Player 1")
        player3 = Player(3, 5, 15, Direction.up, 1, True, "AI Player 2")
        player4 = Player(4, 25, 15, Direction.up, 1, True, "AI Player 4")
        players = [player1, player2, player3, player4]
        height = 20
        width = 30
        cells = [[Cell() for _ in range(width)] for _ in range(height)]
        cells[player1.y][player1.x] = Cell([player1])
        cells[player2.y][player2.x] = Cell([player2])
        cells[player3.y][player3.x] = Cell([player3])
        cells[player4.y][player4.x] = Cell([player4])

        self._game = Game(width, height, cells, players, 1, True, datetime.now(time_zone))
        self._game_round = 0

        self.__you = None
        ai0 = ai_classes.RandomAI(player1)
        # Make a comment out of the next two lines if you do not want to play on your own.
        self.__you = player1
        ai0 = None

        ai1 = ai_classes.PathfindingAI(player2, 2, 75)
        ai2 = ai_classes.NotKillingItselfAI(player3, [ai_classes.AIOptions.max_distance], 1, 0, 3)
        ai3 = ai_classes.SearchTreeAI(player4, 2, 1, True, 10)

        self._ais = [ai0, ai1, ai2, ai3]
    def test_raise_exception_on_non_existing_own_player(self):
        player1 = Player(1, 0, 1, Direction.up, 0, True, "Name 1")
        player3 = Player(3, 0, 0, Direction.up, 0, True, "Name 3")
        players = [player1, player3]
        cells = [[Cell([player3]), Cell([])], [Cell([player1]), Cell()]]

        with self.assertRaises(OwnPlayerMissingException):
            Game(2, 2, cells, players, 2, True, datetime.now())
Ejemplo n.º 6
0
    def test_correct_output_no_winner(self, mock_stdout):
        player1 = Player(1, 0, 0, Direction.up, 1, False, "p1")
        player2 = Player(2, 0, 1, Direction.down, 3, False, "")
        cells = [[Cell([player1])], [Cell([player2])]]
        game = Game(1, 2, cells, [player1, player2], 2, False, datetime.now())
        self.sut.update(game)

        self.assertTrue("No winner in game." in str(mock_stdout.getvalue()))
    def test_raise_exception_when_player_id_invalid(self):
        player1 = Player(1, 1, 0, Direction.up, 0, True, "Name")
        player2 = Player(2, 0, 0, Direction.up, 0, True, "Name")
        cells = [[Cell([player2]), Cell([player1])]]
        game = Game(2, 1, cells, [player1, player2], 1, True, datetime.now())

        with self.assertRaises(PlayerWithGivenIdNotAvailableException):
            game.get_player_by_id(100)
    def test_raise_exception_on_wrong_player_position(self):
        player1 = Player(1, 1, 1, Direction.up, 0, True, "Name 1")
        player2 = Player(2, 0, 0, Direction.up, 0, True, "Name 2")
        player3 = Player(3, 0, 1, Direction.up, 0, True, "Name 3")
        players = [player1, player2, player3]
        cells = [[Cell([player2]), Cell([player3])], [Cell(), Cell([player1])]]

        with self.assertRaises(PlayerPositionException):
            Game(2, 2, cells, players, 2, True, datetime.now())
    def test_return_no_winner_in_ended_game(self):
        player1 = Player(1, 0, 0, Direction.up, 0, False, "Name")
        player2 = Player(1, 1, 0, Direction.up, 0, False, "Name")
        cells = [[Cell([player1]), Cell([player2])]]
        game = Game(2, 1, cells, [player1, player2], 1, False, datetime.now())

        result = game.get_winner()

        self.assertEqual(None, result)
    def test_examines_your_player_after_creation(self):
        player1 = Player(1, 0, 1, Direction.up, 0, True, "Name 1")
        player2 = Player(2, 1, 0, Direction.up, 0, True, "Name 2")
        player3 = Player(3, 0, 0, Direction.up, 0, True, "Name 3")
        players = [player1, player2, player3]
        cells = [[Cell([player3]), Cell([player2])], [Cell([player1]), Cell()]]

        game = Game(2, 2, cells, players, 2, True, datetime.now())

        self.assertEqual(game.you, player2)
    def test_draws_all_players_correctly_in_ended_game_with_winner(self, mock_stdout):
        player1 = Player(1, 1, 0, Direction.up, 1, True, "Jonas")
        player2 = Player(2, 0, 1, Direction.down, 3, False, "Florian")
        cells = [[Cell(), Cell([player1])],
                 [Cell([player2]), Cell()]]
        game = Game(2, 2, cells, [player1, player2], 2, False, datetime.now())

        self.sut.update(game)

        self.assertTrue("Winner: Player 1 (Jonas). Your player ID was 2." in str(mock_stdout.getvalue()))
    def test_dont_raise_exception_on_wrong_inactive_player_position(self):
        player1 = Player(1, 1, 1, Direction.up, 0, False, "Name 1")
        player2 = Player(2, 1, 0, Direction.up, 0, True, "Name 2")
        player3 = Player(3, 0, 1, Direction.up, 0, True, "Name 3")
        players = [player1, player2, player3]
        cells = [[Cell([]), Cell([player2])],
                 [Cell([player3]), Cell([player3])]]

        game = Game(2, 2, cells, players, 2, True, datetime.now())

        self.assertEqual(game.you, player2)
Ejemplo n.º 13
0
    def test_correct_output_round_2(self, mock_stdout):
        player1 = Player(1, 0, 0, Direction.up, 1, True, "p1")
        player2 = Player(2, 0, 1, Direction.down, 3, True, "")
        cells = [[Cell([player1]), Cell([player1])], [Cell([player2]), Cell()]]
        game = Game(2, 2, cells, [player1, player2], 2, True, datetime.now())

        self.sut.update(game)
        self.sut.update(game)

        self.assertTrue("Round :  0" in str(mock_stdout.getvalue()))
        self.assertTrue("Round :  1" in str(mock_stdout.getvalue()))
 def setUp(self):
     self.player1 = Player(1, 10, 10, Direction.down, 1, True, "")
     self.player2 = Player(2, 10, 30, Direction.down, 3, True, "")
     self.player3 = Player(3, 30, 10, Direction.right, 2, True, "Name 3")
     players = [self.player1, self.player2, self.player3]
     cells = [[Cell() for _ in range(40)] for _ in range(40)]
     cells[self.player1.y][self.player1.x] = Cell([self.player1])
     cells[self.player2.y][self.player2.x] = Cell([self.player2])
     cells[self.player3.y][self.player3.x] = Cell([self.player3])
     time = datetime(2020, 10, 1, 12, 5, 13, 0, timezone.utc)
     self.game = Game(40, 40, cells, players, 2, True, time)
     self.sut = GameService(self.game)
Ejemplo n.º 15
0
def create_game(game_ended: bool):
    player1 = Player(1, 10, 10, Direction.down, 1, True, "")
    player2 = Player(2, 10, 30, Direction.down, 3, True, "")
    player3 = Player(3, 30, 10, Direction.right, 2, True, "Name 3")
    players = [player1, player2, player3]

    cells = [[Cell() for _ in range(40)] for _ in range(40)]
    for player in players:
        cells[player.y][player.x] = Cell([player])

    return Game(40, 40, cells, players, 2, game_ended,
                datetime(2020, 10, 1, 12, 5, 13, 0, timezone.utc))
    def test_return_all_other_active_players(self):
        player1 = Player(1, 1, 1, Direction.up, 0, True, "Name 1")
        player2 = Player(2, 1, 0, Direction.up, 0, False, "Name 2")
        player3 = Player(3, 0, 0, Direction.up, 0, True, "Name 3")
        players = [player1, player2, player3]
        cells = [[Cell([player3]), Cell([player2])],
                 [Cell([]), Cell([player1])]]
        game = Game(2, 2, cells, players, 1, True, datetime.now())

        result = game.get_other_player_ids(player1, check_active=True)

        self.assertEqual([3], result)
    def test_visited_cells_should_be_correct_after_collision(self):
        self.player1.direction = Direction.left
        self.player1.speed = 4
        self.player1.x = 2
        self.player1.y = 0
        self.game.cells[self.player1.y][self.player1.x] = Cell([self.player1])
        self.game.cells[self.player1.y][1] = Cell([self.player1])

        visited_cells = self.sut.get_and_visit_cells(self.player1, Action.speed_up)

        self.assertEqual(self.player1.x, 0)
        self.assertEqual(self.player1.y, 0)
        self.assertTrue((0, 0) in visited_cells)
        self.assertTrue((1, 0) in visited_cells)
    def test_copying_a_game_should_return_same_game_but_different_identity(
            self):
        player1 = Player(1, 1, 1, Direction.up, 0, True, "Name")
        player2 = Player(2, 1, 0, Direction.up, 0, True, "Name2")
        player3 = Player(3, 0, 0, Direction.up, 0, True, "Name3")
        players = [player1, player2, player3]
        cells = [[Cell([player3]), Cell([player2])],
                 [Cell([]), Cell([player1])]]
        game = Game(2, 2, cells, players, 2, True, datetime.now())

        result = game.copy()

        self.assertEqual(game, result)
        self.assertNotEqual(id(game), id(result))
    def copy(self):
        """Creates an exact same copy of this game but all objects point to different memory locations.

        Returns:
            A copy of the game.
        """
        players: List[Player] = []
        for player in self.players:
            players.append(
                Player(player.id, player.x, player.y, player.direction,
                       player.speed, player.active, player.name))

        cells: List[List[Cell]] = [[Cell() for _ in range(self.width)]
                                   for _ in range(self.height)]
        for row in range(len(self.cells)):
            for col in range(len(self.cells[row])):
                if self.cells[row][col].players is not None:
                    players_in_cell = []
                    for player in self.cells[row][col].players:
                        for copied_player in players:
                            if copied_player.id == player.id:
                                players_in_cell.append(copied_player)
                    cells[row][col].players = players_in_cell

        return Game(self.width, self.height, cells, players, self.you.id,
                    self.running, self.deadline)
Ejemplo n.º 20
0
    def test_create_action_should_return_None_if_given_action_list_is_none(self):
        player1 = Player(1, 0, 0, Direction.right, 2, True, "")
        player2 = Player(2, 0, 2, Direction.down, 3, True, "")
        players = [player1, player2]
        cells = [[Cell([player1]),  Cell(),             Cell()],
                 [Cell(),           Cell([player2]),    Cell()],
                 [Cell([player2]),  Cell(),             Cell()]]
        game = Game(3, 3, cells, players, 2, True, datetime.now())
        sut = PathfindingAI(player1, 2, 10)

        actions = sut.find_actions_by_best_path_connection(None, game)

        self.assertIsNone(actions)
Ejemplo n.º 21
0
    def test_create_action_should_return_one_of_the_possible_action_with_best_connection(self):
        player1 = Player(1, 0, 0, Direction.right, 2, True, "")
        player2 = Player(2, 0, 2, Direction.down, 3, True, "")
        players = [player1, player2]
        cells = [[Cell([player1]),  Cell(),             Cell()],
                 [Cell(),           Cell([player2]),    Cell()],
                 [Cell([player2]),  Cell(),             Cell()]]
        game = Game(3, 3, cells, players, 2, True, datetime.now())
        sut = PathfindingAI(player1, 2, 10)

        actions = sut.find_actions_by_best_path_connection([Action.change_nothing, Action.slow_down], game)

        self.assertEqual(actions[0][0], Action.slow_down)
Ejemplo n.º 22
0
    def test_create_action_should_return_action_with_best_connection(self):
        player1 = Player(1, 0, 0, Direction.down, 1, True, "")
        player2 = Player(2, 0, 2, Direction.down, 3, True, "")
        players = [player1, player2]
        cells = [[Cell([player1]),  Cell(),             Cell()],
                 [Cell(),           Cell([player2]),    Cell()],
                 [Cell([player2]),  Cell(),             Cell()]]
        game = Game(3, 3, cells, players, 2, True, datetime.now())
        sut = PathfindingAI(player1, 2, 10)

        result = Value('i')
        sut.create_next_action(game, result)

        self.assertEqual(Action.turn_left, Action.get_by_index(result.value))
    def test_draws_all_players_correctly(self):
        player1 = Player(1, 0, 0, Direction.up, 1, True, "p1")
        player2 = Player(2, 0, 1, Direction.down, 3, True, "")
        cells = [[Cell([player1]), Cell([player1])],
                 [Cell([player2]), Cell()]]
        game = Game(2, 2, cells, [player1, player2], 2, True, datetime.now())
        expected_calls = [
            call(ANY, (255, 61, 0), (0, 0, 10, 10)),
            call(ANY, (0, 0, 0), (2, 2, 6, 6)),
            call(ANY, (255, 61, 0), (11, 0, 10, 10)),
            call(ANY, (156, 204, 101), (0, 11, 10, 10)),
            call(ANY, (0, 0, 0), (4, 15, 2, 2)),
            call(ANY, (0, 0, 0), (11, 11, 10, 10))
        ]

        self.sut.update(game)

        pygame_mock.init.assert_called_once()
        pygame_mock.draw.rect.assert_has_calls(expected_calls, any_order=False)
Ejemplo n.º 24
0
    def test_get_random_free_cells_from_playground_should_return_correct_number_of_free_cells(self):
        player1 = Player(1, 0, 0, Direction.up, 1, True, "")
        player2 = Player(2, 0, 1, Direction.down, 3, True, "")
        players = [player1, player2]
        cells = [[Cell([player1]), Cell(), Cell()],
                 [Cell([player2]), Cell(), Cell()],
                 [Cell(), Cell(), Cell()]]
        count_free_cells = 5
        game = Game(3, 3, cells, players, 2, True, datetime.now())
        sut = PathfindingAI(player1, 2, count_free_cells)

        free_cells_xy = sut.get_random_free_cells_from_playground(game)

        self.assertEqual(len(free_cells_xy), count_free_cells)
        for (x, y) in free_cells_xy:
            self.assertTrue(game.cells[y][x].players is None)
    def test_playerX_playerY_should_be_correct_after_collision(self):
        self.player1.direction = Direction.left
        self.player1.speed = 2
        self.player1.x = 1
        self.player1.y = 0
        self.game.cells[self.player1.y][self.player1.x] = Cell([self.player1])

        self.sut.get_and_visit_cells(self.player1, Action.speed_up)

        self.assertEqual(self.player1.x, 0)
        self.assertEqual(self.player1.y, 0)
    def test_playerX_playerY_should_be_correct_without_collision(self):
        self.player1.direction = Direction.left
        self.player1.speed = 2
        self.player1.x = 10
        self.player1.y = 0
        self.game.cells[self.player1.y][self.player1.x] = Cell([self.player1])

        self.sut.get_and_visit_cells(self.player1, Action.change_nothing)

        self.assertEqual(self.player1.x, 8)
        self.assertEqual(self.player1.y, 0)
    def load(self, game_data: str) -> Game:
        """See base class."""
        json_data = json.loads(game_data)
        players = []
        cells = []

        for json_player in json_data["players"]:
            player = Player(
                int(json_player), int(json_data["players"][json_player]["x"]),
                int(json_data["players"][json_player]["y"]),
                Direction[json_data["players"][json_player]["direction"]],
                int(json_data["players"][json_player]["speed"]),
                json_data["players"][json_player]["active"],
                json_data["players"][json_player]["name"]
                if "name" in json_data["players"][json_player] else "")
            players.append(player)

        for json_row in json_data["cells"]:
            row = []
            for json_cell in json_row:
                cell = Cell()
                if json_cell != 0:
                    if json_cell == -1:
                        # If there is a collision cell, it is not defined by which players this cell is occupied.
                        # Therefore is it always filled with the first player so the cell is not empty.
                        cell = Cell([players[0]])
                    else:
                        for player in players:
                            if player.id == json_cell:
                                cell = Cell([player])
                row.append(cell)
            cells.append(row)

        return Game(
            int(json_data["width"]), int(json_data["height"]), cells, players,
            int(json_data["you"]), json_data["running"],
            iso8601.parse_date(json_data["deadline"])
            if json_data["running"] else None)
    def test_visited_cells_should_be_calculated_correctly_turn_1_to_5(self):
        self.player1.direction = Direction.down
        self.player1.speed = 1
        player1_x = self.player1.x
        player1_y = self.player1.y
        self.game.cells[self.player1.y][self.player1.x] = Cell([self.player1])
        self.player2.direction = Direction.up
        self.player2.speed = 3
        player2_x = self.player2.x
        player2_y = self.player2.y
        self.game.cells[self.player2.y][self.player2.x] = Cell([self.player2])
        self.player3.direction = Direction.down
        self.player3.speed = 5
        player3_x = self.player3.x
        player3_y = self.player3.y
        self.game.cells[self.player3.y][self.player3.x] = Cell([self.player3])
        visited_cells_p1_expected = [(player1_x, player1_y + 1), (player1_x, player1_y + 2)]
        visited_cells_p2_expected = [(player2_x, player2_y - 1), (player2_x, player2_y - 2)]
        visited_cells_p3_expected = [(player3_x + 1, player3_y), (player3_x + 2, player3_y), (player3_x + 3, player3_y),
                                     (player3_x + 4, player3_y), (player3_x + 5, player3_y)]

        visited_cells_p1 = self.sut.get_and_visit_cells(self.player1, Action.speed_up)
        visited_cells_p2 = self.sut.get_and_visit_cells(self.player2, Action.slow_down)
        visited_cells_p3 = self.sut.get_and_visit_cells(self.player3, Action.turn_left)

        self.assertEqual(visited_cells_p1_expected, visited_cells_p1)
        self.assertEqual(visited_cells_p2_expected, visited_cells_p2)
        self.assertEqual(visited_cells_p3_expected, visited_cells_p3)
        self.assertTrue(self.player1 in self.game.cells[player1_y + 1][player1_x].players)
        self.assertTrue(self.player1 in self.game.cells[player1_y + 2][player1_x].players)
        self.assertTrue(self.player2 in self.game.cells[player2_y - 2][player2_x].players)
        self.assertTrue(self.player2 in self.game.cells[player2_y - 2][player2_x].players)
        self.assertTrue(self.player3 in self.game.cells[player3_y][player3_x + 1].players)
        self.assertTrue(self.player3 in self.game.cells[player3_y][player3_x + 2].players)
        self.assertTrue(self.player3 in self.game.cells[player3_y][player3_x + 3].players)
        self.assertTrue(self.player3 in self.game.cells[player3_y][player3_x + 4].players)
        self.assertTrue(self.player3 in self.game.cells[player3_y][player3_x + 5].players)
    def test_visited_cells_should_be_calculated_correctly_turn_6(self):
        self.sut.turn.turn_ctr = 12  # 6, 12, 18 should all work
        self.player1.direction = Direction.down
        self.player1.speed = 1
        player1_x = self.player1.x
        player1_y = self.player1.y
        self.game.cells[10][10] = Cell([self.player1])
        self.player2.direction = Direction.up
        self.player2.speed = 5
        player2_x = self.player2.x
        player2_y = self.player2.y
        self.game.cells[10][30] = Cell([self.player2])
        visited_cells_p1_expected = [(player1_x, player1_y + 1), (player1_x, player1_y + 2)]
        visited_cells_p2_expected = [(player2_x, player2_y - 1), (player2_x, player2_y - 6)]

        visited_cells_p1 = self.sut.get_and_visit_cells(self.player1, Action.speed_up)
        visited_cells_p2 = self.sut.get_and_visit_cells(self.player2, Action.speed_up)

        self.assertEqual(visited_cells_p1_expected, visited_cells_p1)
        self.assertEqual(visited_cells_p2_expected, visited_cells_p2)
        self.assertTrue(self.player1 in self.game.cells[player1_y + 1][player1_x].players)
        self.assertTrue(self.player1 in self.game.cells[player1_y + 2][player1_x].players)
        self.assertTrue(self.player2 in self.game.cells[player2_y - 1][player2_x].players)
        self.assertTrue(self.player2 in self.game.cells[player2_y - 6][player2_x].players)
    def test_translate_cell_matrix_to_pathfinding_matrix_should_be_correct(
            self):
        player1 = Player(1, 0, 0, Direction.up, 1, True, "")
        player2 = Player(2, 0, 1, Direction.down, 3, True, "")
        players = [player1, player2]
        cells = [[Cell([player1]), Cell()], [Cell([player2]),
                                             Cell()], [Cell(), Cell()]]

        game = Game(2, 3, cells, players, 2, True, datetime.now())
        expected_matrix = [[0, 1], [0, 1], [1, 1]]

        matrix = game.translate_cell_matrix_to_pathfinding_matrix()

        self.assertEqual(matrix, expected_matrix)