-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
62 lines (52 loc) · 1.49 KB
/
main.cpp
File metadata and controls
62 lines (52 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
class BowlingGame {
public:
void roll(int pins) {
rolls.push_back(pins);
}
int score() {
int totalScore = 0;
int rollIndex = 0;
for (int frame = 0; frame < 10; ++frame) {
if (isStrike(rollIndex)) {
totalScore += 10 + strikeBonus(rollIndex);
rollIndex += 1;
} else if (isSpare(rollIndex)) {
totalScore += 10 + spareBonus(rollIndex);
rollIndex += 2;
} else {
totalScore += sumOfBallsInFrame(rollIndex);
rollIndex += 2;
}
}
return totalScore;
}
private:
std::vector<int> rolls;
bool isStrike(int rollIndex) {
return rolls[rollIndex] == 10;
}
bool isSpare(int rollIndex) {
return rolls[rollIndex] + rolls[rollIndex + 1] == 10;
}
int strikeBonus(int rollIndex) {
return rolls[rollIndex + 1] + rolls[rollIndex + 2];
}
int spareBonus(int rollIndex) {
return rolls[rollIndex + 2];
}
int sumOfBallsInFrame(int rollIndex) {
return rolls[rollIndex] + rolls[rollIndex + 1];
}
};
int main() {
BowlingGame game;
// Sample input from the example image (frame rolls)
int inputRolls[] = {1, 4, 4, 5, 6, 4, 5, 5, 10, 0, 1, 7, 3, 6, 4, 10, 2, 8, 6};
for (int pins : inputRolls) {
game.roll(pins);
}
std::cout << "Total Score: " << game.score() << std::endl;
return 0;
}