class Tests(unittest.TestCase):
    def setUp(self):
        self.game = BowlingGame()

    def do_rolls(self, n):
        for i in rolls[:n]:
            self.game.roll(i)

    def test_no_rolls(self):
        self.assertEqual(0, self.game.score())

    def test_one_roll(self):
        self.do_rolls(1)
        self.assertEqual(1, self.game.score())

    def test_two_rolls(self):
        self.do_rolls(2)
        self.assertEqual(5, self.game.score())

    def test_four_rolls(self):
        self.do_rolls(4)
        self.assertEqual(14, self.game.score())

    def test_rolls_including_spare(self):
        self.do_rolls(7)
        self.assertEqual(34, self.game.score())
    
    def test_full_game(self):
        self.do_rolls(len(rolls))
        self.assertEqual(133, self.game.score())

    def test_perfect_game(self):
        for i in range(22):
            self.game.roll(10)
        self.assertEqual(300, self.game.score())
Example #2
0
 def test_score_only_misses(self):
     """
         Own Test 1: Test only misses.
     """
     print("Own Test 1: Test only misses.")
     test_game = BowlingGame("- - - - - - - - - -")
     self.assertEqual(test_game.score(), 0)
Example #3
0
 def test_score_only_strikes(self):
     """
         Example Test 1: Test strikes.
     """
     print("Example Test 1: Test strikes.")
     test_game = BowlingGame("X X X X X X X X X X X X")
     self.assertEqual(test_game.score(), 300)
Example #4
0
 def test_score_following_misses(self):
     """
         Example Test 2: Test nines.
     """
     print("Example Test 2: Test nines.")
     test_game = BowlingGame("9- 9- 9- 9- 9- 9- 9- 9- 9- 9-")
     self.assertEqual(test_game.score(), 90)
Example #5
0
 def test_score_only_splits(self):
     """
         Example Test 3: Test splits.
     """
     print("Example Test 3: Test spare.")
     test_game = BowlingGame("5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5")
     self.assertEqual(test_game.score(), 150)
Example #6
0
 def test_excemption_not_enough_rolls(self):
     """
     let's make sure that the catch if the user didn't input enough rolls
     """
     rolls = [3, 2, 1]
     game = BowlingGame(log_name=self.log_name)
     with self.assertRaises(ValueError):
         game.play_from_list(rolls)
Example #7
0
 def test_rule_change(self):
     """
     test new rule change implementation (shortgame)
     """
     game = BowlingGame(rules=ShortRules(), log_name=self.log_name)
     short_list = [10] * 7  # 5 strikes and 2 extra rolls
     score = game.play_from_list(short_list)
     self.assertTrue(score == 150)
Example #8
0
 def test_score_second_to_last_strike(self):
     """
         Own Test 8: Test with a strike second to last.
     """
     print("Own Test 8: Test with a strike second to last.")
     # 8*3 + 10 + 2*3 = 40
     test_game = BowlingGame("12 12 12 12 12 12 12 12 X 12")
     self.assertEqual(test_game.score(), 40)
Example #9
0
 def test_score_single_miss(self):
     """
         Own Test 6: Test a single miss.
     """
     print("Own Test 6: Test a single miss.")
     # 0 + 9*3 = 27
     test_game = BowlingGame("- 12 12 12 12 12 12 12 12 12")
     self.assertEqual(test_game.score(), 27)
Example #10
0
 def test_score_preceding_misses(self):
     """
         Own Test 7: Test misses preceding hits.
     """
     print("Own Test 7: Test misses preceding hits.")
     # 10*2 = 20
     test_game = BowlingGame("-2 -2 -2 -2 -2 -2 -2 -2 -2 -2")
     self.assertEqual(test_game.score(), 20)
Example #11
0
 def test_score_single_spare(self):
     """
         Own Test 4: Test a single spare.
     """
     print("Own Test 4: Test a single spare.")
     # (10 + 1) + 9*3 = 38
     test_game = BowlingGame("1/ 12 12 12 12 12 12 12 12 12")
     self.assertEqual(test_game.score(), 38)
