Thread: problem with object-oriented

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Post 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; 
    }
    Last edited by sadvocal; 10-25-2008 at 08:02 AM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Quote Originally Posted by C_ntua View Post
    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. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Quote Originally Posted by matsp View Post
    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

  6. #6
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    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
    Currently research OpenGL

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 10-25-2008 at 11:16 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Binary Search Tree - object instantiation problem
    By patricio2626 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2006, 02:11 AM
  3. object oriented programming
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2006, 10:18 AM
  4. Object oriented job questions
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-28-2004, 05:06 AM
  5. Object Oriented Design help!! ;(
    By cpp4ever in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2001, 11:16 AM