1. ## problem with object-oriented

This is a bowling score calculation program.

prof. said make this much more object-oriented but i hav no idea how

so, plz help me T.T

================================================== =

Code:
```#include <iostream>

using namespace std;

class game {
public:
int gamescore; //store the game score
int ball_1[12]; //1st ball
int ball_2[12]; //2nd ball
int e_ball; //bonus of 10th frame

void init();
void input();
void calc();
void showthescoreboard();
};

void game::init() {
for(int i = 0; i < 12; i++) {
ball_1[i] = 0;
ball_2[i] = 0;
}
e_ball = 0;
}

void game::input() { //enter the pins you get
cout << "Enter the number of the pins you throw down" << endl << endl;
for(int i = 1; i < 10; i++) {
cout << "FRAME  " << i << endl;
cout << "1st ball: ";
cin >> ball_1[i];

if(ball_1[i] == 10) { //strike
cout << "STRIKE!!" << endl;
cout << endl;
ball_2[i] = 0;
}

else if(ball_1[i] < 10) {
cout << "2nd ball: ";
cin >> ball_2[i];

if(ball_1[i]+ball_2[i] == 10) { //spare
}

else if(ball_1[i]+ball_2[i] > 10) {
cout << "ERROR!!" << endl;
i--;
}
cout << endl;
}

else {
cout << "ERROR!!" << endl;
cout << endl;
i--;
}
}

cout << "FRAME  " << 10 << endl;

while(1) {
cout << "1st ball: ";
cin >> ball_1[10];

if(ball_1[10] == 10) {
cout << "STRIKE!!" << endl;
cout << endl;
cout << "2nd ball: ";
cin >> ball_2[10];

if(ball_2[10] == 10) { //bonus
cout << "STRIKE!!" << endl;
cout << endl;
cout << "3rd ball: ";
cin >> e_ball;

if(e_ball == 10) {
cout << "STRIKE!!" << endl;
cout << endl;

break;
}

else if(e_ball < 10) {
break;
}

else {
cout << "ERROR!!" << endl;
cout << endl;
}
}

else if(ball_2[10] < 10) {
break;
}

else {
cout << "ERROR!!" << endl;
cout << endl;
}
}

else if(ball_1[10] < 10) {
cout << "2nd ball: ";
cin >> ball_2[10];

if(ball_1[10]+ball_2[10] == 10) { //bonus ball
cout << "3rd ball: ";
cin >> e_ball;

if(e_ball == 10) {
cout << "STRIKE!!" << endl;
cout << endl;

break;
}

else if(e_ball < 10) {
break;
}

else {
cout << "ERROR!!" << endl;
cout << endl;
}
}

else if(ball_1[10]+ball_2[10] < 10) {
e_ball = 0;

break;
}

else {
cout << "ERROR!!" << endl;
cout << endl;
}
}

else {
cout << "ERROR!!" << endl;
cout << endl;
}
}
}

void game::calc() { //calculator
int i = 0;
for(i = 1; i < 11; i++) {
if(i == 10) { //calculate 10th frame
gamescore += ball_1[10] + ball_2[10] + e_ball;
}
else {
if(ball_1[i] == 10) { //STRIKE
if(ball_1[i+1] == 10 && ball_1[i+2] == 10) { //turkey
gamescore += 30;
}
else if(ball_1[i+1] == 10 && ball_1[i+2] != 10) { //double
gamescore += 20 + ball_1[i+2];
}
else { //else
gamescore += 10 + ball_1[i+1] + ball_2[i+1];
}
}
else if (ball_1[i] < 10 && ball_1[i]+ball_2[i] == 10) {
gamescore += 10 + ball_1[i+1];
}
else {
gamescore += ball_1[i] + ball_2[i];
}
}
}
}

void game::showthescoreboard() {
cout << "Your total score is " << gamescore << endl;
}

int main(void) {
game play = {0};
cout << "Bowling Score" << endl << endl;
play.init();
play.input();
play.calc();
play.showthescoreboard();

return 0;
}```

2. OK. First of all indent so we can actually read your code.
Second more object oriented means add more ... objects
You only have one class, thus one object, game. You could make other ones as well. Like player which will store a players information. Like ball which will do anything that has to do with the balls. Game class could prompt for new players and initialize them. Then the players objects would call functions, like throw, which will use a ball object that will do your job.
Or something like this. Indent first

3. Also, game::init appears to be essentially a constructor, so perhaps you should make that into a constructor instead of a member function.

I agree with C_ntua that the code could be split into multiple objects, and some of your code that currently repeats several times (e.g. "if(ball == 10) STRIKE") could then be simplified.

--
Mats

4. Originally Posted by C_ntua
OK. First of all indent so we can actually read your code.
Second more object oriented means add more ... objects
You only have one class, thus one object, game. You could make other ones as well. Like player which will store a players information. Like ball which will do anything that has to do with the balls. Game class could prompt for new players and initialize them. Then the players objects would call functions, like throw, which will use a ball object that will do your job.
Or something like this. Indent first
srry, this is the first time I wrote the thread.

anyway thanks for the answer but still I don't get how...

think the way I made the code is wrong and should edit the whole code.. T.T

5. Originally Posted by matsp
Also, game::init appears to be essentially a constructor, so perhaps you should make that into a constructor instead of a member function.

I agree with C_ntua that the code could be split into multiple objects, and some of your code that currently repeats several times (e.g. "if(ball == 10) STRIKE") could then be simplified.

--
Mats

then,, how can I split the code??

6. One thing you should do is not use that bad smilie
And if you love coding rewriting the code shouldn't be a problem, since it ain't that long :P

then,, how can I split the code??