Example #12
0
 def test_score_single_strike(self):
     """
         Own Test 5: Test a single strike.
     """
     print("Own Test 5: Test a single strike.")
     # (10 + 3) + 9*3 = 40
     test_game = BowlingGame("X 12 12 12 12 12 12 12 12 12")
     self.assertEqual(test_game.score(), 40)
Example #13
0
 def test_score_no_special_cases(self):
     """
         Own Test 3: Test no special cases like strikes, spares, misses.
     """
     print("Own Test 3: Test no special cases like strikes, spares, misses.")
     # 10*3 = 30
     test_game = BowlingGame("12 12 12 12 12 12 12 12 12 12")
     self.assertEqual(test_game.score(), 30)
Example #14
0
 def test_score_mixed_cases(self):
     """
         Own Test 2: Test mixed cases, i.e. a mix of spares, strikes and misses.
     """
     print("Own Test 2: Test mixed cases, i.e. a mix of spares, strikes and misses.")
     # 0 + (10+10) + (10+0+0) + 0 + 7 + (10+6) + (10+10) + (10 + 0+0) + (10+0) = 93
     test_game = BowlingGame("- 2/ X - 52 4/ 6/ X - 8/ -")
     self.assertEqual(test_game.score(), 93)
Example #15
0
    def test_excemption_roll_value(self):
        """
        let's do some basic checking for input like that we don't let people score more frames that pins per frame
        """
        rolls = [20] * 12
        game = BowlingGame(log_name=self.log_name)

        with self.assertRaises(ValueError):
            game.play_from_list(rolls)
Example #16
0
 def test_perfect_game(self):
     """
     test perfect game score
     12 rolls of 10 pins
     """
     rolls = [10] * 12
     game = BowlingGame(log_name=self.log_name)
     score = game.play_from_list(rolls)
     self.assertTrue(score == 300, 'score is %i' % score)
Example #17
0
    def test_mixed_score(self):
        """
        test mix scores here's just a random list i ran through an online bowling score calculator
        assuming that the calculator was correct our game logic should be too :D 
        """
        rolls = [0, 0, 1, 2, 5, 4, 3, 1, 10, 9, 1, 0, 4, 6, 1, 4, 6, 5, 1]

        game = BowlingGame(log_name=self.log_name)
        score = game.play_from_list(rolls)
        self.assertTrue(score == 78, 'score = %s' % score)
Example #18
0
    def test_nine_one_split_game(self):
        """
        this test simulates a game full of 9,1 splits with 10 pin score on the last frame (bonus)
        """
        rolls = []
        for _ in xrange(10):
            rolls.extend([9, 1])

        # last frame is a strike
        rolls.append(10)

        game = BowlingGame(log_name=self.log_name)
        score = game.play_from_list(rolls)
        self.assertTrue(score == 191, 'score is %i' % score)
Example #19
0
class BowlingGameTests(unittest.TestCase):
    def setUp(self):
        self.bowling_game = BowlingGame()

    def test_gutter_game(self):
        #Arrange
        expected_score = 0

        #Act
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)
        self.bowling_game.roll(0)

        #Assert
        self.assertEqual(self.bowling_game.score, expected_score)
Example #20
0
class BowlingTests(unittest.TestCase):
    def setUp(self):
        """setup test"""
        self.game = BowlingGame()
    
    def roll(self, pins):
        """rolls once"""
        self.rollMany(1, pins)

    def rollMany(self, times, pins):
        """rolls many"""
        for i in range(times):
            self.game.roll(pins)
    
    def testGameExists(self):
        """can create a game"""
        game = BowlingGame()

    def testGutterGame(self):
        """score 0 for gutter game"""
        self.rollMany(20, 0)
        assert self.game.score() == 0

    def testAllOnes(self):
        """score 20 for all ones game"""
        self.rollMany(20, 1)
        assert self.game.score() == 20

    def testOneSpare(self):
        """score 16 for one spare"""
        self.roll(3)
        self.roll(7)
        self.roll(3)
        self.rollMany(17, 0)
        assert self.game.score() == 16

    def testOneStrike(self):
        """score 24 for one strike"""
        self.roll(10)
        self.roll(3)
        self.roll(4)
        self.rollMany(16, 0)
        assert self.game.score() == 24
