Here's my code, any ways in shortening it?

This is a discussion on Here's my code, any ways in shortening it? within the C++ Programming forums, part of the General Programming Boards category; Okay heres the code...Ive just finished programming this, is there any way to save space. It seems a bit big. ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    15

    Here's my code, any ways in shortening it?

    Okay heres the code...Ive just finished programming this, is there any way to save space. It seems a bit big. Also I deleted my name from privacy reasons.

    Code:
    //This program will play ASCLLAND - Intermediate Division
    //Look at ICT 11 "Switch Statements" for rules.
    
    #include <iostream.h>
    
    void ASCLLAND();		//Plays the game ASCLLAND
    
    int main()
    
    {
    	ASCLLAND();
    	return(0);
    	
    }
    
    void ASCLLAND()
    //Pre:Takes moves in from user
    //Post:Outputs location based on rules of ASCLLAND
    {
    	bool WhichPlayer;		//To decide which player is rolling
    	int DiceRoll;			//The Amount of spaces moved
    	int PlayerA;			//Where Player A is
    	int PlayerB;			//Where Player B is
    	
    	DiceRoll = 0;
    	PlayerA = 0;
    	PlayerB = 0;
    	WhichPlayer = true;
    
    //It continues to loop until a 0 is entered
    	do
    	{
    //To ensure that the user only inputs a number from 0-8
    //Also adds up users moves
    		do
    		{
    			if (WhichPlayer == true)
    			{
    				cout << "Enter Move for Player A:(1-8, 0 to exit) ";
    				cin >> DiceRoll;
    			}
    			else if (WhichPlayer == false)
    			{
    				cout << "Enter Move for Player B:(1-8, 0 to exit) ";
    				cin >> DiceRoll;
    			}
    				
    //Switch statement to find out the spaces moved the players make
    			switch(DiceRoll)
    			{
    			case 1:
    			case 2:
    			case 3:
    			case 5:
    			case 7:
    			case 8:
    				{
    					//If statement is true, it will add up the dice roll
    					//If Player A exceeds 40, s/he is automatically brought back to 40
    					//Makes the statement false to allow player B to have next roll
    					if (WhichPlayer == true)
    					{
    						if (PlayerA >= 40 || PlayerB >= 40)
    						{break;}
    						else
    						{
    							PlayerA += DiceRoll;
    							WhichPlayer = false;
    						break;
    						}
    					}
    					//If statement is false, it will add up dice roll for player B
    					//If Player B exceeds 40, s/he is automatically brought back to 40
    					//Makes the statement true so the next move is player A's.
    					else if (WhichPlayer == false)
    					{
    						if (PlayerA >= 40 || PlayerB >= 40)
    						{break;}
    						else
    						{
    							PlayerB += DiceRoll;
    							WhichPlayer = true;
    							break;
    						}
    					}
    				}
    			case 4:
    			case 6:
    				{
    					//Same as above but with subtraction
    					if(WhichPlayer == true)
    					{
    						PlayerA -= DiceRoll;
    						if (PlayerA < 0)
    						{
    							PlayerA = 0;
    						}
    						WhichPlayer = false;
    						break;
    					}
    					else if (WhichPlayer == false)
    					{
    						PlayerB -= DiceRoll;
    						if (PlayerB < 0)
    						{
    							PlayerB = 0;
    						}
    						WhichPlayer = true;
    						break;
    					}
    				}
    			case 0:
    				{
    					break;
    				}
    			}
    
    			//If player A lands on player B or vice versa
    			//The player that first there returns to start(rules of game)
    			if(PlayerA == PlayerB)
    			{
    				if(WhichPlayer == true)
    				{
    					PlayerA = 0;
    				}
    				else if (WhichPlayer == false)
    				{
    					PlayerB = 0;
    				}
    			}
    
    		}
    		while(DiceRoll > 8 || DiceRoll < 0);
    
    	}
    	while(DiceRoll != 0);
    
    	if (PlayerA >= 40)
    	{
    		cout << "A - End" << endl;
    	}
    	else
    	{
    		cout << "A - " << PlayerA << endl;
    	}
    	
    
    	if (PlayerB >= 40)
    	{
    		cout << "B - End" << endl;
    	}
    	else
    	{
    		cout << "B - " << PlayerB << endl;
    	}
    }

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Meh, not too much... removed some of those redundant braces... I don't know if I broke anything, so don't complain

    Code:
    /This program will play ASCLLAND - Intermediate Division
    //Look at ICT 11 "Switch Statements" for rules.
    
    #include <iostream>
    
    void ASCLLAND();		//Plays the game ASCLLAND
    
    int main()
    {
    	ASCLLAND();
    	return(0);
    	
    }
    
    
    int GetRoll(bool which)
    {
    	int ret;
    	std::cout << "Enter move for player " << static_cast<int>(which)+1 << ", 1-8, 0 to exit";
    	while (std::cin >> ret) {}
    	return ret;
    }
    
    void ASCLLAND()
    //Pre:Takes moves in from user
    //Post:Outputs location based on rules of ASCLLAND
    {
    	using namespace std;
    	bool WhichPlayer = true;	//To decide which player is rolling
    	int DiceRoll = 0;		//The Amount of spaces moved
    	int PlayerA = 0;		//Where Player A is
    	int PlayerB = 0;		//Where Player B is
    	
    	//It continues to loop until a 0 is entered
    	do
    	{
    		//To ensure that the user only inputs a number from 0-8
    		//Also adds up users moves
    		do
    		{
    			switch (GetRoll(WhichPlayer))
    			{
    			case 1:
    			case 2:
    			case 3:
    			case 5:
    			case 7:
    			case 8:
    				//If statement is true, it will add up the dice roll
    				//If Player A exceeds 40, s/he is automatically brought back to 40
    				//Makes the statement false to allow player B to have next roll
    				if (WhichPlayer == true)
    				{
    					if (PlayerA >= 40 || PlayerB >= 40)
    						break;
    					else {
    						PlayerA += DiceRoll;
    						WhichPlayer = false;
    						break;
    					}
    				}
    				//If statement is false, it will add up dice roll for player B
    				//If Player B exceeds 40, s/he is automatically brought back to 40
    				//Makes the statement true so the next move is player A's.
    				else if (WhichPlayer == false)
    				{
    					if (PlayerA >= 40 || PlayerB >= 40)
    						break;
    					else {
    						PlayerB += DiceRoll;
    						WhichPlayer = true;
    						break;
    					}
    				}
    			case 4:
    			case 6:
    				//Same as above but with subtraction
    				if(WhichPlayer == true)
    				{
    					PlayerA -= DiceRoll;
    					if (PlayerA < 0)
    						PlayerA = 0;
    					WhichPlayer = false;
    					break;
    				}
    				else if (WhichPlayer == false)
    				{
    					PlayerB -= DiceRoll;
    					if (PlayerB < 0)
    						PlayerB = 0;
    					WhichPlayer = true;
    					break;
    				}
    			case 0:
    				break;
    			}
    
    			//If player A lands on player B or vice versa
    			//The player that first there returns to start(rules of game)
    			if(PlayerA == PlayerB)
    			{
    				if(WhichPlayer == true)
    					PlayerA = 0;
    				else if (WhichPlayer == false)
    					PlayerB = 0;
    			}
    
    		} while (DiceRoll > 8 || DiceRoll < 0);
    
    	} while (DiceRoll != 0);
    
    	if (PlayerA >= 40)
    		cout << "A - End" << endl;
    	else
    		cout << "A - " << PlayerA << endl;
    	
    
    	if (PlayerB >= 40)
    		cout << "B - End" << endl;
    	else
    		cout << "B - " << PlayerB << endl;
    }

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Pffffff come on, Eibro! You can do better than that!!!

    Code:
    #include <iostream>
    
    void ASCLLAND();
    std::ostream& OutputScore( char, int );
    
    int main()
    {
        ASCLLAND();
        return 0;
    }
    
    void ASCLLAND()
    {
        bool WhichPlayer = true;
        int DiceRoll = 0, PlayerA = 0, PlayerB = 0;
        do
        {
            std::cout << "Enter Move for Player " << ( WhichPlayer ? 'A' : 'B' ) << ":(1-8, 0 to exit) ";
            std::cin >> DiceRoll;
            if( ( DiceRoll == 4 || DiceRoll == 6 ) && ( ( ( WhichPlayer ) ? PlayerA : PlayerB ) -= DiceRoll ) < 0 ) ( ( WhichPlayer = !WhichPlayer ) ? PlayerB : PlayerA ) = 0;
            else if( DiceRoll > 0 && DiceRoll < 9 && ( PlayerA < 40 && PlayerB < 40 ) ) ( ( WhichPlayer = !WhichPlayer ) ? PlayerB : PlayerA ) += DiceRoll;
            if( PlayerA == PlayerB ) ( WhichPlayer ? PlayerA : PlayerB ) = 0;
        } while( DiceRoll > 8 || DiceRoll < 0 || DiceRoll );
        OutputScore( 'A', PlayerA ) << std::endl;
        OutputScore( 'B', PlayerB ) << std::endl;
    }
    
    std::ostream& OutputScore( char Player, int Score )
    {
        std::cout << Player << " - ";
        if( Score >= 40 ) return std::cout << "End";
        else return std::cout << Score;
    }
    Yeah, it works.

    What can I say, I was bored :p

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    #include <iostream>void ASCLLAND();std::ostream& OutputScore( char, int );int main(){    ASCLLAND();    return 0;}void ASCLLAND(){    bool WhichPlayer = true;    int DiceRoll = 0, PlayerA = 0, PlayerB = 0;    do    {        std::cout << "Enter Move for Player " << ( WhichPlayer ? 'A' : 'B' ) << ":(1-8, 0 to exit) ";        std::cin >> DiceRoll;        if( ( DiceRoll == 4 || DiceRoll == 6 ) && ( ( ( WhichPlayer ) ? PlayerA : PlayerB ) -= DiceRoll ) < 0 ) ( ( WhichPlayer = !WhichPlayer ) ? PlayerB : PlayerA ) = 0;        else if( DiceRoll > 0 && DiceRoll < 9 && ( PlayerA < 40 && PlayerB < 40 ) ) ( ( WhichPlayer = !WhichPlayer ) ? PlayerB : PlayerA ) += DiceRoll;        if( PlayerA == PlayerB ) ( WhichPlayer ? PlayerA : PlayerB ) = 0;    } while( DiceRoll > 8 || DiceRoll < 0 || DiceRoll );    OutputScore( 'A', PlayerA ) << std::endl;    OutputScore( 'B', PlayerB ) << std::endl;}std::ostream& OutputScore( char Player, int Score ){    std::cout << Player << " - ";    if( Score >= 40 ) return std::cout << "End";    else return std::cout << Score;}
    What can I say? I was even more bored

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Haha, hey, you can't just copy mine and dump it on one line! That's cheating!

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Now it's just silly, but still neat to see how much a program can be shortened...

    Code:
    #include <iostream>
    using namespace std;
    
    void O( char P, int S )
    {
        cout << P << " - ";
        if( S >= 40 ) cout << "End";
        else cout << S;
        cout << endl;
    }
    
    int main()
    {
        bool P = true;
        int D, A = 0, B = 0;
        do
        {
            cout << "Enter Move for Player " << ( P ? 'A' : 'B' ) << ":(1-8, 0 to exit) ";
            cin >> D;
            if( ( D == 4 || D == 6 ) && ( ( P ? A : B ) -= D ) < 0 ) ( ( P = !P ) ? B : A ) = 0;
            else if( D > 0 && D < 9 && A < 40 && B < 40 ) ( ( P = !P ) ? B : A ) += D;
            if( A == B ) ( P ? A : B ) = 0;
        } while( D );
        O( 'A', A );
        O( 'B', B );
        return 0;
    }
    This keeps the same input and output as you originally had and works the same way to the user.
    Last edited by Polymorphic OOP; 01-21-2003 at 01:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21