Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MIN_BET 5
#define MAX_BET 50
#define START_AMOUNT 200
bool getInput(int *pinBet, int *puserBet);
int randDice(int *pdice1, int *pdice2,int *pcomputerWon,int *pcomputerLost);
void calcMoney(int *puserMoney, int *pinBet);
void printOutput(int dice1, int dice2, int inBet, int userMoney);
int main(void)
{
int inBet(0);
int userBet(0);
double userMoney(0);
int pdice1, pdice2, pcomputerWon, pcomputerLost,
puserMoney, pinBet, betRoll, dice1, dice2;
while(getInput(&inBet, &userBet)){
randDice(&pdice1, &pdice2, &pcomputerWon, &pcomputerLost);
calcMoney(&puserMoney, &pinBet);
printOutput(dice1, dice2, inBet, userMoney);
}
system("pause");
return 0;
}
bool getInput(int *pinBet, int *pbetRoll)
{
double userMoney;
if(*pinBet!=0)
return false;
else{
return true;
}
if(userMoney!=0)
return false;
else{
return true;
}
printf("Enter a bet between 5 and 50, inclusive(0 to quit): ");
scanf("%d", &pinBet);
while(*pinBet<MIN_BET||*pinBet>MAX_BET||*pinBet>userMoney){
printf("Bet is invalid\n");
printf("Enter a bet between 5 and 50, inclusive(0 to quit): "),
scanf("%d", &pinBet);
}
printf("Are you betting that the computer will roll 2 6's within 25 rolls(0 for no): ");
scanf("%d", &pbetRoll);
system ("pause");
}
int randDice(int *pdice1, int *pdice2,int *pcomputerWon,int *pcomputerLost)
{
int rollNum;
for (*pdice1=1;*pdice1<=25;++*pdice1){
*pdice1=(rand()%6)+1;
}
for (*pdice2=1;*pdice1<=25;++*pdice2){
*pdice2=(rand()%6)+1;
}
for(rollNum=1; rollNum<=25||(*pdice1==6&&*pdice2==6);++rollNum){
printf("Roll #%d, rolled %d and %d", rollNum, pdice1, pdice2);
}
if(*pdice1==6&&*pdice2==6){
printf("The computer rolled double skull in %d rolls", rollNum);
return *pcomputerWon;
}
else{
printf("The computer did NOT rolled double skull in 25 rolls");
return *pcomputerLost;
}
}
void calcMoney(int *puserMoney, int *pinBet)
{
int betRoll;
int dice1;
int dice2;
if(betRoll!=0&&(dice1!=6&&dice2!=6))
*puserMoney=*puserMoney+*pinBet;
else{
*puserMoney=*puserMoney-*pinBet;
}
}
void printOutput(int dice1, int dice2, int inBet, int userMoney)
{
if(dice1==6&&dice2==6)
printf("you won %d", inBet);
else{
printf("you lost %d", inBet);
}
printf("your total money is now %d", userMoney);
}