class BowlingTests(unittest.TestCase):
    def setUp(self):
        self.game = BowlingGame()

    def roll(self, rolls):
        [self.game.roll(roll) for roll in rolls]

    def roll_and_score(self, rolls):
        self.roll(rolls)
        return self.game.score()

    def test_should_be_able_to_score_a_game_with_all_zeros(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 0)

    def test_should_be_able_to_score_a_game_with_no_strikes_or_spares(self):
        rolls = [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 90)

    def test_a_spare_follow_by_zeros_is_worth_ten_points(self):
        rolls = [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 10)

    def test_points_scored_in_the_roll_after_a_spare_are_counted_twice(self):
        rolls = [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 16)

    def test_consecutive_spares_each_get_a_one_roll_bonus(self):
        rolls = [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 31)

    def test_last_frame_spare_gets_bonus_roll_that_is_counted_twice(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 17)

    def test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll(self):
        rolls = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 10)

    def test_two_rolls_points_after_strike_are_counted_twice(self):
        rolls = [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 26)

    def test_consecutive_stikes_each_get_the_two_roll_bonus(self):
        rolls = [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 81)

    def test_strike_in_last_frame_gets_two_roll_bonus_counted_once(self):
        rolls = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1
        ]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 18)

    def test_rolling_spare_with_bonus_roll_does_not_get_bonus(self):
        rolls = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3
        ]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 20)

    def test_strikes_with_the_two_bonus_rolls_do_not_get_bonus_rolls(self):
        rolls = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10
        ]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 30)

    def test_strike_with_bonus_after_spare_in_last_frame_gets_no_bonus(self):
        rolls = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10
        ]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 20)

    def test_all_strikes_is_a_perfect_game(self):
        rolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 300)

    def test_rolls_cannot_score_negative_points(self):

        self.assertRaises(ValueError, self.game.roll, -11)

    def test_a_roll_cannot_score_more_than_10_points(self):

        self.assertRaises(ValueError, self.game.roll, 11)

    def test_two_rolls_in_a_frame_cannot_score_more_than_10_points(self):
        self.game.roll(5)

        self.assertRaises(ValueError, self.game.roll, 6)

    def test_bonus_after_strike_in_last_frame_cannot_score_more_than_10(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]

        self.roll(rolls)

        self.assertRaises(ValueError, self.game.roll, 11)

    def test_bonus_aft_last_frame_strk_can_be_more_than_10_if_1_is_strk(self):
        rolls = [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6
        ]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 26)

    def test_bonus_aft_last_frame_strk_cnt_be_strk_if_first_is_not_strk(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6]

        self.roll(rolls)

        self.assertRaises(ValueError, self.game.roll, 10)

    def test_an_incomplete_game_cannot_be_scored(self):
        rolls = [0, 0]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.score)

    def test_cannot_roll_if_there_are_already_ten_frames(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.roll, 0)

    def test_bonus_rolls_for_strike_must_be_rolled_before_score_is_calc(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.score)

    def test_both_bonuses_for_strike_must_be_rolled_before_score(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.score)

    def test_bonus_rolls_for_spare_must_be_rolled_before_score_is_calc(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.score)
 def setUp(self):
     self.game = BowlingGame()
 def setUp(self):
     self.game = BowlingGame()
