This project implements a simple bowling game in Dart. The game allows players to roll a ball and calculates the score based on standard bowling rules. It covers the basic bowling concepts like strikes, spares, and open frames. This document describes the game's structure, methods, and the corresponding test cases that verify its correctness.
A standard bowling game consists of 10 frames. In each frame:
- A player has two chances to knock down 10 pins unless a strike is rolled.
- A strike occurs when all 10 pins are knocked down in the first roll, granting the player 10 points plus the number of pins knocked down in the next two rolls.
- A spare occurs when all 10 pins are knocked down after two rolls, granting 10 points plus the pins knocked down in the next roll.
- An open frame is when fewer than 10 pins are knocked down after two rolls, and the player scores the total pins knocked down in the frame.
The maximum score for a perfect game is 300, which requires rolling 12 strikes (10 for the frames and 2 additional rolls for the final frame).
The Game
class simulates a bowling game, keeping track of rolls and calculating the score.
List<int> rolled
: A list of size 22 that stores the number of pins knocked down in each roll. It accommodates a maximum of 21 rolls (including bonus rolls for strikes in the 10th frame).int currentBall
: Keeps track of the current ball/roll in the game.
- Records the number of pins knocked down in each roll.
- Parameters:
pins
: Number of pins knocked down in the current roll.
- Calculates and returns the cumulative score of the game.
- Returns: The total score after all frames.
- Adds the score for a spare, including the next roll after the spare.
- Parameters:
score
: The current score.currentBall
: The index of the current frame’s first roll.
- Returns: The updated score including the spare bonus.
- Adds the score for a strike by including the next two rolls.
- Parameters:
score
: The current score.currentBall
: The index of the current frame’s first roll.
- Returns: The updated score including the strike bonus.
- Checks if the current frame is a spare.
- Parameters:
currentBall
: The index of the current frame’s first roll.
- Returns:
true
if the frame is a spare,false
otherwise.
- Checks if the current frame is a strike.
- Parameters:
currentBall
: The index of the current frame’s first roll.
- Returns:
true
if the frame is a strike,false
otherwise.
void main() {
Game game = Game();
game.roll(10); // Strike
game.roll(3);
game.roll(6);
print(game.getScore()); // Outputs the current score
}
The following test cases ensure that the bowling game logic works correctly under different scenarios.
The test cases are written using the package:test/test.dart
framework. Each test verifies that the Game
class behaves as expected by checking the game's score after various rolls.
A helper function to loop through multiple rolls in the game.
void rollAll(List<int> rolls) {
for (var pins in rolls) {
game.roll(pins);
}
}
test('do no thing', () {
expect(1, 1);
});
- Verifies that the test setup works.
test('should get score is zero when roll is zero', () {
game.roll(0);
expect(game.getScore(), 0);
});
- Verifies that the score remains 0 if no pins are knocked down.
test('open frame and add pins', () {
rollAll([2, 4]);
expect(game.getScore(), 6);
});
- Verifies that the score correctly adds the pins in an open frame.
test('when it is spare add next ball to score', () {
rollAll([4, 6, 3, 0]);
expect(game.getScore(), 16);
});
- Verifies that the next roll’s pins are added after a spare.
test('a ten in two frames is not a spare', () {
rollAll([3, 6, 4, 2]);
expect(game.getScore(), 15);
});
- Verifies that scoring 10 pins over two frames doesn't count as a spare.
test('a Strike adds two next balls', () {
rollAll([10, 3, 2]);
expect(game.getScore(), 20);
});
- Verifies that the next two rolls are added after a strike.
test('perfect game score is 300', () {
for (int i = 0; i < 12; i++) {
game.roll(10);
}
expect(game.getScore(), 300);
});
- Verifies the score for a perfect game (12 consecutive strikes).
test('random rolls', () {
rollAll([3, 4, 2, 3, 4, 1, 1, 2, 10, 2, 3]);
expect(game.getScore(), 40);
});
- Verifies the score after a series of random rolls.
For further details on this project and video tutorials, please refer to this YouTube playlist.
This documentation should provide a clear understanding of how the bowling game is implemented, how to use the Game
class, and how the test cases verify the logic.