Cleaning up my code.....

Started by
11 comments, last by random-decay 22 years, 6 months ago
Hey everyone. THis is a finished version of Craps, but I have a tiny favor to ask. Right after the main game loop is finished, I display a frequency table of the rolls that were, well, rolled. It''s a bunch of if statments determining the formatting of the percentages. How the heck would I decrease the size of this / put it in a function somehow? Thanks
  
//----------------------------------------------------------------------------

//	driver.cpp

//	This program simulates the street version of Craps.  The player starts

//  the game with $100 in their pot.  They are allowed to bet, but they can''t

//  bet less than $2 or more than their pot amount.  Here are the rules to 

//  the game:  Each roll is started by rolling 2 six-sided dice.  If the throw

//  total on the first roll is 7 or 11, the player wins.  If the throw total 

//  on the first roll is 2, 3, or 12, the player loses.  Any other throw total

//  becomes the player''s ''point.''  The player must roll the ''point'' in order

//  to win, but if they get a 7 before that, the player loses.

//	

//	Project: Craps

//----------------------------------------------------------------------------


// I/O

#include <iostream>

// Reading/writing files

#include <fstream>

// Needed for rand and srand

#include <cstdlib>

// Needed for time

#include <ctime>

// Needed for clearing the screen

#include <stdlib.h>

using namespace std;

// Function prototypes

int Random1To6( void );
bool PlayAgain( void );

// Game states

enum Status { CONTINUE, WON, LOST };

// Constants

const int NUM_COMBOS = 11;
const int OFF_SET    = 2;

//----------------------------------------------------------------------------

//  Function: main

//  Return:   int indicating exit status of program

//  Owner: 	  Joe Ridino

//----------------------------------------------------------------------------

