# Craps Game - Need Help

Hello, i am very new to arduino and am currently trying to work on a project for school. Our goal is to make a very basic Craps game but i can not figure out how to code it. Basically here is what i need to do…

Roll Dice

Add the dice together

Check it if equals 7 or 11, If i does say Loser
]
if it doesn’t roll again

If it equals 11, say Loser, if it equals the number you rolled the first time say Winner Winner Chicken Dinner, and if it equals something other than these re roll until you get your number or an 11

Thanks for the help in advance!

Hello and welcome,

Since it's a school project I will just tell you to look at the random function. What you describe is very simple and shouldn't take more than 20 lines of code :)

thanks for that, but i have already tried to code that with success, but i could never get the two numbers to add up and to do the other functions that i needed. Do you think that you might be able to help me a little more on that part?

Team up with a classmate.

TheAllegedGamer: but i could never get the two numbers to add up

``````result = a + b;
``````

I can help only if you can prove that you put some efforts in at least trying ;)

Anyway, is there any human interaction with your program? Like, should a player press a button to roll the dice ? Or you just want the arduino playing alone?

I was going to put a button into it yes, but i wanted to get the general code down first

Let's see if you did make an effort. Post your code, and please use code tags.

ok, my code is not currently with me because i am not on my school computer but i will post it when i get the chance, thanks for the help so far!

But there is basically nothing in the "general code", it's just a few comparisons

How you compare two numbers:

``````if ( number1 == number2 )
{
// equal
}
else
{
// not equal
}
``````

Adding buttons, possibly LCD... will probably be the hardest part, but not too hard.

guix: Adding buttons, possibly LCD... will probably be the hardest part, but not too hard.

I doubt that they'll ask for an LCD, probably just use the serial interface.

So... Had nothing to do so, as a training for myself and because I may be too nice, I decided to write a basic state machine for your game. No human interaction, you do that yourself ;)

``````enum gameStates_t : uint8_t
{
newGame,
rollDice,
checkWin,
gameOver
};

gameStates_t gameState;
uint8_t dice1, dice2, diceSum, firstSum;
bool firstRoll;
char buf[64];

void setup()
{
Serial.begin( 115200 );
randomSeed( analogRead( 0 ) );
gameState = newGame;
}

void loop()
{
switch ( gameState )
{
case newGame :
{
Serial.println( "Entering state 'newGame'..." );

Serial.println( "New game!" );
firstRoll = true;

gameState = rollDice;
break;
}

case rollDice :
{
Serial.println( "Entering state 'rollDice'..." );

dice1 = random( 6 ) + 1;
dice2 = random( 6 ) + 1;
diceSum = dice1 + dice2;

sprintf( buf, "Rolling dice... Results: %d + %d = %d", dice1, dice2, diceSum );
Serial.println( buf );

gameState = checkWin;
break;
}

case checkWin :
{
Serial.println( "Entering state 'checkWin'..." );

if ( firstRoll == true )
{
if ( diceSum == 7 || diceSum == 11 )
{
Serial.println( "Loser!" );
gameState = gameOver;
}
else
{
firstRoll = false;
firstSum = diceSum;
gameState = rollDice;
}
}
else
{
if ( diceSum == 11 )
{
Serial.println( "Loser!" );
gameState = gameOver;
}
else if ( diceSum == firstSum )
{
Serial.println( "Winner!" );
gameState = gameOver;
}
else
{
gameState = rollDice;
}
}
break;
}

case gameOver :
{
Serial.println( "Entering state 'gameOver'..." );

Serial.println( "Playing again in 5 seconds, or you can press the Reset button too.\n\n" );
delay( 5000 );

gameState = newGame;
break;
}
}
}
``````

Not perfect but seems to work as you described... As you can see it's simple enought to translate from english to C ;). No joke, finding my Arduino took longer than writing this code..

Edit: improved, this should be a good base for your program.

I am very confused because whenever i try to run the code it puts a lot of random charachters on my serial port. Do you know why?

When you open the serial monitor, what number is in the lower right corner? make sure this number Serial.begin( ... ) matches that number.

thank you i figured it out!

Here some additional data and code if you want to print the dices and their pips on the serial monitor:

``````char* dice[6][5]={
" ----- ",
"|     |",
"|  X  |",
"|     |",
" -----",

" ----- ",
"|X    |",
"|     |",
"|    X|",
" -----",

" ----- ",
"|X    |",
"|  X  |",
"|    X|",
" -----",

" ----- ",
"|X   X|",
"|     |",
"|X   X|",
" -----",

" ----- ",
"|X   X|",
"|  X  |",
"|X   X|",
" -----",

" ----- ",
"|X   X|",
"|X   X|",
"|X   X|",
" -----",
};

void printDice(int pips)
{
pips--; // dice array is zero based
for (int i=0;i<5;i++)
{
Serial.println(dice[pips][i]);
}
Serial.println();
}
``````

If I were an Arduino teacher, I would read this forum. ;)