Example #24
0
class TestBowlingGame(unittest.TestCase):

    def setUp(self):

        self.game = BowlingGame()

    def test_gutter_game(self):

        self.roll_many(0, 20)

        self.assertEqual(self.game.score(), 0)

    def test_all_ones(self):

        self.roll_many(1, 20)

        self.assertEqual(self.game.score(), 20)

    def test_one_spare(self):

        self.game.roll(5)
        self.game.roll(5)
        self.game.roll(3)
        self.roll_many(0, 17)

        self.assertEqual(self.game.score(), 16)

    def test_one_strike(self):

        self.game.roll(10)
        self.game.roll(4)
        self.game.roll(3)
        self.roll_many(0, 16)

        self.assertEqual(self.game.score(), 16)

    def test_perfect_game(self):

        self.roll_many(10, 12)

        self.assertEqual(self.game.score(), 300)

    def test_all_spares(self):

        self.roll_many(5, 21)
        
        self.assertEqual(self.game.score(), 150)

    def roll_many(self, pins, rolls):

        for i in range(rolls):

            self.game.roll(pins)
Example #25
0
class BowlingTests(unittest.TestCase):
    def setUp(self):
        self.game = BowlingGame()

    def roll(self, rolls):
        [self.game.roll(roll) for roll in rolls]

    def roll_and_score(self, rolls):
        self.roll(rolls)
        return self.game.score()

    def test_should_be_able_to_score_a_game_with_all_zeros(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 0)

    def test_should_be_able_to_score_a_game_with_no_strikes_or_spares(self):
        rolls = [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 90)

    def test_a_spare_follow_by_zeros_is_worth_ten_points(self):
        rolls = [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 10)

    def test_points_scored_in_the_roll_after_a_spare_are_counted_twice(self):
        rolls = [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 16)

    def test_consecutive_spares_each_get_a_one_roll_bonus(self):
        rolls = [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 31)

    def test_last_frame_spare_gets_bonus_roll_that_is_counted_twice(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 17)

    def test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll(self):
        rolls = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 10)

    def test_two_rolls_points_after_strike_are_counted_twice(self):
        rolls = [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 26)

    def test_consecutive_stikes_each_get_the_two_roll_bonus(self):
        rolls = [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 81)

    def test_strike_in_last_frame_gets_two_roll_bonus_counted_once(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 10, 7, 1]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 18)

    def test_rolling_spare_with_bonus_roll_does_not_get_bonus(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 10, 7, 3]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 20)

    def test_strikes_with_the_two_bonus_rolls_do_not_get_bonus_rolls(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,
                 10, 10]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 30)

    def test_strike_with_bonus_after_spare_in_last_frame_gets_no_bonus(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
                 3, 10]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 20)

    def test_all_strikes_is_a_perfect_game(self):
        rolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 300)

    def test_rolls_cannot_score_negative_points(self):

        self.assertRaises(ValueError, self.game.roll, -11)

    def test_a_roll_cannot_score_more_than_10_points(self):

        self.assertRaises(ValueError, self.game.roll, 11)

    def test_two_rolls_in_a_frame_cannot_score_more_than_10_points(self):
        self.game.roll(5)

        self.assertRaises(ValueError, self.game.roll, 6)

    def test_bonus_after_strike_in_last_frame_cannot_score_more_than_10(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]

        self.roll(rolls)

        self.assertRaises(ValueError, self.game.roll, 11)

    def test_bonus_aft_last_frame_strk_can_be_more_than_10_if_1_is_strk(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,
                 10, 6]

        score = self.roll_and_score(rolls)

        self.assertEqual(score, 26)

    def test_bonus_aft_last_frame_strk_cnt_be_strk_if_first_is_not_strk(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6]

        self.roll(rolls)

        self.assertRaises(ValueError, self.game.roll, 10)

    def test_an_incomplete_game_cannot_be_scored(self):
        rolls = [0, 0]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.score)

    def test_cannot_roll_if_there_are_already_ten_frames(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.roll, 0)

    def test_bonus_rolls_for_strike_must_be_rolled_before_score_is_calc(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.score)

    def test_both_bonuses_for_strike_must_be_rolled_before_score(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.score)

    def test_bonus_rolls_for_spare_must_be_rolled_before_score_is_calc(self):
        rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3]

        self.roll(rolls)

        self.assertRaises(IndexError, self.game.score)
