Exemplo n.º 1
0
int main(void){
	double balance = 0, wager = 0;
	int choice = 0;

	printGameRules();
	balance = getBankBalance();
	
	//Display game menu and allow user to continually choose options until they quit
	while(choice != 4){
		displayGameMenu();
		choice = getChoice();
		switch (choice){
			case 1: 
				printf("Current bank balance: $%.2lf\n", balance);
				break;
			case 2: 
				balance = addMoney(balance);
				printf("You now have $%.2lf in the bank\n", balance);
				break;
			case 3:
				balance = playRound(balance);
				break;
			case 4:
				break;
			default:
				printf("\nMenu choice is invalid, Please choose a number between 1 and 4\n"); //Validates menu choice
				break;
		}

	}


	return 0;
}
/*************************************************************
 * Function: play ()                                         *
 * Date Created: September 29, 2012                          *
 * Date Last Modified: September 30, 2012                    *
 * Description: This function is the main function called to *
 *              start a game of craps.                       *
 * Input parameters: void                                    *
 * Returns: void                                             *
 * Preconditions: User knows the rules of the game of craps  *
 * Postconditions: User has doesn't want to continue playing *
 *                 or player is bankrupt.                    *
 *************************************************************/
void play (void)
{
	int dieOne = 0, dieTwo = 0,     /* values for two dice */
		sumDice = 0,                /* sum of two dice */
		playerPoint = 0,            /* player's point */
		gameStatus = 0,             /* status of game */
		numberOfRolls = 0,          /* tracks number of rolls */
		playMore = 0;               /* status indicator for more game */

	double initialBankBalance = 0.0,	/* value for initial bank balance */
		   balance = 0.0,               /* value for current bank balance */
		   wager = 0.0;                 /* value for wager or bet */


	/* Prompts user for inital bank balance */
	initialBankBalance = getBankBalance ();
	balance = initialBankBalance;
	
	do
	{
		do
		{
			/* Before each rool, prompt user for a wager */
			wager = getWagerAmount ();
		} while (!(checkWagerAmount (wager, balance)));


		/* A player rolls two dice. */
		printf ("<Press ENTER to roll the dice>");
		pressEnter ();

		/* Gets two random values from 1-6, and are 
		   saved on the variables dieOne and dieTwo */
		dieOne = rollDie ();
		dieTwo = rollDie ();

		/* Shows in the screen two animated die and 
		   then the the actual values of the dice */
		animateDices (dieOne, dieTwo);

		/* After the dice have come to rest, the sum 
		   of the spots on the two upward faces is 
		   calculated. */
		sumDice = calculateSumDice (dieOne, dieTwo);

		switch (isWinLossOrPoint (sumDice))
		{
			case WINS:
				/* If the sum is 7 or 11 on the first throw, 
				   the player wins. */
				gameStatus = WINS;
				break;

			case POINT:
				/* If the sum is 4, 5, 6, 8, 9, or 10 on the 
				   first throw, then the sum becomes the player's 
				   "point." */
				gameStatus = POINT;
				playerPoint = sumDice;

				do
				{
					/* Prompts player to roll die until player gets the point or craps */
					printf ("Continue rolling the dice until you get a sum of %d\n", playerPoint);
					printf ("<Press ENTER to roll the dice>");
					pressEnter ();

					/* gets two random values for dieOne and dieTwo */
					dieOne = rollDie ();
					dieTwo = rollDie ();

					/* animates two dice on the screen */
					animateDices (dieOne, dieTwo);

					/* gets the sum of the two dice */
					sumDice = calculateSumDice (dieOne, dieTwo);

					/* checks if user wins, craps or neither */
					gameStatus = isPointLossOrNeither (sumDice, playerPoint);

				} while (gameStatus == POINT);

				break;

			case CRAPS:
				/* If the sum is 2, 3, or 12 on the first throw 
				   (called "craps"), the player loses (i.e. the 
				   "house" wins). */
				gameStatus = CRAPS;
				break;
		}

		/* Once a game is loss or won, the bank 
		   balance should be adjusted. */
		balance = adjustBankBalance (balance, wager, gameStatus);
		
		/* Keeps track of the number of plays */
		numberOfRolls++;

		/* Prints out messages on screen */
		chatterMessages (numberOfRolls, gameStatus, initialBankBalance, balance);
		printf ("<Press ENTER to continue>");
		pressEnter ();

		if (balance > 0)
		{
			/* If balance is greater than 0,
			   Prompts player to play more or not */
			playMore = playAgain (balance);
		}
		else
		{
			playMore = 0;
		}

	} while (playMore);
	
}