![]() |
|
Craps Game - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: Craps Game (/Thread-Craps-Game) |
Craps Game - chmod - 11-02-2013 A cli craps game Rules: Spoiler:
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 diceRollRE: Craps Game - dR.0xYw0Rm - 11-03-2013 Very NICE game ! I have played for 10 minutes being focused like stupid , lol :>
RE: Craps Game - dR.0xYw0Rm - 11-03-2013 Very NICE game ! I have played for 10 minutes being focused like stupid , lol :>
RE: Craps Game - chmod - 11-03-2013 (11-03-2013, 12:25 AM)dR.0xYw0Rm Wrote: Very NICE game ! I have played for 10 minutes being focused like stupid , lol :> Glad you liked it thanks for the feedback! RE: Craps Game - chmod - 11-03-2013 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 RE: Craps Game - ArkPhaze - 11-03-2013 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. ![]() edit: You've still got a few more: Code: // End if
// End fuction main
// End function diceRollBut 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..
RE: Craps Game - chmod - 11-03-2013 (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. Thanks for the feedback I'm always looking to improve so if you find anything else let me know! RE: Craps Game - ArkPhaze - 11-03-2013 What are you still forgetting about the 'int' datatype? ![]() Code: int bet;Code: while (bet > bank || bet == 0) {}RE: Craps Game - ArkPhaze - 11-04-2013 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. ![]() 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: -234This is critical here, and something most often forgotten about. 'int' is a signed datatype; it extends into integer values below 0 as well. RE: Craps Game - chmod - 11-04-2013 (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. 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. |