Google Answers Logo
View Question
 
Q: C++ Problem Code ( Answered 3 out of 5 stars,   2 Comments )
Question  
Subject: C++ Problem Code
Category: Computers > Programming
Asked by: atishpanje-ga
List Price: $18.00
Posted: 10 Apr 2005 19:02 PDT
Expires: 10 May 2005 19:02 PDT
Question ID: 507636
I need C++ Programm Code for this Problem:

The Game of Craps
The game of craps is a feature of casino gambling. The game has a
simple set of rules that pits the player against the house. The game
begins by having the player toss two die (a pair of die is called
dice). Depending on the total, we can have an immediate winner.
Otherwise, the game continues.

Initial toss:

a. If the total is a 2, 3, or 12, the house wins (you loose).
b. If the total is a 7 or 11, the player wins.
c. If the total is a 4,5,6, 8,9, or 10, the game continues as instructed below.

The total from the initial toss becomes the target value (or point).
The player continues tossing the dice until the total rolled matches
the "point" or the total rolled is seven (7). lithe total is 7, the
house wins; otherwise, the player wins. Throughout this process, the
house has the advantage, because the total 7 occurs from any of the
pairs (1,6), (2,5), (3,4), (4,3), (5,2), or (6,1); a seven (7) is the
most likely tQtal when rolling two dice.

Requirements:
1. Display a welcome message for a new player.
2. After the initial toss, display the outcome of the roll. You must
research the randO function and know how to seed the random number
generator.
3. If the roll of the dice is as in (b) above display "Win", if the
result is as in (a) above display "LOOSER!! !".
4. If the outcome of the roll is as in (c) apove, display the point,
and ask the player to roll again. As above, based on the next roll
display whether the player: a) rolls again, b) wins, or c) looses.

5. Assume that the player begins with $500.00 to start the game.

6. Maximum table limit is $50.00; a player can bet in $5.00 increments
up to the table limit. Assume all bets are even money (i.e., if you
bet $5.00 and you win you get back $10.00).

7. Write your program that will accept a player's bet before he rolls,
determine the outcome of the play (win/loose), and add to or deduct
from the player's winnings according to his bet.

8. After each play, the program will display the players "stake" (how
much he has left). In addition, the program will ask the player ifhe
wants to play again or quit. If the player is "busted" (looses his
stake), the game ends.

NOTE: You must use a variable "seed" for the random number generator.
Hint: use the C++ time( ) function for returning a number that can be
used to seed the random number generator.
Answer  
Subject: Re: C++ Problem Code
Answered By: studboy-ga on 12 Apr 2005 14:11 PDT
Rated:3 out of 5 stars
 
crap.cpp:

#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;

int roll()
{
   return(rand() % 6 + 1);
}

int main()
{
  int stake = 500, bet = 0;
  int choice = 0;
  int roll1, roll2, sum;

  srand(time(NULL));  //seed

  cout << "welcome to crap!" << endl;
  roll1 = roll();
  roll2 = roll();
  sum = roll1 + roll2;
  cout << "opening roll: " << roll1 << " + " << roll2 << " = " << sum << endl;

  while (stake > 0)
  {
     cout << "stake = " << stake << endl;
     cout << "(0) quit or (1) bet? ";
     cin >> choice;
     if (choice == 1) {
        cout << "how much? ";
        cin >> bet;
        while ((bet % 5 != 0) || (bet < 0) || (bet > 50)) {
           cout << "illegal bet--please bet again:" << endl;
           cout << "how much? ";
           cin >> bet;
        }
        roll1 = roll();
        roll2 = roll();
        sum = roll1 + roll2;
        cout << "roll: " << roll1 << " + " << roll2 << " = " << sum << endl;
        if ((sum == 2) || (sum == 3) || (sum == 12)) {
           cout << "you lose!  try again!" << endl;
           stake -= bet;
        } else if ((sum == 7) || (sum == 11)) {
           cout << "you win!  try again!" << endl;
           stake += bet;
        } else
           cout << "tie!  try again!" << endl;
     } else
        return(0);
  }
}

----------------------------------

Using gcc/g++ to compile on Linux:

> g++ -o crap crap.cpp
> crap
welcome to crap!
opening roll: 4 + 2 = 6
stake = 500
(0) quit or (1) bet? 1
how much? 60
illegal bet--please bet again:
how much? 4
illegal bet--please bet again:
how much? 10
roll: 4 + 3 = 7
you win!  try again!
stake = 510
(0) quit or (1) bet? 1
how much? 15
roll: 6 + 4 = 10
tie!  try again!
stake = 510
(0) quit or (1) bet? 1
how much? 20
roll: 4 + 3 = 7
you win!  try again!
stake = 530
(0) quit or (1) bet? 1
how much? 10
roll: 3 + 5 = 8
tie!  try again!
stake = 530
(0) quit or (1) bet? 1
how much? 40
roll: 2 + 3 = 5
tie!  try again!
stake = 530
(0) quit or (1) bet? 1
how much? 5
roll: 6 + 2 = 8
tie!  try again!
stake = 530
(0) quit or (1) bet? 1
how much? 15
roll: 3 + 5 = 8
tie!  try again!
stake = 530
(0) quit or (1) bet? 1
how much? 20
roll: 4 + 3 = 7
you win!  try again!
stake = 550
(0) quit or (1) bet? 1
how much? 15
roll: 4 + 1 = 5
tie!  try again!
stake = 550
(0) quit or (1) bet? 1
how much? 5
roll: 5 + 6 = 11
you win!  try again!
stake = 555
(0) quit or (1) bet? 1
how much? 10
roll: 6 + 1 = 7
you win!  try again!
stake = 565
(0) quit or (1) bet? 1
how much? 30
roll: 6 + 2 = 8
tie!  try again!
stake = 565
(0) quit or (1) bet? 1
how much? 5
roll: 1 + 1 = 2
you lose!  try again!
stake = 560
(0) quit or (1) bet? 0
>

Clarification of Answer by studboy-ga on 12 Apr 2005 14:15 PDT
Actually because 7 occurs more often player wins more often.  Target added:

int main()
{
  int stake = 500, bet = 0;
  int choice = 0;
  int roll1, roll2, sum, target;

  srand(time(NULL));

  cout << "welcome to crap!" << endl;
  roll1 = roll();
  roll2 = roll();
  target = roll1 + roll2;
  cout << "opening roll: " << roll1 << " + " << roll2 << " = " << target << endl;

  while (stake > 0)
  {
     cout << "stake = " << stake << endl;
     cout << "(0) quit or (1) bet? ";
     cin >> choice;
     if (choice == 1) {
        cout << "how much? ";
        cin >> bet;
        while ((bet % 5 != 0) || (bet < 0) || (bet > 50)) {
           cout << "illegal bet--please bet again:" << endl;
           cout << "how much? ";
           cin >> bet;
        }
        roll1 = roll();
        roll2 = roll();
        sum = roll1 + roll2;
        cout << "roll: " << roll1 << " + " << roll2 << " = " << sum << endl;
        if ((sum == 2) || (sum == 3) || (sum == 12)) {
           cout << "you lose!  try again!" << endl;
           stake -= bet;
        } else if ((sum == 7) || (sum == 11) || (sum == target)) {
           cout << "you win!  try again!" << endl;
           stake += bet;
        } else
           cout << "tie!  try again!" << endl;
     } else
        return(0);
  }
}

Clarification of Answer by studboy-ga on 12 Apr 2005 14:31 PDT
The description of the game is a bit confusing.  I reread it a couple
of times and I think this is what you wanted.  Feel free to change the
program.

int main()
{
  int stake = 500, bet = 0;
  int choice = 0;
  int roll1, roll2, sum, target;

  srand(time(NULL));

  cout << "welcome to crap!" << endl;
  roll1 = roll();
  roll2 = roll();
  target = roll1 + roll2;

  cout << "opening roll: " << roll1 << " + " << roll2 << " = " << target << endl;
  if ((target == 2) || (target == 3) || (target == 12)) {
     cout << "loser!" << endl;
  } else if ((target == 7) || (target == 11)) {
     cout << "win!" << endl;
  }

  while (stake > 0)
  {
     cout << "stake = " << stake << endl;
     cout << "(0) quit or (1) bet? ";
     cin >> choice;
     if (choice == 1) {
        cout << "how much? ";
        cin >> bet;
        while ((bet % 5 != 0) || (bet < 0) || (bet > 50)) {
           cout << "illegal bet--please bet again:" << endl;
           cout << "how much? ";
           cin >> bet;
        }
        roll1 = roll();
        roll2 = roll();
        sum = roll1 + roll2;
        cout << "roll: " << roll1 << " + " << roll2 << " = " << sum << endl;
        if (sum == 7) {
           cout << "you lose!  try again!" << endl;
           stake -= bet;
        } else {
           cout << "you win!  try again!" << endl;
           stake += bet;
        }
     } else
        return(0);
  }
  if (stake <= 0)
     cout << "busted!" << endl;
  return(0);
}

