예제 #1
0
    def test_attack_empty_cell(self):
        game = Game()
        game.save()

        result = game.attack_cell(2, 3)
        self.assertEqual(AttackStatus.MISSED, result.attack_status)
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[2][3])
예제 #2
0
    def test_attack_invalid_cell(self):
        game = Game()
        game.opponent_grid[2][3] = GameCell.MISSED.value
        game.save()

        result = game.attack_cell(2, 3)
        self.assertEqual(AttackStatus.INVALID, result.attack_status)
예제 #3
0
    def test_save_new_game(self):
        game = Game()
        game.opponent_grid[0][0] = GameCell.SHIP.value
        game.save()

        all_games = Game.objects.all()

        self.assertEqual(1, len(all_games))
        self.assertEqual(GameStatus.IN_PROGRESS.value,
                         all_games[0].game_status)
        self.assertEqual(GameCell.SHIP.value, all_games[0].opponent_grid[0][0])
예제 #4
0
    def test_attack_cell_kill_top_left_ship(self):
        game = Game()
        game.opponent_grid[0][0] = GameCell.SHIP.value
        game.save()

        result = game.attack_cell(0, 0)
        self.assertEqual(AttackStatus.KILLED, result.attack_status)
        self.assertEqual(GameStatus.FINISHED.value, game.game_status)
        self.assertEqual(GameCell.KILLED.value, game.opponent_grid[0][0])

        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[0][1])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[1][0])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[1][1])
예제 #5
0
def create_random_game():
    """
    Small utility factory function to create a random game
    :return: A new game with randomly placed ships
    """

    game = Game()

    ships = [4, 3, 3, 2, 2, 2, 1, 1, 1, 1]
    for ship_size in ships:
        _create_random_ship(game.opponent_grid, ship_size)

    game.save()
    return game
예제 #6
0
    def test_attack_cell_kill_bottom_right_ship(self):
        game = Game()
        game.opponent_grid[9][9] = GameCell.SHIP.value
        game.save()

        result = game.attack_cell(9, 9)

        self.assertEqual(AttackStatus.KILLED, result.attack_status)
        self.assertEqual(GameStatus.FINISHED.value, game.game_status)
        self.assertEqual(GameCell.KILLED.value, game.opponent_grid[9][9])

        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[8][9])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[9][8])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[8][8])
예제 #7
0
    def test_attack_cell_injure_large_ship(self):
        game = Game()
        game.opponent_grid[0][0] = GameCell.SHIP.value
        game.opponent_grid[1][0] = GameCell.INJURED.value
        game.opponent_grid[2][0] = GameCell.INJURED.value
        game.opponent_grid[3][0] = GameCell.SHIP.value
        game.save()

        result = game.attack_cell(3, 0)

        self.assertEqual(AttackStatus.INJURED, result.attack_status)
        self.assertEqual(GameStatus.IN_PROGRESS.value, game.game_status)
        self.assertEqual(GameCell.INJURED.value, game.opponent_grid[3][0])
        self.assertEqual(GameCell.SHIP.value, game.opponent_grid[0][0])

        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[4][1])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[2][1])
예제 #8
0
    def test_attack_cell_kill_center_ship(self):
        game = Game()
        game.opponent_grid[5][5] = GameCell.SHIP.value
        game.save()

        result = game.attack_cell(5, 5)
        self.assertEqual(AttackStatus.KILLED, result.attack_status)
        self.assertEqual(GameStatus.FINISHED.value, game.game_status)
        self.assertEqual(GameCell.KILLED.value, game.opponent_grid[5][5])

        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[5][6])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[5][4])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[6][5])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[4][5])

        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[6][6])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[4][4])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[6][4])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[4][6])
예제 #9
0
    def test_attack_cell_kill_one_of_two_ships(self):
        game = Game()
        game.opponent_grid[5][5] = GameCell.SHIP.value
        game.opponent_grid[3][3] = GameCell.SHIP.value
        game.save()

        result = game.attack_cell(5, 5)

        self.assertEqual(AttackStatus.KILLED, result.attack_status)
        self.assertEqual(GameStatus.IN_PROGRESS.value, game.game_status)
        self.assertEqual(GameCell.KILLED.value, game.opponent_grid[5][5])

        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[5][6])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[5][4])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[6][5])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[4][5])

        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[6][6])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[4][4])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[6][4])
        self.assertEqual(GameCell.MISSED.value, game.opponent_grid[4][6])
예제 #10
0
    def test_attack_cell_in_finished_game(self):
        game = Game(game_status=GameStatus.FINISHED.value)
        game.save()

        result = game.attack_cell(2, 3)
        self.assertEqual(AttackStatus.INVALID, result.attack_status)