# problem with object-oriented

Printable View

• 10-25-2008
sadvocal
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; }```
• 10-25-2008
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
• 10-25-2008
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
• 10-25-2008
sadvocal
Quote:

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
• 10-25-2008
sadvocal
Quote:

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

thanks for the answer,

then,, how can I split the code??

I'm so newb. please help me T.T
• 10-25-2008
Akkernight
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
• 10-25-2008
VirtualAce
Quote:

thanks for the answer,

then,, how can I split the code??

I'm so newb. please help me T.T
How about we turn the tide here?
• How would you split the code up?
• What objects do you see in the current code?
• Describe to us what you have in mind to do in order to make this code more object oriented.

If you can't think in terms of objects our help would just get you through one assignment which wouldn't help you learn anything. This question actually applies to any object oriented language which in my opinion makes it a good question. And this is a common task you will be doing should you pursue a career in programming. Refactoring existing code is definitely applicable in the real world.
• 10-25-2008
Elysia
Think of classes as objects, and think of your current code as a series of instructions that manipulates certain objects.
Like in Tic-tac-toe, you place Pieces (an object) on a board (another object).