Example #26
0
'''
Created on Aug 20, 2018

@author: carlos

this is a simple example of rule encapsulation and inheritance to implement the game with new rules

'''
from bowling import Rules


class ShortRules(Rules):
    frames = 5


if __name__ == '__main__':
    from bowling import BowlingGame
    GAME = BowlingGame(rules=ShortRules())
    HALFROLLS = [10] * 7  # 5 strikes and 2 extra rolls
    GAME.play_from_list(HALFROLLS)
Example #27
0
class BowlingGameTestCase(BowlingGameTestCaseBase):
    def setUp(self):
        self.target = BowlingGame()

    def test_game_starts_at_frame_1(self):
        self.assertEqual(self.target.current_frame_number, 1)

    def test_game_starts_at_in_progress_state(self):
        self.assertEqual(self.target.game_state, GameStates.InProgress)

    def test_game_finishes_after_10_frames(self):
        for i in range(20):
            self.target.roll(1)

        self.assertEqual(self.target.current_frame_number, 10)
        self.assertEqual(self.target.game_state, GameStates.Finished)

    def test_cannot_roll_after_game_is_finished(self):
        for i in range(20):
            self.target.roll(1)
        self.assertEqual(self.target.game_state, GameStates.Finished)
        with self.assertRaises(BowlingGameFinishedException):
            self.target.roll()

    def test_game_remembers_last_knocked_down_pin_count(self):
        self.target.roll(1)
        self.assertEqual(self.target.last_throw_count, 1)

    def test_roll_without_specified_score_knocks_down_random_pins(self):
        self.target.roll()
        self.assertBetween(self.target.last_throw_count, 0, 10)

    def test_knock_down_less_than_10_pins_frame_does_not_change(self):
        self.target.roll(5)
        self.assertBetween(self.target.current_frame_number, 0, 10)

    def test_frame_advances_after_two_rolls(self):
        self.target.roll(5)
        self.target.roll(2)
        self.assertEqual(self.target.current_frame_number, 2)

    def test_strike_in_first_roll_of_frame_ends_frame(self):
        self.target.roll(10)
        self.assertEqual(self.target.current_frame_number, 2)

    def test_bowling_game_str(self):
        self.assertEqual(str(self.target), '<BowlingGame frm: {0}>'.format(1))
        self.target.roll(1)
        self.target.roll(1)
        self.assertEqual(str(self.target), '<BowlingGame frm: {0}>'.format(2))
Example #28
0
 def setUp(self):
     """setup test"""
     self.game = BowlingGame()