int main( void )
{
	// Status of the game

	Status gameStatus;

	// Main game loop variable

	bool stillPlaying = true;

	// An array of ints for the roll frequency

	int rollFreq[NUM_COMBOS] = { 0 };

	int bet, dice1, dice2, point, throwTotal, totalRolls = 0;
	
	// Set player''s pot to $100

	int pot = 100;
	
	// Seed the randomizer

	srand( static_cast< unsigned >( time( NULL ) ) );

	cout << "CRAPS!" << endl << endl;

	// Main game loop

	while ( stillPlaying )
	{
		cout << "Pot = " << pot << endl;
		
		// Ask player to bet - must be >= 2 or <= pot

		do
		{
			cout << "Enter bet: ";
			cin >> bet;
		}
		while ( bet < 2 || bet > pot );

		// Throw the dice and add up the total

		dice1 = Random1To6();
		dice2 = Random1To6();
		throwTotal = dice1 + dice2;

		totalRolls++;

		// Add up the number of times a roll is thrown

		rollFreq[throwTotal - OFF_SET]++;

		cout << "Roll = ( " << dice1 << " , " << dice2 << " ) ";
		cout << throwTotal << endl;
		

		// The first roll

		switch ( throwTotal )
		{
			case 7:
			case 11:
				gameStatus = WON;
			break;

			case 2:
			case 3:
			case 12:
				gameStatus = LOST;
			break;

			default:
				gameStatus = CONTINUE;
				point = throwTotal;
				cout << "Point = " << point << endl;
			break;
		}

		// The player has point

		while ( gameStatus == CONTINUE )
		{
			// Throw the dice and add up the total

			dice1 = Random1To6();
			dice2 = Random1To6();
			throwTotal = dice1 + dice2;
			
			totalRolls++;
			
			// Add up the number of times a roll is thrown

			rollFreq[throwTotal - OFF_SET]++;

			cout << "Roll = ( " << dice1 << " , " << dice2 << " ) ";
			cout << throwTotal << endl;
		
			if ( throwTotal == point )
			{
				gameStatus = WON;
			}
			else if ( throwTotal == 7 )
			{
				gameStatus = LOST;
			}
			else
			{
				gameStatus = CONTINUE;
			}
		}

		// The player has won the game

		if ( gameStatus == WON )
		{
			pot += bet;
			cout << "Winner" << endl;
			stillPlaying = PlayAgain();
			system("cls");
		}
		
		// The player has lost the game

		if ( gameStatus == LOST )
		{
			pot -= bet;
			cout << "Loser" << endl;
			cout << "Pot = " << pot << endl;

			// Check to see if player busted

			if ( pot == 0 )
			{
				cout << "You''re outta money, chump!" << endl;
				
				stillPlaying = false;
				break;
			}

			stillPlaying = PlayAgain();
			system("cls");
		}

	} // End of main game loop

	
// CALCULATES THE FREQUENCY OF EACH ROLL - PUT THIS IN A FUNCTION

	cout << "Frequency:" << endl;

		if ( float ( rollFreq[0] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "2   " << float ( rollFreq[0] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[1] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "3   " << float ( rollFreq[1] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[2] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "4   " << float ( rollFreq[2] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[3] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "5   " << float ( rollFreq[3] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[4] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "6   " << float ( rollFreq[4] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[5] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "7   " << float ( rollFreq[5] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[6] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "8   " << float ( rollFreq[6] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[7] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "9   " << float ( rollFreq[7] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[8] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "10  " << float ( rollFreq[8] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[9] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "11  " << float ( rollFreq[9] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );

		if ( float ( rollFreq[10] ) * 100 / totalRolls != 0 )
		{
			cout.setf( ios_base::fixed );
			cout.precision( 2 );
		}

		cout << "12  " << float ( rollFreq[10] ) * 100 / totalRolls << "%" << endl;
		cout.unsetf( ios_base::fixed );


	return ( EXIT_SUCCESS );
}

//----------------------------------------------------------------------------

//  Function: Random1To6

//  Return:   int indicating the random number

//  Owner: 	  Joe Ridino

//----------------------------------------------------------------------------

int Random1To6( void )
{
	//add 1 to number since rand() % 6 returns only numbers 0-5

	return ( ( rand() % 6 ) + 1 );
}

//----------------------------------------------------------------------------

//  Function: PlayAgain

//  Return:   bool indicating if player wants to keep playing

//  Owner: 	  Joe Ridino

//----------------------------------------------------------------------------

bool PlayAgain( void )
{
	char choice;
	cout << "Do you want to continue (y,n)? ";
	cin >> choice;

	return ( choice == ''y'' );
}

  
Advertisement
after the end of main loop and before the return(EXIT_SUCCESS) replace that code with the following...

  cout << "Frequency:" << endl;for( int i = 0; i < NUM_COMBOS; i++ ){	float p = (float) rollFreq[i]*100/totalRolls;	if( p != 0 )	{		cout.setf( ios_base::fixed );		cout.precision( 2 );	}	cout << i+2 << "   " << p << "%" << endl;	cout.unsetf( ios_base::fixed );}  


i''ll leave making it into a function to you..

hope this helps..

-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..
Thanks a lot. How''s the code, overall?
it looks all right.... you have a few things that you have repeated in there...

for example isntead of...

dostuff();
while( 1 )
dostuff();

you can move the code around so you only have

while( 1 )
dostuff();

if you don''t understand what i''m saying ask me further and i''ll help you fix that.. the code IS correct, but you got some things that usually beginners do (like more code than you really need..some things can be written in a simpler way..etc)

-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..
I kinda understand, but please explain further....
do you have icq or e-mail?

-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..
email: mcguile257@yahoo.com
check your e-mail

-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..
Thanks. But I made the function a different way......

    void FrequencyTable( const int arr[], int arSize, int r ){	cout << "Frequency:" << endl;		for ( int i = 0; i < NUM_COMBOS; i++ )	{		float p = float ( arr[i] * 100 ) / r;				if ( p != 0 )		{			cout.setf( ios_base :: fixed );			cout.precision( 2 );		}		cout << i  + OFF_SET  << "   " << p << "%" << endl;		cout.unsetf( ios_base :: fixed );	}	}    


and then called it saying:
FrequencyTable( rollFreq, NUM_COMBOS, totalRolls );

Edited by - random-decay on October 13, 2001 9:18:41 PM
I''ll go with your way, even though it doesn''t really matter, right?

This topic is closed to new replies.

Advertisement