Clarification of Answer by studboy-ga on 12 Apr 2005 15:07 PDT
I reread it one more time and it's till confusing--maybe this is what you want:

int main()
{
  int stake = 500, bet = 0;
  int choice = 0;
  int roll1, roll2, sum, target;

  srand(time(NULL));

  cout << "welcome to crap!" << endl;
  while (stake > 0)
  {
     cout << "stake = " << stake << endl;
     cout << "(0) quit or (1) bet? ";
     cin >> choice;
     if (choice == 1) {
        cout << "how much? ";
        cin >> bet;
        while ((bet % 5 != 0) || (bet < 0) || (bet > 50)) {
           cout << "illegal bet--please bet again:" << endl;
           cout << "how much? ";
           cin >> bet;
        }

        roll1 = roll();
        roll2 = roll();
        target = roll1 + roll2;

        cout << "opening roll: " << roll1 << " + " << roll2 << " = "
<< target << endl;
        if ((target == 2) || (target == 3) || (target == 12)) {
           cout << "loser!" << endl;
           stake -= bet;
           continue;
        } else if ((target == 7) || (target == 11)) {
           cout << "winner!" << endl;
           stake += bet;
           continue;
        }

        sum = 0;

        while ((sum != 7) && (sum != target)) {
           roll1 = roll();
           roll2 = roll();
           sum = roll1 + roll2;
           cout << "roll: " << roll1 << " + " << roll2 << " = " << sum << endl;
           if (sum == 7) {
              cout << "you lose!" << endl;
              stake -= bet;
              continue;
           } else {
              cout << "you win!" << endl;
              stake += bet;
           }
        }
     } else
        return(0);
  }
  if (stake <= 0)
     cout << "busted!" << endl;
  return(0);
}
atishpanje-ga rated this answer:3 out of 5 stars

Comments  
Subject: Re: C++ Problem Code
From: abis799-ga on 24 Oct 2005 13:11 PDT
 
This code has the following problems:
- The worst problem is that it does not let the user enter any odds
amount which is generally 2 times the flat bet at most casinos but can
be up to 10 or 20 times at some.
- Also it prints to the screen " you win " whenever the point number
is on and it rolls a number that is not the point number or a seven.

The code does seem to be logically sound from my debugging.

I would apprecieate it if someone much smarter would answer this question:
- Is the rand() function in C++ really as random as the roll of two die?
You see, when dice are rolled the side facing up when they come to
rest determines the outcome and there are more FACES that can result
in a seven while the rand() function in C++ merely generates a random
number 1 through 6 making the possible combinations of a seven
actually LESS than a true roll of the dice?  Is my logic wrong here or
is there some way to manipulate the rollDice() function to more
acuratly simulate the roll of two die? Thanks anybody who is a C++
programer and a craps wizard!!
Subject: Re: C++ Problem Code
From: ulti-ga on 14 Nov 2005 10:53 PST
 
This is the answer for the question 
"Is the rand() function in C++ really as random as the roll of two
die?" in the comment.

C++ rand() is not as random as the roll of *two* dice. But it is
almost random as the roll of a *single* die.
Since the program used roll() twice, and then sums up the outcomes,
its randomness is the closest we can get to a real roll of two die.

However we must note that C++ rand is not really a random number
generator, its actually a pseudo-random number generator. Thats the
only way to generate random numbers using software. If you want true
random number generator, you must use some physical phenomenon like
nuclear-disintegration or brownian motion or electrical noise from a
resistor or semiconductor etc.

For further information in hardware random number generators visit
http://www.robertnz.net/hwrng.htm

Hope this answers the question.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy