Bowling Score

This actually isn't an assignment in a class. I am in Computer Science 1 where I am learning c++ and the other day I went bowling. I thought it would be cool to see if I could calc a bowling score since it gets a little complicated with strikes and spares. I still have a ton to learn so I would like feedback on what I have done so far. I am just now learning about how to use functions so that wasn't used here. I do think it could probably be broken up into smaller pieces though. Anyways I did get it all to work but now want to get a running total...I dont know where to start with that...Anyways here is what I have so far...

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*****************
 * Bowling Score *
 *****************/

#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;
}
Last edited on
You can edit your first post(as I can see you've already done with your second), so you don't have to double post.

@your code: it looks somewhat painful. You can collapse most of your code and greatly improve on your object oriented technique. However, does it work? And if so, great job =)
HA. It does work. You will get the correct score as long as you enter no more then 10 total pins per frame (which I did not add into the programming...Im sure that could be done pretty easily in order to not have a frame where someone entered more than 10 pins).

As far as the code goes. Where can I make improvements? I know that there are a ton to be made, but I dont know where to begin. This was my first attempt at something outside of what I have been doing in class. Im glad that it works, but know it is "somewhat painful" as you said....

I have just started learning about the used of functions (outside of the main function) and wonder if that would be a good start??

Also I know that its just plain wrong that I didn't comment on the code...I should probably go back through and do that...
I have just started learning about the used of functions (outside of the main function) and wonder if that would be a good start??


Yes, it would help you eliminate a huge amount of repetitive code in those for loops. Looking more closely...what are those for loops supposed to do?? They seem to "loop" over only one number...couldn't you just put them all into one loop?
There are so many loops cause I couldn't figure out how to count the additional rolls if there is a strike or a spare. Since a strike means that you count the next 2 rolls which could potentially mean it is counting the next 2 frames if strikes continue that is where I was starting to have problems. Then there is also the issue of a possible third roll in the 10th frame. So let me explain my code and maybe then someone can tell me how to do this more efficiently...

I have 3 array's (which I havent really learned about yet...so I may have used them wrong) the first array gets each roll for each frame in the first 2 loops. The reason that there are 2 loops to get the rolls is because of the the 10th frame and the fact that it could have 3 total rolls where the rest of them were only 2 rolls.

The second array gets a value for each frame depending on the result. It will assign 2 to the frameoutcome if it is a strike, 1 if it is a spare, and 0 if it is an open frame.

The last array is for totaling up each frame by finding out the outcome from the previous array. If the outcome was 0 then it will just sum the 2 rolls of the first frame. If it was a 1 then it will sum the 2 rolls of the first frame and then also count the first roll of the next frame. Lastly if it was a 2 then it will count the frame that the strike was thrown in, as well as the next 2 rolls whether that means just then next frame or possibly the first roll of the next 2 frames if strikes keep on getting thrown.

So the last 4 loops are for getting the totals as stated earlier by checking the frame outcome and then summing the appropriate frames/rolls. The reason that there are 4 loops is because the first one is set up to get the first 8 frames totals. Then the second is to get the 9th frame since it wouldn't be the same as the previous loop for instance if a strike was thrown in the 9th and then the first roll in the 10th was a strike then the 9th frame would be looking for an 11th frame. The third im now thinking wasn't necessary since its a loop that runs once...it really doesn't need to be a loop...or for that matter even the previous loop since it runs once as well...(writing this out helps me HA) Anyways the 10th frame would just total all three rolls even if there wasn't a 3rd roll since it would just be 0. The fourth loop is then to sum up the total score of the game.

Ok so that explanation might have been overkill...but I still would like any help or feedback that anyone has...
Well for your program I'd probably do something like this:

pseudo(ish):
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

include vector and iostream

start main loop

here ask for player names, I would do something like

int get_players(vector<string>& players){

    string name = "";

 while (1){
     cout << "\n\n>>";

     name = get_all_cin();

     if (name != "end"){
     players.push_back(name);
     }
     else if (players.size() < 1)
     cout << "\nHey man, you need at least one player!";
     else
     break;
 }
 return players.size();
}

then set up a way to grab all of the player's scores, again I'd use a vector. Probably a
vector<vector<int>>;

now all you need to do is loop through your players, ask for their score, if it's a 10, it's a strike, so go to next player's frame (unless it's the 10'th), and have a function that will count that player's total score, then display it. 

then at the end display everything in a nice formatted display 
No idea about vector yet...But I will read about that and see what I can do with that. I do think that I am going to at least try to break the existing code up into separate functions and then go from there...
Good plan man. If you need some help, just ask.
Here is the start of my revisions...


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
63
64
65
66
67
68
69
70
71
/*********************
 * Bowling Score # 2 *
 *********************/

#include <iostream>

using namespace std;

const int totalPins = 10;

int bowlArray[10][5] = {0};
int currentFrame = 0;
void getRoll();
int rollOne();
int rollTwo();
int rollThree();
int runningTotal();

int main(int argc, char** argv) {  

    int count;
    for(count = 0;currentFrame <=9; count++)
    {
        getRoll();
    }

    return 0;
}
void getRoll()
{
bowlArray[currentFrame][0] = rollOne();

    if (bowlArray[currentFrame][0] == totalPins)
    {
        cout << "STRIKE!" << endl;
        bowlArray[currentFrame][3] = bowlArray[currentFrame][0];
    }
    else
    {
        bowlArray[currentFrame][1] = rollTwo();
        if (bowlArray[currentFrame][0] + bowlArray[currentFrame][1] == totalPins)
        {
            bowlArray[currentFrame][3] = bowlArray[currentFrame][0] + bowlArray[currentFrame][1];
            cout << "SPARE!" << endl;
        }
        else
        {
            bowlArray[currentFrame][3] = bowlArray[currentFrame][0] + bowlArray[currentFrame][1];
        }
    }
    cout << runningTotal() << endl;
    currentFrame++;
}
int rollOne()
{
    int roll = 0;
    cout << "Input roll 1 for frame number " << currentFrame + 1 << endl;
    cin >> roll;
    return roll;
}
int rollTwo()
{
    int roll = 0;
    cout << "Input roll 2 for frame number " << currentFrame + 1 << endl;
    cin >> roll;
    return roll;
}
int runningTotal()
{
    return 0;
}
Topic archived. No new replies allowed.