Beispiel #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])
Beispiel #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)
Beispiel #3
0
    def test_attack_cell_valid(self, view_set_object):
        game = Game(pk=1)
        game.attack_cell = Mock(return_value=AttackCellResponse(AttackStatus.MISSED, game))
        view_set_object.return_value = game

        response = self.client.post(reverse('game-attack', args=[2]), data={'row': 3, 'column': 4}, )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(AttackStatus.MISSED.value, response.data['attackStatus'])
        game.attack_cell.assert_called_once_with(3, 4)
Beispiel #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])
Beispiel #5
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])
Beispiel #6
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])
Beispiel #7
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])
Beispiel #8
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])
Beispiel #9
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)