Example #29
0
class BowlingGameScoringTestCase(TestCase):
    def setUp(self):
        self.target = BowlingGame()

    def test_all_gutter_rolls_result_in_zero_score(self):
        for i in range(20):
            self.target.roll(0)

        self.assertEqual(self.target.score, 0)

    def test_all_1_rolls_result_in_20_score(self):
        for i in range(20):
            self.target.roll(1)

        self.assertEqual(self.target.score, 20)

    def test_open_frame_score_is_number_of_pins_knocked_down(self):
        self.target.roll(3)
        self.target.roll(3)  # end of first frame. open frame.
        self.target.roll(3)
        self.assertEqual(self.target.score, 9)

    def test_spare_frame_score_is_own_score_plus_next_roll(self):
        self.target.roll(7)
        self.target.roll(3)  # end of first frame. spare.
        self.target.roll(7)
        self.target.roll(2)  # end of second frame.
        self.assertEqual(self.target.score, 26)

    def test_spare_then_3_then_all_misses_score_16(self):
        self.target.roll(2)
        self.target.roll(8)
        self.target.roll(3)
        for i in range(17):
            self.target.roll(0)

        self.assertTrue(self.target.game_state, GameStates.Finished)
        self.assertEqual(self.target.score, 16)

    def test_strike_frame_score_is_own_score_plus_next_two_rolls(self):
        self.target.roll(10)
        self.assertEqual(self.target.score, 10)
        self.target.roll(3)
        self.target.roll(7)
        self.assertEqual(self.target.score, 20)

    def test_strike_then_three_then_four_then_all_misses_score_24(self):
        self.target.roll(10)
        self.target.roll(3)
        self.target.roll(4)
        for i in range(16):
            self.target.roll(0)

        self.assertTrue(self.target.game_state, GameStates.Finished)
        self.assertEqual(self.target.score, 24)

    def test_perfect_game_scores_300(self):
        for i in range(12):
            self.target.roll(10)

        self.assertEqual(self.target.score, 300)

    def test_spare_frame_score_not_counted_until_next_throw(self):
        self.target.roll(7)
        self.target.roll(3)
        self.assertEqual(self.target.score, 0)
        self.target.roll(2)
        self.assertEqual(self.target.score, 14)

    def test_strike_in_the_last_frame_gives_two_more_rolls(self):
        for i in range(18):
            self.target.roll(0)
        #self.assertEqual(self.target.score, 0)
        self.assertEqual(self.target.current_frame_number, 10)

        self.target.roll(10)
        #self.assertEqual(self.target.score, 10)
        self.assertEqual(self.target.game_state, GameStates.InProgress)

        self.target.roll(10)
        self.assertEqual(self.target.game_state, GameStates.InProgress)
        self.assertEqual(self.target.score, 20)

        self.target.roll(10)
        self.assertEqual(self.target.game_state, GameStates.Finished)
        self.assertEqual(self.target.score, 30)
Example #30
0
 def roll_new_game(self, rolls):
     game = BowlingGame()
     for roll in rolls:
         game.roll(roll)
     return game
Example #31
0
 def setUp(self):
     self.target = BowlingGame()
Example #32
0
class BowlingGameTests(unittest.TestCase):
    def setUp(self):
        self.game = BowlingGame()
    
    def roll_many_times(self, pins, rolls):
        for _ in range(rolls):
            self.game.roll(pins)

    def roll_spare(self):
        self.game.roll(5)
        self.game.roll(5)

    def roll_strike(self):
        self.game.roll(10)

    def test_can_create_BowlingGame(self):
        pass

    def test_when_all_throws_in_gutter_then_score_should_be_zero(self):
        self.roll_many_times(pins=0, rolls=20)
        self.assertEqual(self.game.score(), 0)

    def test_when_no_bonus_points_then_score_should_be_sum_of_rolls(self):
        self.roll_many_times(pins=1, rolls=20)
        self.assertEqual(self.game.score(), 20)

    def test_when_spare_then_bonus_for_next_roll(self):
        self.roll_spare()
        self.roll_many_times(pins=1, rolls=18)
        self.assertEqual(self.game.score(), 11 + 18)

    def test_when_strike_then_bonus_for_two_next_rolls(self):
        self.roll_strike()
        self.roll_many_times(pins=1, rolls=18)
        self.assertEqual(self.game.score(), 12 + 18)

    def test_when_perfect_game_then_score_should_be_300(self):
        self.roll_many_times(pins=10, rolls=12)
        self.assertEqual(self.game.score(), 300)
Example #33
0
 def test_score_output_type(self):
     """
         Own Test 9: Test output type.
     """
     test_game = BowlingGame("12 12 12 12 12 12 12 12 X 12")
     self.assertIsInstance(test_game.score(), int)
Example #34
0
 def setUp(self):
     self.game = BowlingGame()
     try:
         self.assertRaisesRegex
     except AttributeError:
         self.assertRaisesRegex = self.assertRaisesRegexp
Example #35
0
 def setUp(self):
     self.bowling_game = BowlingGame()
Example #36
0
 def _roll_many(self, rolls):
     game = BowlingGame()
     for pins in rolls:
         game.roll(pins)
     return game
Example #37
0
def main():
    new_bowling_game = BowlingGame("1- 2- 3- 4- 5- 6- 7- 8- 9- 9-")
    print(new_bowling_game.score())