Created
May 7, 2025 07:03
-
-
Save VaradLanke/c1e6e1e0d3c6a57de5a19b5254508b38 to your computer and use it in GitHub Desktop.
Bowling Game code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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