-
help with a craps game!
Ok, first i'll give the assignment and then i'll say my ideas..but please help me just start the program because as usual, i dont know where to begin:
-You are to crate a simple craps game using functions exclusively (very little will be used in main)
~the user will start with 200 dollars
~prompt the user to bet an amount of their remaining cash
~wins or losses will be added or subtracted from the user's cash reserve
~ the use will then be prompted if they wish to ontinue playing, unless they have no money left in which case the game will end.
-------
okay, so i'm not allowed to use an global variables and as it says, i need to use all functions. so first, in main i though that i'd ask the person's name and set the TotalCash to 200. but then i don't understand how to "send" that information to each of the following functions that i'll be making.
and then after main, i want to make a menu function to explain to the user the rules of the game along with the option to begin playing. and then it'd go to the 'first bet' function and ask for a bet ect, and check to see if they have enough money.
my main question is how do i send things to different functions???
thank you,
jessie
-
You need to declare local variables inside your main and pass them as parameters to your function. You can pass pointers to the functions or just grab the return values.
For example:
Code:
int GetBet()
{
int x;
printf("Enter a bet:\n");
scanf("%d", x);
return x;
}
void GetBet2(int *x)
{
printf("Enter a bet:\n");
scanf("%d", x);
}
void PrintBet(int a, int b)
{
printf("Bet1 = %d\nBet2 = %d\n", a, b);
}
int main()
{
int bet, bet2;
// like this
bet = GetBet();
// or like this
GetBet2(&bet2);
// pass info to function
PrintBet(bet, bet2);
return 0;
}
-
You may want to make a structure that holds vital information and pass referrences of that structure to the functions:
Code:
struct player {
char name[8]; //a short name
int money; //the amount of money
char bet[50];
};
//the bet member is a boolean type.
//1 means a bet has been placed in a certain
//position, 0 means it has not.
//you decide which index represents
//which place on the board
void PlaceBet(struct player *);
int main(void) {
struct player me;
//ask for bets
...
PlaceBet(&me);
...
//do other stuff here
return 0;
}
-
Do you think the instructor would class a structure as global variables?
-
Not if you don't use a global structure you are good and your professor will be happy. For example:
Code:
struct some_struct {
int x, y;
};
struct some_struct unhappy_teacher;
int main(void) {
//code goes here
return 0;
}
But this does not use any globals:
Code:
struct some_struct {
int x, y;
};
int main(void) {
struct some_struct teacher_is_happy;
//code here
return 0;
}
So go you aren't using a loop-hole you are using your brain.
-
This might sound like a silly question, but what is craps??
-
Its not a silly question. It is a vegas game. It is a game that is bases on betting on probabilities of rolls of two dice. They probably have it on yahoo games or something. I'm sure you can find somewhere to play it online for fun.
-
-
thanks a lot for replying, everyone. but i dont even know what a structure is....
um here is what i have so far. its obviously very very wrong because it doesn't even run...
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
char *name ()
{
static char name[20];
printf("Please enter your name:");
scanf("%s", name);
return name;
}
int main ()
{
int TotalCash=200;
return TotalCash;
/*What else do i put in main??*/
}
void FirstBet ()
{
int Dice1=rand()%6+1;
int Dice2=rand()%6+1;
int Sum=Dice1+Dice2;
int bet;
printf("Enter a bet:\n");
scanf("%d", &bet);
if (bet<=200) /*do nested if statements work here??*/
if (bet>0)
printf("You rolled a %d and a %d. The total is %d", Dice1, Dice2, Sum);
else
printf("Enter a new bet.");
}
[edit]Nice try with the code tags, but its a forward slash, not a backslash. Hammer.
-
I wrote a c++ craps game a couple of weeks ago. It was going to become part of a tutorial on enums.
its in c++ and is incomplete(just the play function shown) but it shows how you could write code to play craps.
Code:
void PlayGame()
{
InitRandGen();
enum Status { CONTINUE, WON, LOST };
Status GameStatus; // controls individual game
bool quit = false; // controls game loop
HSEntry HighScores[5]; // high scores table
memset(HighScores,0,5*sizeof(HSEntry)); // zero hs table
ReadHS(HighScores);
maximise();
cursoroff();
PrintHS(HighScores);
WelcomeScreen();
long Savings = 10; // This is the players cash. $10 to start.
int Die1 = 0, Die2 = 0; // The dice
int Sum = 0; // total of dice
int Stake = 0; // The bet.
int Point = 0; // The point to make if needed
int HouseWins = 0, PlayerWins = 0;
while (!quit)
{
StatScreen(Die1, Die2, Sum, Stake, Savings, Point, HouseWins, PlayerWins);
if (Savings == 0) // out of money
{
gotoxy(0,8);
colour(BBLUE | TLTYELLOW);
cout << "YOU ARE OUT OF MONEY !!!!! LOSER!!!!! Loser press any key! " << flush;
_getch(); //*****msvc specific call*****
PrintHS(HighScores);
return;
}
Stake = GetWager(Savings); // savings altered here to account for stake
Sum = RollDice(Die1, Die2); // die1,die2 by ref.
switch(Sum)
{
case 7:
case 11: GameStatus = WON;
PlayerWins ++;
Savings += Stake * 2; // Pay up for win
Stake=0; // reset stake ready for statscreen
StatScreen(Die1, Die2, Sum, Stake, Savings, Point, HouseWins, PlayerWins);
PWin();
break;
case 2:
case 3:
case 12: GameStatus = LOST;
HouseWins ++;
Stake=0; // reset for statscreen
StatScreen(Die1, Die2, Sum, Stake, Savings, Point, HouseWins, PlayerWins);
HWin();
break;
default: GameStatus = CONTINUE;
Point = Sum;
StatScreen(Die1, Die2, Sum, Stake, Savings, Point, HouseWins, PlayerWins);
ReThrow();
break;
}
while ( GameStatus == CONTINUE )
{
StatScreen(Die1, Die2, Sum, Stake, Savings, Point, HouseWins, PlayerWins);
Sum = RollDice(Die1, Die2);
if (Sum == Point)
{
GameStatus = WON;
PlayerWins ++;
Savings += Stake * 2;
Stake=0;
Point = 0;
StatScreen(Die1, Die2, Sum, Stake, Savings, Point, HouseWins, PlayerWins);
PWin();
}
else
if (Sum == 7)
{
GameStatus = LOST;
HouseWins ++;
Stake=0;
StatScreen(Die1, Die2, Sum, Stake, Savings, Point, HouseWins, PlayerWins);
Point = 0;
HWin();
}
else
{
StatScreen(Die1, Die2, Sum, Stake, Savings, Point, HouseWins, PlayerWins);
ReThrow();
}
}
if(Savings) quit=AskForContinue();
if(quit == true)
{
if(Savings > HighScores[0].score)
{
clrscr();
const int SIZE = 256;
char buffer[SIZE];
gotoxy(0,8);
colour(BBLUE | TLTYELLOW);
cout << "You have made it onto the high scores board. Congratulations!!!!" << endl;
cout << "Please enter your name :- ";
cin.getline(buffer,SIZE);
HighScores[0].score = Savings;
strncpy(HighScores[0].name,buffer,20);
BubbleSort(HighScores);
WriteHS(HighScores);
PrintHS(HighScores);
}
}
}
}
-
that is very hard to follow for someone with no c++ experience..:confused: