Skip to content

Instantly share code, notes, and snippets.

@VaradLanke
Created May 7, 2025 07:03
Show Gist options
  • Save VaradLanke/c1e6e1e0d3c6a57de5a19b5254508b38 to your computer and use it in GitHub Desktop.
Save VaradLanke/c1e6e1e0d3c6a57de5a19b5254508b38 to your computer and use it in GitHub Desktop.
Bowling Game code
#include <iostream>
#include <vector>
using namespace std;
struct Frame {
int firstThrow;
int secondThrow;
int thirdThrow;
bool isStrike;
bool isSpare;
};
int calculateScore(const vector<Frame>& frames) {
int totalScore = 0;
for (int i = 0; i < frames.size(); ++i) {
totalScore += frames[i].firstThrow + frames[i].secondThrow;
if (frames[i].isSpare && i < 9) {
totalScore += frames[i + 1].firstThrow;
}
if (frames[i].isStrike && i < 9) {
totalScore += frames[i + 1].firstThrow +
(frames[i + 1].isStrike && i + 1 < 9 ? frames[i + 2].firstThrow : frames[i + 1].secondThrow);
}
if (i == 9) {
totalScore += frames[i].thirdThrow;
}
}
return totalScore;
}
int main() {
vector<Frame> frames(10);
cout << "Enter scores for 10 frames (format: first second [third for 10th]):\n";
for (int i = 0; i < 10; ++i) {
cin >> frames[i].firstThrow >> frames[i].secondThrow;
frames[i].thirdThrow = (i == 9 ? (frames[i].firstThrow == 10 || frames[i].firstThrow + frames[i].secondThrow == 10 ? cin.get() : 0) : 0);
frames[i].isStrike = (frames[i].firstThrow == 10);
frames[i].isSpare = (!frames[i].isStrike && frames[i].firstThrow + frames[i].secondThrow == 10);
}
cout << "Total Score: " << calculateScore(frames) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment