PA4

main.c

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015, Section 01
//Programming Assignment: pa4
//Due: Oct. 7, 2015
//File Type: main function file.
//Purpose: ask user to input values and recall functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//A player rolls two dice.Each die has six faces.These faces contain 1, 2, 3, 4, 5, and 6 spots.
//After the dice have come to rest, the sum of the spots on the two upward faces is calculated.If the sum is 7 or 11 on the first throw, the player wins.
//If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses(i.e.the "house" wins).
//If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's "point."
//To win, you must continue rolling the dice until you "make your point." The player loses by rolling a 7 before making the point.
//Write a program that implements a craps game according to the above rules.
//The game should allow for wagering.This means that you need to prompt that user for an initial bank balance from which wagers will be added or subtracted.
//Before each roll prompt the user for a wager.
//Once a game is lost or won, the bank balance should be adjusted.As the game progresses, print various messages to create some "chatter" such as, 
//"Sorry, you busted!", or "Oh, you're going for broke, huh?", or "Aw cmon, take a chance!", or "You're up big, now's the time to cash in your chips!"
//Use the below functions to help you get started!You may define more than the ones suggested if you wish!
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "header.h"

int main(void){
	//declare variables.
	int menu = 0, result = 0, die1_value = 0, die2_value = 0, sum_dice = 0, rollAgain = 0, winLossPoint = 0, pointLossNeither = 0, prevRoll = 0, numRolls = 0, counter = 0, point_value = 0,point_value2 = 0, add_or_subtract = 0;
	double initial_bank_balance = 0.0, current_bank_balance = 0.0, balance = 0.0, wager = 0.0;

	system("mode con cols=80 lines=40");//define window size.
	system("color 5f");//change window color.

	do{//do while loop start.
		printf("\n\n    .  .  .  .  .      .  .  .  .  .\
			      \n   .           ..     .           ..\
			      \n  .     6     . .    .     5     . .\
				  \n .           .  .   .           .  .\
				  \n.  .  .  .  .   .  .  .  .  .  .   .\
				  \n.           . 1 .  .           . 3 .\
				  \n.     2     .  .   .     4     .  .\
				  \n.           . .    .           . .\
				  \n.  .  .  .  .      .  .  .  .  .\n\n\
\n\nAuthor: Nathan Scott\n\
Verson: 4.11\n\n\
-------------------------------------MENU-------------------------------------\n\
1. Rules of the Game of \"Craps\"\n\
2. Start Game\n\
0. Exit\n\
------------------------------------------------------------------------------\n\n\
Enter: ");

		fflush(stdin);//clear scanf buffer.
		if (scanf("%d", &menu) != 1 || menu > 2){//determine user enter is valid or not.
			system("cls");
			printf("\n***************************************\n\
Error: please enter a valid choice!\n\
***************************************\n");
		}
		else//if user input is valid then enter else clause.
		{
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//1. Prints out the rules of the game of "craps".
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (menu == 1){
				print_game_rules();
			}
			
			if (menu == 2){
				do{
					if (result == 0 || balance <= 0){
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//2. Prompts the player for an initial bank balance from which wagering will be added or subtracted.
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						balance += get_bank_balance();
						initial_bank_balance = balance;
					}
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//3. Prompts the player for a wager on a particular roll.
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						wager = get_wager_amount();

						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//4. Checks to see if the wager is within the limits of the player's available balance.
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						result = check_wager_amount(wager, balance);
					

					if (result != 0){
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//5. Rolls one die.//2, 3, or 12 on first throw//RESTART random seed?
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						srand((unsigned)time(0));//generate seed for rand().
						die1_value = roll_die();
						die2_value = roll_die();
						printf("First roll values are: %d, %d\n",die1_value, die2_value);
						system("pause");

						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//6. Sums together the values of the two dice and returns the result.
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						sum_dice = calculate_sum_dice(die1_value, die2_value);
						point_value = sum_dice;
						
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//7. Determines the result of the first dice roll.
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						winLossPoint = is_win_loss_or_point(point_value);
						if (winLossPoint == 1){
							add_or_subtract = winLossPoint;
							balance = adjust_bank_balance(balance, wager, add_or_subtract);
							printf("You won on the first roll, the new balance is: %.2lf\n", balance);
							system("pause");
						}
						
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//8. Determines the result of any successive roll after the first roll.
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						while (winLossPoint == -1){
							die1_value = roll_die();
							die2_value = roll_die();
							printf("Second roll values are %d, %d\n", die1_value, die2_value);
							system("pause");

							point_value2 = die1_value + die2_value;

							if (point_value == point_value2){
								balance = adjust_bank_balance(balance, wager, winLossPoint);
								winLossPoint = 1;
							}
							else if (is_win_loss_or_point(point_value2) == 0){
								winLossPoint = is_win_loss_or_point(point_value2);
								printf("Second roll winLossPoint is: %d\n", winLossPoint);
								balance = adjust_bank_balance(balance, wager, winLossPoint);
								printf("The new balance is: %.2lf\n", balance);
								system("pause");
							}
							else if(is_win_loss_or_point(point_value2) == -1){
								winLossPoint = is_win_loss_or_point(point_value2);
								printf("Second roll winLossPoint is: %d\n", winLossPoint);
								system("pause");
							}

							pointLossNeither = is_point_loss_or_neither(point_value, point_value2);
							if (pointLossNeither == -1){
								add_or_subtract = 1;
							}
							else if (pointLossNeither == 0){
								add_or_subtract = 0;
							}
							else if (pointLossNeither == -1){
								add_or_subtract = -1;
							}
						} 

						printf("Exited while loop with value: %d\n", winLossPoint); 
						system("pause");
						
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//9. If add_or_subtract is 1, then the wager amount is added to the bank_balance.
						//This function is not necessary for my program, but the same information is accounted for.
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						/*pointLossNeither = is_point_loss_or_neither(point_value, point_value2);
							if (pointLossNeither == -1){
								add_or_subtract = 1;
							}
							else if (pointLossNeither == 0){
								add_or_subtract = 0;
							}
							else if (pointLossNeither == -1){
								add_or_subtract = -1;
							}*/

						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						//10. Prints an appropriate message dependent on the number of rolls taken so far by the player, the current balance,
						//and whether or not the player just won his roll.
						//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
							chatter_messages(numRolls, point_value, initial_bank_balance, balance);
							counter++;
						if (balance <= 0){
							printf("\nGame Over!\n");
						}
					}
				} while (balance > 0);
			}
		}
	} while (menu != 0);//end do while loop when menu == 0.
	system("cls");
	printf("\n\nGood Bye!\n\n");
	system("pause");
	return 0;
}

