-
craps game & dice game..
Hey guys I'm pretty much stuck on how to actually do the functions for the craps and dice game..here's the criteria and what i have...
craps criteria & dice game criteria..
Welcome to the Casino. Here are your choices:
1) Buy chips
2) Sell chips
3) Play Craps
4) Play Arup's Game of Dice
5) Status Report
6) Quit
3
How many chips would you like to bet?
0
Sorry, that is not allowed. No game played.
Welcome to the Casino. Here are your choices:
1) Buy chips
2) Sell chips
3) Play Craps
4) Play Arup's Game of Dice
5) Status Report
6) Quit
3
How many chips would you like to bet?
10
Press 'r' and hit enter for your first roll.
r
You rolled 8.
Press 'r' and hit enter for your next roll.
r
You rolled 12.
Press 'r' and hit enter for your next roll.
r
You rolled 6.
Press 'r' and hit enter for your next roll.
r
You rolled 7.
Sorry, you have lost.
Welcome to the Casino. Here are your choices:
1) Buy chips
2) Sell chips
3) Play Craps
4) Play Arup's Game of Dice
5) Status Report
6) Quit
4
How many chips would you like to bet?
17
Press 'r' and hit enter for your first roll.
r
You rolled 3.
Press 'r' and hit enter for your next roll.
r
You rolled 8.
You win!
Heres what i have..
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CHIP_VALUE 11
#define SELL_VALUE 10
//Intialize needed functions
int pairofdice();
int craps();
int arupsdice();
int buychips();
int sellchips();
void statusreport();
void menu();
Code:
int main(void) {
int ans;
int cash = 1000;
int chips = 0;
do {
//Print menu and gather user response
menu();
scanf("%d", &ans);
//Invoke proper function per user response
if (ans == 1)
chips = buychips(&cash);
else if (ans == 2)
sellchips();
else if (ans == 3)
craps ();
else if (ans == 4)
arupsdice();
else if (ans == 5)
statusreport();
//Sort out invalid inputs from user
else if (ans != 6)
printf("Invalid menu choice, please select again.\n");
} while(ans != 6);
printf("Thanks for visiting!!\n");
return 0;
}
// Pre-conditions: None.
// Post-conditions: Main menu will be printed and a valid user answer will be returned.
void menu() {
printf("Welcome to the Casino. Here are your choices:\n");
printf("1) Buy chips\n");
printf("2) Sell chips\n");
printf("3) Play Craps\n");
printf("4) Play Arup's Game of Dice\n");
printf("5) Status Report\n");
printf("6) Quit\n");
}
// Pre-conditions: None.
// Post-conditions: Generates two random numbers in between
// one and six inclusive and returns their sum.
int pairofdice() {
int dice1, dice2, roll;
srand(time(0));
//Random number 1 through six for each die
dice1 = 1 + rand()%6;
dice2 = 1 + rand()%6;
//Combine for roll total
roll = dice1 + dice2;
return roll;
}
// Pre-condition: None.
// Post-condition: Plays one game of Craps and returns
// PLAYER_WON if the player won and
// PLAYER_LOST if they lost.
int craps() {
printf("This is where you insert your code for the craps game...\n");
}//end craps
// Pre-condtion: None.
// Post-condition: Plays one game of Arup's game of dice and
// returns PLAYER_WON if the player won and
// returns PLAYER_LOST if thet lost.
int arupsdice() {
printf("This is where you insert your code for the arupsdice game...\n");
}//end arupsdice
// Pre-condition: cash is the address of the variable
// storing the amount of money the user
// wants to spend on chips.
// Post-condition: The number of chips purchased is returned
// and the variable storing the amount of
// money the user paid for chips is adjusted
// to equal the change left over after the
// transaction.
int buychips(int*cash_ptr) {
int Cash_4_Chips;
int *cash_ptr;
printf("How much cash do you want to spend for chips?\n");
scanf("%d", &Cash_4_Chips);
if(Cash_4_Chips>*cash_ptr);
printf(" Sorry, you do not have that much money. No chips bought.\n");
}//End function to buy chips
// Pre-conditions: numchips > 0.
// Post-conditions: Returns the cash obtained for selling
// numchips number of chips.
int sellchips() {
printf("This is where you insert your code for selling chips...\n");
}//End function to sell chips
// Pre-condition: The first parameter is the number of
// chips the use has, the second is how
// much cash they currently have.
// Post-condition: A report detailing the number of chips
// and the amount of cash the user has is
// printed.
void statusreport() {
printf("This is where you insert your code for the status report...\n");
}//Emd status report
return 0;
}
I'm confused on how to do it because I looked at other examples on the site for craps.. Would appreciate it a lot for help
-
You're only supposed to call srand() once during the execution of a program. The best place to put it is in main().
You have some stray code at the end:
Just get rid of it.
Code:
int buychips(int*cash_ptr) {
int Cash_4_Chips;
int *cash_ptr;
You will most likely get a warning here. Something to the effect of "shadowing a parameter". Get rid of the code in red.
Code:
int buychips(int*cash_ptr) {
int Cash_4_Chips;
int *cash_ptr;
printf("How much cash do you want to spend for chips?\n");
scanf("%d", &Cash_4_Chips);
if(Cash_4_Chips>*cash_ptr);
printf(" Sorry, you do not have that much money. No chips bought.\n");
}//End function to buy chips
Because of the extra semicolon after your if, the printf() will always execute.
You never return a value from this function.
As for craps() . . . you'll need a while loop.
-
revised
ok here's my revised version...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CHIP_VALUE 11
#define SELL_VALUE 10
//Intialize needed functions
int pairofdice();
int craps();
int arupsdice();
int buychips();
int sellchips();
void statusreport();
void menu();
int main(void) {
Code:
int ans;
int cash = 1000;
int chips = 0;
srand(time(0))
do {
//Print menu and gather user response
menu();
scanf("%d", &ans);
//Invoke proper function per user response
if (ans == 1)
chips = buychips(&cash)
else if (ans == 2)
sellchips();
else if (ans == 3)
craps ();
else if (ans == 4)
arupsdice();
else if (ans == 5)
statusreport();
//Sort out invalid inputs from user
else if (ans != 6)
printf("Invalid menu choice, please select again.\n");
} while(ans != 6);
printf("Thanks for visiting!!\n");
return 0;
}
// Pre-conditions: None.
// Post-conditions: Main menu will be printed and a valid user answer
// will be returned.
void menu() {
printf("Welcome to the Casino. Here are your choices:\n");
printf("1) Buy chips\n");
printf("2) Sell chips\n");
printf("3) Play Craps\n");
printf("4) Play Arup's Game of Dice\n");
printf("5) Status Report\n");
printf("6) Quit\n");
}
// Pre-conditions: None.
// Post-conditions: Generates two random numbers in between
// one and six inclusive and returns their sum.
int pairofdice() {
int dice1, dice2, roll;
//Random number 1 through six for each die
dice1 = 1 + rand()%6;
dice2 = 1 + rand()%6;
//Combine for roll total
roll = dice1 + dice2;
return roll;
}
// Pre-condition: None.
// Post-condition: Plays one game of Craps and returns
// PLAYER_WON if the player won and
// PLAYER_LOST if they lost.
int craps() {
printf("This is where you insert your code for the craps game...\n");
}//end craps
// Pre-condtion: None.
// Post-condition: Plays one game of Arup's game of dice and
// returns PLAYER_WON if the player won and
// returns PLAYER_LOST if thet lost.
int arupsdice() {
printf("This is where you insert your code for the arupsdice game...\n");
}//end arupsdice
// Pre-condition: cash is the address of the variable
// storing the amount of money the user
// wants to spend on chips.
// Post-condition: The number of chips purchased is returned
// and the variable storing the amount of
// money the user paid for chips is adjusted
// to equal the change left over after the
// transaction.
int buychips(int*cash_ptr) {
int Cash_4_Chips;
printf("How much cash do you want to spend for chips?\n");
scanf("%d", &Cash_4_Chips);
if(Cash_4_Chips>*cash_ptr);
printf(" Sorry, you do not have that much money. No chips bought.\n");
}//End function to buy chips
// Pre-conditions: numchips > 0.
// Post-conditions: Returns the cash obtained for selling
// numchips number of chips.
int sellchips() {
printf("This is where you insert your code for selling chips...\n");
}//End function to sell chips
// Pre-condition: The first parameter is the number of
// chips the use has, the second is how
// much cash they currently have.
// Post-condition: A report detailing the number of chips
// and the amount of cash the user has is
// printed.
void statusreport() {
printf("This is where you insert your code for the status report...\n");
}//End status report
-
well i have everything for the sell of chips.. but the status report is getting really difficult.Idon't know whether i need a equation or a for loop after the void statusreport...
Code:
// numchips number of chips.
int sellchips(int *numchips_ptr) {
int chipsB;
int chips_sold;
int Chips_4_Sale;
printf("How many chips do you want to sell?\n");
scanf("%d", &Chips_4_Sale);
if(Chips_4_Sale>*numchips_ptr)
{
printf("Sorry, you do not have that many chips. No chips sold.\n");
}
else
{
chipsB=Chips_4_Sale/SELL_VALUE;
chips_sold=chipsB*SELL_VALUE;
*numchips_ptr=*numchips_ptr-chips_sold;
}
return chipsB;
}//End function to sell chips
// Pre-condition: The first parameter is the number of
// chips the use has, the second is how
// much cash they currently have.
// Post-condition: A report detailing the number of chips
// and the amount of cash the user has is
// printed.
void statusreport() {
int Cash_4_Chips
printf("\n");
}//End status report