HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Craps game rules and code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
crapscodegameandrules

Problem

I'm developing a small craps game in C++, and my C++ skills are a bit rusty. I really need someone to review my code to ensure that I have correctly implemented the game according to the rules.

Game rules:



  • The player or shooter rolls a pair of standard dice




  • If the sum is 7 or 11 the game is won



  • If the sum is 2, 3 or 12 the game is lost



  • If the sum is any other value, this value is called the shooter’s point and he


continues rolling until he rolls a 7 and loses or he rolls the point again in which
case he wins


  • If a game is won the shooter plays another game and continues playing until he


loses a game, at which time the next player around the Craps table becomes the
shooter


My code:

#include 
#include 

using namespace std;

bool checkWinning(int roll);

int main(int argc, const char * argv[])
{
    //create player aka shooter

    //create pair of dice
    unsigned int dice1=0;
    unsigned int  dice2 = 0;

    //create roll
    unsigned int roll = 0;

    //create game loop
    while(checkWinning(roll) == true)
    {
        dice1 =  rand() % 6 + 1;
        dice2  = rand() % 6 + 1;
        roll = dice1 + dice2;

        cout<< dice1<<" +"<< dice2<<" = "<<  roll << endl;
        //cout<< checkWinning(2) <<endl;
    }

    return 0;
}

bool checkWinning(int roll)
{
    bool winner  = true;
    if( roll == 2 || roll == 3 || roll == 12)
        return winner= false;
    else
        if(roll == 7 || roll == 11 )
        return winner;
    else
        return winner;
};

Solution

-
Try not to use using namespace std.

-
The parameters in main() are only necessary if you're executing from the command line.

-
You're not calling std::srand() nor including `. However, if you're using C++11, both std::srand and std::rand are not recommended due to certain computational complications (the C++11 pseudo-random number generators can be found under ). But, for a simple program, it may not matter.

In general, here's how to call
std::srand():

// casting may just be necessary if warnings are generated
//     that will alert you if there's a possible loss of data
// prefer nullptr to NULL if using C++11
std::srand(static_cast(std::time(NULL)));


Only include this once, preferably at the top of
main(). This is preferred because

  • It'll help you keep track of it, especially if it'll need to be removed at some point.



  • If called repeatedly, you'll receive the "same random number" each time.



-
It's best to keep variables as close in scope as possible. Here,
dice1 and dice2 can be initialized in the while-loop:

unsigned int dice1 = rand() % 6 + 1;
unsigned int dice2 = rand() % 6 + 1;


roll, however, will need to stay where it is so that the loop will work.

-
The
bool-checking can be shortened:

// these are similar
while (checkWinning(roll) == true)
while (checkWinning(roll))


// these are also similar
while (checkWinning(roll) == false)
while (!checkWinning(roll))


-
checkWinning() takes int roll, but roll is already unsigned int. They should match.

-
checkWinning()'s closing curly brace shouldn't have a ;. It's not a type.

-
bool winner seems redundant; just return true or false. Also, the conditions seem a little unclear. If the sum constitutes a win or a re-roll, how do you specifically distinguish the two? They both return true`. I'd at least rename the function for clarification. There's also a Boolean enum, but that may be overkill here (or even unnecessary as there are only two ending outcomes).

-
There should be a final outcome message, indicating a win or a loss. Also, you're not giving the player the option to play another game if victorious (and until loss).

Code Snippets

// casting may just be necessary if warnings are generated
//     that will alert you if there's a possible loss of data
// prefer nullptr to NULL if using C++11
std::srand(static_cast<unsigned int>(std::time(NULL)));
unsigned int dice1 = rand() % 6 + 1;
unsigned int dice2 = rand() % 6 + 1;
// these are similar
while (checkWinning(roll) == true)
while (checkWinning(roll))
// these are also similar
while (checkWinning(roll) == false)
while (!checkWinning(roll))

Context

StackExchange Code Review Q#31451, answer score: 9

Revisions (0)

No revisions yet.