func.h

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015, Section 01
//Programming Assignment: pa4
//Due: Oct. 7, 2015
//File Type: header file.
//Purpose: include lib and pre-define functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#ifndef _HEADERNAME_H 
#define _HEADERNAME_H
#define _CRT_SECURE_NO_WARNINGS

void print_game_rules(void);
double get_bank_balance(void);
double get_wager_amount(void);
int check_wager_amount(double wager, double balance);
int roll_die(void);
int calculate_sum_dice(int die1_value, int die2_value);
int is_win_loss_or_point(int sum_dice);
int is_point_loss_or_neither(int sum_dice, int point_value);
double adjust_bank_balance(double bank_balance, double wager_amount, int add_or_subtract);
void chatter_messages(int number_rolls, int win_loss_neither, double initial_bank_balance, double current_bank_balance);

#endif

func.c

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015, Section 01
//Programming Assignment: pa4
//Due: Oct. 7, 2015
//File Type: source file.
//Purpose: define functions and logics.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "header.h"//include the header file.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//1. Prints out the rules of the game of "craps".
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void print_game_rules(void){
	system("cls");
	printf("The Rules of the Game of \"Craps\"\n\n\
A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. \n\
After the dice have come to rest, the sum of the spots on the two upward faces is calculated. \n\
If the sum is 7 or 11 on the first throw, the player wins. \n\
If the sum is 2, 3, or 12 on the first throw (called \"craps\"), the player loses (i.e. the \"house\" wins). \n\
If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's \"point.\". \n\
To win, you must continue rolling the dice until you \"make your point.\". \n\
The player loses by rolling a 7 before making the point.\n");
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//2. Prompts the player for an initial bank balance from which wagering will be added or subtracted.
//The player entered bank balance(in dollars, i.e.$100.00) is returned.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double get_bank_balance(void){
	system("cls");
	printf("\n2. Add Money\n");
	printf("Please add money to your bank:\n");
	double money = 0.0;
	
		if (scanf("%lf", &money) == 1){
			printf("\nCurrent Balance: %.2lf\n", money);
			
		}else{
			printf("\nError: please enter a valid value!\n\n");
			fflush(stdin);
		}
		
	printf("\n%.2lf dollars has been added to your bank!\n", money);
	system("pause");
	return money;
	
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//3. Prompts the player for a wager on a particular roll.The wager is returned.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double get_wager_amount(void){
	system("cls");
	printf("\n3. Place A Wager\n");
	printf("Please add a wager on a particular roll:\n");
	double money = 0.0;

		if (scanf("%lf", &money) == 1){
			printf("\nCurrent Wager: %.2lf\n", money);
		}
		else{
			printf("\nError: please enter a valid value!\n\n");
			fflush(stdin);
		}

	printf("\n%.2lf dollars has been reserved from your bank!\n", money);
	system("pause");
	return money;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//4. Checks to see if the wager is within the limits of the player's available balance.
//If the wager exceeds the player's allowable balance, then 0 is returned; otherwise 1 is returned.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int check_wager_amount(double wager, double balance){
	if (wager > balance){
		printf("\nPlease add more money to your bank before you roll!\n");
		system("pause");
		return 0;
	}
	else{
		return 1;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//5. Rolls one die.This function should randomly generate a value between 1 and 6, inclusively.Returns the value of the die.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int roll_die(void){
	system("cls");
	int roll = 0, dice = 0, number = 0;
	//srand((unsigned)time(0));//generate seed for rand().
	number = rand() % 6 + 1;
	//printf("roll value: %d",number);
	/*printf("\n\n    .  .  .  .  .\
			  \n   .           ..\
			  \n  .     %d     . .\
			  \n .           .  .\
			  \n.  .  .  .  .   .\
			  \n.           .   .\
			  \n.           .  .\
			  \n.           . .\
			  \n.  .  .  .  .\n\n", number);*/
	//system("pause");
	return number;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//6. Sums together the values of the two dice and returns the result.
//Note: this result may become the player's point in future rolls.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int calculate_sum_dice(int die1_value, int die2_value){
	int result = 0;
	result = die1_value + die2_value;
	return result;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//7. Determines the result of the first dice roll.If the sum is 7 or 11 on the roll, the player wins and 1 is returned.
//If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses(i.e.the "house" wins) and 0 is returned.
//If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's "point" and -1 is returned.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int is_win_loss_or_point(int sum_dice){
	if (sum_dice == 7 || sum_dice == 11){
		return 1;
	}
	else if (sum_dice == 2 || sum_dice == 3 || sum_dice == 12){
		return 0;
	}
	else if (sum_dice == 4 || sum_dice == 5 || sum_dice == 6 || sum_dice == 7 || sum_dice == 8 || sum_dice == 9 || sum_dice == 10){
		return -1;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//8. Determines the result of any successive roll after the first roll.
//If the sum of the roll is the point_value, then 1 is returned.
//If the sum of the roll is a 7, then 0 is returned.
//Otherwise, -1 is returned.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int is_point_loss_or_neither(int sum_dice, int point_value){
	if (sum_dice == point_value){
		return 1;
	}
	else if (sum_dice == 7){
		return 0;
	}
	else{
		return -1;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//9. If add_or_subtract is 1, then the wager amount is added to the bank_balance.
//If add_or_subtract is 0, then the wager amount is subtracted from the bank_balance.
//Otherwise, the bank_balance remains the same.
//The bank_balance result is returned.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double adjust_bank_balance(double bank_balance, double wager_amount, int add_or_subtract){
	if (add_or_subtract == 1){
		bank_balance += wager_amount;
		return bank_balance;
	}
	else if (add_or_subtract == 0){
		bank_balance -= wager_amount;
		return bank_balance;
	}
	else if (add_or_subtract == -1) {
		return bank_balance;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//10. Prints an appropriate message dependent on the number of rolls taken so far by the player, the current balance, and whether or not the player just won his roll.
//The parameter win_loss_neither indicates the result of the previous roll.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void chatter_messages(int number_rolls, int win_loss_neither, double initial_bank_balance, double current_bank_balance){
	system("cls");
	char previous = NULL;
	if (win_loss_neither == 1){ 
		printf("\nGreat!\nYou Won!\n");
	}
	else if (win_loss_neither == 0){
		printf("\nCraps!\nYou Loses!\n");
	}
	else if (win_loss_neither == -1){
		printf("\nGood!\nYou got points!\n");
	}
	printf("\nRoll %d, Previous Roll: %d, Initial Bank: %.2lf, Current Bank: %.2lf\n", number_rolls + 1, win_loss_neither, initial_bank_balance, current_bank_balance);
	system("pause");
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//THE END
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////