This Java class Game
simulates a game of bowling. It keeps track of rolls and calculates the total score based on the rules of bowling.
-
Setup
- Instantiate a
Game
object:Game game = new Game();
- Instantiate a
-
Rolling
- Use the
roll(int pins)
method to record the number of pins knocked down in each roll:game.roll(3); // Example roll with 3 pins knocked down
- Use the
-
Calculating Score
- At the end of the game, call the
score()
method to get the total score:int totalScore = game.score();
- At the end of the game, call the
-
Example
// Example usage: Game game = new Game(); int[] rolls = {3, 5, 10, 4, 6, 7, 2, 3, 10, 8, 1, 10, 10, 10, 9, 0}; for (int pins : rolls) { game.roll(pins); } int totalScore = game.score(); System.out.println("Total score: " + totalScore);
-
void roll(int pins)
- Records the number of pins knocked down in a roll.
-
int score()
- Calculates and returns the total score of the game.
Game
- Represents a game of bowling.
- Maintains a list of rolls and computes the score based on bowling rules.