(C++) File bowling.txt 2 8 1 X X 3 / 4 5 1 / X X 3 / 4 / 8 X X X X X X X X X X 9
ID: 3750979 • Letter: #
Question
(C++) File bowling.txt
2
8 1 X X 3 / 4 5 1 / X X 3 / 4 / 8
X X X X X X X X X X 9 /
The game of bowling consists of ten frames. For each frame, ten pins are set at the end of a lane. The player’s objective is to knock down all the pins by rolling a ball down the lane. The bowler is allowed up to two rolls to knock down all the pins. If the bowler knocks them all down on the first attempt, the frame is scored as a strike. If the bowler does not knock them down on the first attempt in the frame the bowler is allowed a second attempt to knock down the remaining pins. If the bowler succeeds in knocking the rest of the pins down in the second attempt, the frame is scored as a spare.
The score for a bowling game consists of a sum of the scores for each frame. The score for frames 1-9 will be the total number of pins knocked down in the frame, plus bonuses for strikes and spares. If a bowler scores a strike in a frame, the score for that frame is ten plus the sum of the next two rolls. If a bowler scores a spare in a frame, the score for that frame is ten plus the score of the next roll.
If a bowler scores a strike in the tenth (final) frame, the bowler is allowed two more rolls; a bowler scoring a spare in the tenth frame is allowed one more roll. The score for frame 10 is the total number of pins knocked down in that frame. The maximum possible score in a game of bowling (strikes in all ten frames plus two extra strikes for the tenth frame strike) is 300. Write a C++ program to score a bowling match.
Input will be from a datafile. The first line will be a single integer, n, indicating the number of games to be scored. Each of the next n lines will represent a single game and will consist of all the rolls for that game. The outcome of each roll will be indicated with an integer representing the number of pins knocked down, a '/ ' indicates a spare, or a 'X' for a strike. Output the frame/total game score using the format shown below. Enter the file name interactively from the keyboard. Use user-defined functions/methods in your program. Do not use break or continue statements. Refer to the sample output below.
Sample Run Enter the file name: bowling. txt Game 1: Frames 18 8-1 X 34-5 1/X 34/8 75 95 118138 152 17 Total Score: 170 Game 2: Frames 18 3e 180 210 24 269 289 Total Score: 289Explanation / Answer
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int frame[10][3] = {{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0,0}};
int frameOutcome[10];
int frameTotal[10];
int currentFrame = 0;
int totalScore = 0;
do
{
cout << "Input roll 1 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][0];
cout << endl;
if (frame[currentFrame][0] == 10)
{
cout << "STRIKE" << endl;
frameOutcome[currentFrame] = 2;
currentFrame++;
}
else
{
cout << "Input roll 2 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][1];
cout << endl;
if (frame[currentFrame][0] + frame[currentFrame][1] == 10)
{
cout << "SPARE!" << endl;
frameOutcome[currentFrame] = 1;
currentFrame++;
}
else
{
cout << "The total for frame " << currentFrame + 1 <<
" is: " << frame[currentFrame][0] + frame[currentFrame][1] << endl;
frameOutcome[currentFrame] = 0;
currentFrame++;
}
}
}
while (currentFrame < 9);
do
{
cout << "Input roll 1 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][0];
cout << endl;
if (frame[currentFrame][0] == 10)
{
cout << "STRIKE" << endl;
frameOutcome[currentFrame] = 2;
cout << "Input roll 2 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][1];
cout << endl;
cout << "Input roll 3 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][2];
cout << endl;
currentFrame++;
}
else
{
cout << "Input roll 2 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][1];
cout << endl;
if (frame[currentFrame][0] + frame[currentFrame][1] == 10)
{
cout << "SPARE!" << endl;
frameOutcome[currentFrame] = 1;
cout << "Input roll 3 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][2];
cout << endl;
currentFrame++;
}
else
{
cout << "The total for frame " << currentFrame + 1 <<
" is: " << frame[currentFrame][0] + frame[currentFrame][1] << endl;
frameOutcome[currentFrame] = 0;
currentFrame++;
}
}
}
while (currentFrame < 10);
for (currentFrame = 0; currentFrame < 8 ; currentFrame++)
{
switch(frameOutcome[currentFrame])
{
case 0:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1];
break;
case 1:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1] +
frame[currentFrame + 1][0];
break;
case 2:
if(frameOutcome[currentFrame + 1] == 2)
{
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame + 1][0] +
frame[currentFrame + 2][0];
}
else
{
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame + 1][0] +
frame[currentFrame + 1][1];
}
break;
}
}
for (currentFrame = 8; currentFrame < 9 ; currentFrame++)
{
switch(frameOutcome[currentFrame])
{
case 0:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1];
break;
case 1:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1] +
frame[currentFrame + 1][0];
break;
case 2:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame + 1][0] +
frame[currentFrame + 1][1];
break;
}
}
for (currentFrame = 9; currentFrame < 10 ; currentFrame++)
{
switch(frameOutcome[currentFrame])
{
case 0:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1];
break;
case 1:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1] +
frame[currentFrame][2];
break;
case 2:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1] +
frame[currentFrame][2];
break;
}
}
for (currentFrame = 0; currentFrame < 10 ; currentFrame++)
{
totalScore = totalScore + frameTotal[currentFrame];
cout << "Frame " << currentFrame + 1 << " Total score: " <<
frameTotal[currentFrame];
cout << endl;
}
cout << "Total score = " << totalScore << endl;
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.