Craps Game 11-02-2013, 06:15 PM
#1
A cli craps game
Rules:
Rules:
Spoiler:
- You start with a balance of $1000 you bet on the outcome of the rolls
- Two die are rolled and the numbers are summed up
- If player rolls 7 or 11 on the first roll player wins
- If player rolls 2, 3 or 12 on the first roll player looses
- If player rolls any other number the total become your points
- To win after the first roll you must roll a total equal to the number of points you have
- After the first roll 7 is game over
- Game ends when player runs out of money
Code:
/* Craps game: craps.c */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Enumeration constants for game status
enum Status { CONTINUE, WIN, LOOSE } ;
int diceRoll(void); // Prototype for function
// Function main begins program execution
int main(void)
{
char buff[10];
int sum;
int points;
int bank = 1000;
int bet;
enum Status gameStatus; // CONTINUE, WIN, LOOSE
// Random number generator
srand(time(NULL));
printf("Craps is a casino game whereby the player bets on the outcome of dice rolls.\n");
printf("There are two ways to win the game, rolling a 7 or 11 on the first roll, or\n");
printf("if you don't get either of these the total value of the dice becomes your\n");
printf("score and the aim then is to match this on a subsequent roll.\n\n");
printf("Beware however as there are also ways to loose the game.\n");
printf("If you are unlucky enough to roll a 2, 3 or 12 on your first roll or roll\n");
printf("a 7 thereafter you will loose and the house will take your stake.\n\nThe game will end when you run out of money.\n\n");
while (bank > 0) {
printf("Balance: $%d\nDealer: How much do you want to bet\nYou: $", bank);
if (fgets(buff, sizeof(buff), stdin) !=NULL) {
bet = atoi(buff);
}
while (bet > bank || bet <= 0) {
if (bet > bank) {
printf("Dealer: Your credit cards no good here! how much do you want to bet\nYou: $");
}
if (bet <= 0) {
printf("Dealer: It's only pretend money you know, how about a real bet?\nYou: $");
}
if (fgets(buff, sizeof(buff), stdin) !=NULL) {
bet = atoi(buff);
}
}
sum = diceRoll(); // First roll
// Determine game status
switch(sum) {
// Win on first roll
case 7:
printf("Seven out\n");
gameStatus = WIN;
break;
case 11:
printf("Yo-leven\n");
gameStatus = WIN;
break;
// Loose on first roll
case 2:
printf("Snake eyes\n");
gameStatus = LOOSE;
break;
case 3:
printf("Ace Deuce\n");
gameStatus = LOOSE;
break;
case 12:
printf("Boxcars\n");
gameStatus = LOOSE;
break;
// Remember points
default:
gameStatus = CONTINUE;
points = sum;
printf("Points are %d\n", points);
break;
}
// While game is not complete
while (gameStatus == CONTINUE) {
sum = diceRoll(); // Roll again
// Determine game status
if (sum == points) { // Win by making point
gameStatus = WIN; // Game over player wins
}
else {
if (sum == 7) { // Loose by rolling a 7
gameStatus = LOOSE; // Game over player losses
}
}
}
// Display won or lost message
if (gameStatus == WIN) {
printf("Dealer: You win $%d, congratulations\n", bet);
bank += bet;
} // End if
else { // Player lost
printf("Dealer: Sorry you loose $%d, better luck next time!\n", bet);
bank -= bet;
}
}
printf("Dealer: Oh you ran out of money, looks like the house wins again!\n");
return 0; // Indicate successful terminiation
} // End fuction main
// roll the dice and calculate the sum, displaying the results
int diceRoll(void)
{
int die1;
int die2;
int workSum;
// Randomize value of dice 1-6
die1 = 1 + (rand() % 6);
die2 = 1 + (rand() % 6);
workSum = die1 + die2;
// Display the results
printf("Player rolled %d + %d = %d\n", die1, die2, workSum);
return workSum;
} // End function diceRollIf you need help feel free to PM me
![[Image: klfpJD]](http://goo.gl/klfpJD)
Probitcoin
Freebitcoin
BTC clicks
bitcoin wallet:
1FBPAanbs3rJU9BUpobpDJc9hHUaCaC25N
Probitcoin
Freebitcoin
BTC clicks
bitcoin wallet:
1FBPAanbs3rJU9BUpobpDJc9hHUaCaC25N

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)


![[Image: V7mYdF6.png]](http://i.imgur.com/V7mYdF6.png)

