Login Register






Craps Game filter_list
Author
Message
Craps Game #1
A cli craps game
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 diceRoll
If you need help feel free to PM me
[Image: klfpJD]
Probitcoin
Freebitcoin
BTC clicks
bitcoin wallet:
1FBPAanbs3rJU9BUpobpDJc9hHUaCaC25N

Reply

RE: Craps Game #2
Very NICE game ! I have played for 10 minutes being focused like stupid , lol :>

[Image: 3mj5iHO.png]
If you need help , drop me a PM and I will help you with the best I can !

[Image: V7mYdF6.png]



Reply

RE: Craps Game #3
Very NICE game ! I have played for 10 minutes being focused like stupid , lol :>

[Image: 3mj5iHO.png]
If you need help , drop me a PM and I will help you with the best I can !

[Image: V7mYdF6.png]



Reply

RE: Craps Game #4
(11-03-2013, 12:25 AM)dR.0xYw0Rm Wrote: Very NICE game ! I have played for 10 minutes being focused like stupid , lol :>

[Image: 3mj5iHO.png]


Glad you liked it thanks for the feedback!
If you need help feel free to PM me
[Image: klfpJD]
Probitcoin
Freebitcoin
BTC clicks
bitcoin wallet:
1FBPAanbs3rJU9BUpobpDJc9hHUaCaC25N

Reply

RE: Craps Game #5
Fixed an issue @ArkPhaze spotted where it would only check if the bet exceeded the balance once and would allow you to bet 0, also removed some unnecessary comments.



Added a description to the beginning of the game and some dialog from the dealer
If you need help feel free to PM me
[Image: klfpJD]
Probitcoin
Freebitcoin
BTC clicks
bitcoin wallet:
1FBPAanbs3rJU9BUpobpDJc9hHUaCaC25N

Reply

RE: Craps Game #6
I'm just looking at it over again, but it looks much more organized at first glance, I'll see if I can find anything else to help you out. Good job though. Smile

edit: You've still got a few more:
Code:
// End if // End fuction main // End function diceRoll

But other than that, this is already looking much better. I've read through it, the only thing I'd suggest at this point is maybe break the thing up into some functions to help make things easier to maintain. However, this is pretty good as far as I can tell.. Smile
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Craps Game #7
(11-03-2013, 07:47 PM)ArkPhaze Wrote: I'm just looking at it over again, but it looks much more organized at first glance, I'll see if I can find anything else to help you out. Good job though. Smile

Thanks for the feedback I'm always looking to improve so if you find anything else let me know!
If you need help feel free to PM me
[Image: klfpJD]
Probitcoin
Freebitcoin
BTC clicks
bitcoin wallet:
1FBPAanbs3rJU9BUpobpDJc9hHUaCaC25N

Reply

RE: Craps Game #8
What are you still forgetting about the 'int' datatype? Wink
Code:
int bet;

Code:
while (bet > bank || bet == 0) {}
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Craps Game #9
I guess I'll post this here since you didn't get what I was hinting at. I just wanted to see if you could find it on your own. Smile

Code:
int bank = 1000; int bet; scanf("%d", &bet); while (bet > bank || bet == 0) {} std::cout << "Your bet: " << bet << std::endl;

Result:
Code:
-234 Your bet: -234

This is critical here, and something most often forgotten about. 'int' is a signed datatype; it extends into integer values below 0 as well.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Craps Game #10
(11-04-2013, 12:30 AM)ArkPhaze Wrote: I guess I'll post this here since you didn't get what I was hinting at. I just wanted to see if you could find it on your own. Smile

Code:
int bank = 1000; int bet; scanf("%d", &bet); while (bet > bank || bet == 0) {} std::cout << "Your bet: " << bet << std::endl;

Result:
Code:
-234 Your bet: -234

This is critical here, and something most often forgotten about. 'int' is a signed datatype; it extends into integer values below 0 as well.

Your completely right and it's something I overlooked, fixed now. Thanks again for your input I appreciate it, while we are on the subject the program doesn't really parse things efficiently anyway and entering anything but an integer breaks it so I'm going to play around with the code tomorrow and try to fix this.
If you need help feel free to PM me
[Image: klfpJD]
Probitcoin
Freebitcoin
BTC clicks
bitcoin wallet:
1FBPAanbs3rJU9BUpobpDJc9hHUaCaC25N

Reply