Esempio n. 1
0
    def test_frames(self):
        # normal: 10 frames
        bg = BowlingGame()
        bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        self.assertEqual(bg.get_frame_count(), 10)
        self.assertEqual(bg.score(), 40)

        # normal: 11 frames
        bg = BowlingGame()
        bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        self.assertEqual(bg.get_frame_count(), 10)
        self.assertEqual(bg.score(), 40)

        # spare: 11 frames
        bg = BowlingGame()
        bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 2)
        self.assertEqual(bg.get_frame_count(), 10)
        self.assertEqual(bg.score(), 50)

        # strike: 11 frames
        bg = BowlingGame()
        bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        bg._roll_many(2, 2, 2, 2, 2, 2, 2, 2, 10, 4, 5)
        self.assertEqual(bg.get_frame_count(), 10)
        self.assertEqual(bg.score(), 64)
Esempio n. 2
0
    def test_strike_game(self):
        # start with a strike
        bg = BowlingGame()
        bg._roll_many(10, 5, 4)
        self.assertEqual(bg.score(), 28)
        self.assertEqual(bg.get_frame_count(), 2)

        # mid-game with a strike
        bg = BowlingGame()
        bg._roll_many(7, 1, 10, 3, 3)
        self.assertEqual(bg.score(), 30)
        self.assertEqual(bg.get_frame_count(), 3)

        # end-game with a strike
        bg = BowlingGame()
        bg._roll_many(4, 1, 10)
        self.assertEqual(bg.score(), 15)
        self.assertEqual(bg.get_frame_count(), 2)
Esempio n. 3
0
    def test_spare_game(self):
        # start with a spare
        bg = BowlingGame()
        bg._roll_many(4, 6, 5, 0)
        self.assertEqual(bg.score(), 20)
        self.assertEqual(bg.get_frame_count(), 2)

        # mid-game with a spare
        bg = BowlingGame()
        bg._roll_many(2, 3, 5, 5, 2, 7)
        self.assertEqual(bg.score(), 26)
        self.assertEqual(bg.get_frame_count(), 3)

        # end with a spare
        bg = BowlingGame()
        bg._roll_many(3, 5, 9, 1)
        self.assertEqual(bg.score(), 18)
        self.assertEqual(bg.get_frame_count(), 2)
Esempio n. 4
0
class TestBowlingGame(unittest.TestCase):
    def setUp(self):
        self.game = BowlingGame()

    def testAllGuttersReturns0(self):
        self.roll_times(0, 20)
        self.assertEquals(0, self.game.score())

    def testAll1sReturns20(self):
        self.roll_times(1, 20)
        self.assertEquals(20, self.game.score())

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

        self.roll_times(0, 17)
        self.assertEquals(20, self.game.score())

    def testItHandlesStrikes(self):
        self.roll_strike()
        self.game.roll(5)
        self.game.roll(3)

        self.roll_times(0, 17)
        self.assertEquals(26, self.game.score())

    def testPerfectGameIs300(self):
        self.roll_times(10, 12)
        self.assertEquals(300, self.game.score())

    def testDutch200Is200(self):
        for frame in range(10):
            self.roll_strike()
            self.roll_spare()
        self.assertEquals(200, self.game.score())

    def testGutterUntilLastFrameTurkeyIs30(self):
        self.roll_times(0, 18)
        self.roll_strike()
        self.roll_strike()
        self.roll_strike()
        self.assertEquals(30, self.game.score())

    def roll_spare(self):
        self.game.roll(1)
        self.game.roll(9)

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

    def roll_times(self, pins_toppled, times):
        for _ in range(times):
            self.game.roll(pins_toppled)
Esempio n. 5
0
 def test_normal_game(self):
     bg = BowlingGame()
     bg._roll_many(3, 6, 1, 7, 4, 4)
     self.assertEqual(bg.score(), 25)
     self.assertEqual(bg.get_frame_count(), 3)
Esempio n. 6
0
def testZero():
    game = BowlingGame()
    Assert.Equal(0, game.score())
Esempio n. 7
0
def testGutterGame():
    game = BowlingGame()
    for i in range(20):
        game.bowl(0)
    Assert.Equal(0, game.score())
Esempio n. 8
0
from random import randrange
from BowlingGame import BowlingGame

if __name__ == '__main__':
    bg = BowlingGame()

    # letting a random game play itself, just cause :)
    for frame in range(10):
        roll1 = randrange(0, 11)
        bg.roll(roll1)
        print 'Frame #{}: {}'.format(frame + 1, roll1)
        if roll1 == 10:  # strike
            if frame == 9:  # the 10th frame
                roll2 = randrange(0, 11)
                bg.roll(roll2)
                print 'Frame #{}: {}'.format(frame + 1, roll2)
                if roll2 == 10:  # another strike so roll 3 has 10 pins up
                    roll3 = randrange(0, 11)
                else:
                    roll3 = randrange(0, 10 - roll2)
                bg.roll(roll3)
                print 'Frame #{}: {}'.format(frame + 1, roll3)
            else:
                continue
        else:
            roll2 = randrange(0, 10 - roll1)
            print 'Frame #{}: {}'.format(frame + 1, roll2)
            bg.roll(roll2)
    print 'Final Score: {}'.format(bg.score())
    print 'Pins Toppled: {}'.format(bg.pins_toppled)