Craps Dice game in C++
Posted by Samath
Last Updated: January 06, 2021

Craps is a dice game in which the players make wagers on the outcome of the roll, or a series of rolls, of a pair of dice.  You are required to write a program to play a variation of the game, as follows:

Roll two dice. Each die has six faces representing values 1, 2, 3, 4, 5, and 6 respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e. 4, 5, 6, 8, 9, or 10), a point is established. Continue until you roll a 2, 3, or 12 (you lose) or the same point value (you win). Your program acts as a single player.

You should use a function to check whether or not a player won the game based on the information presented above. (You are expected to randomly generate the dice values)

#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
using namespace std;

void sleep(unsigned int mseconds)
{
    clock_t sp = mseconds + clock();
    while (sp > clock());
}

int main()
{  
            int dice1, dice2;
            int sum;
            char roll;
            int r;

cout<<"\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"\t\t\t< \tCRAPS \t\t>"<<endl;
cout<<"\t\t\t< \tDice Game \t>"<<endl;
cout<<"\t\t\t< \t\t\t>"<<endl;
cout<<"\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl<<endl;

cout<<"Press R to Roll the dice: ";
cin>>roll;

if(roll == 'R' || roll == 'r')
{
            for(;;)
            {
                        srand( time(NULL) );
                        dice1 = rand() % 6 + 1;
                        dice2 = rand() % 6 + 1;
                        sum = dice1 + dice2;

  switch(sum)
  {
            case 2:
            cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"You Lose";
            cout<<endl;
                        sleep(1000);
            break;
            case 3:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"You Lose";
            cout<<endl;
            sleep(1000);
            break;
            case 4:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"Point is "<<sum;
            cout<<endl;
           sleep(1000);
            break;
           case 5:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"Point is "<<sum;
            cout<<endl;
             sleep(1000);
            break;
          case 6:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"Point is "<<sum;
            cout<<endl;
                        sleep(1000);
            break;
                        case 7:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"You Win";
            cout<<endl;
                        sleep(1000);
            break;
            case 8:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"Point is "<<sum;
            cout<<endl;
                                    sleep(1000);
            break;
                        case 9:

    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"Point is "<<sum;
            cout<<endl;
                        sleep(1000);
            break;
            case 10:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"Point is "<<sum;
            cout<<endl;
            sleep(1000);
            break;
              case 11:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"You Win";
            cout<<endl;
                        sleep(100);
            break;
            case 12:
    cout<<"You rolled "<<dice1<< " + " <<dice2<< " = " << sum;
            cout<<endl;
            cout<<"You Lose";
            cout<<endl;
            sleep(1000);
            break;           
  }

  if(sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10)
  {
      continue;
  }
  else
  {
            break;
  }
}
}
}

 

Related Content
The Matching Game in Java
The Matching Game in Java
Samath | Mar 20, 2015
Hangman Game in C++
Hangman Game in C++
Samath | Jan 17, 2024
Hangman Game using C#
Hangman Game using C#
Samath | Jan 17, 2024
Animal Game in Java
Animal Game in Java
gideonna | Apr 13, 2016
Number Guessing Game using C++
Number Guessing Game using C++
Samath | Jan 04, 2017
Blackjack Game in C++
Blackjack Game in C++
Samath | Jan 05, 2017
Checkers Game using C++
Checkers Game using C++
Samath | Jan 17, 2024
Minesweeper Game using C++
Minesweeper Game using C++
Samath | Jan 05, 2017
Paper Strip Game using Python
Paper Strip Game using Python
Samath | Jan 17, 2024
Tic Tac Toe Game using C#
Tic Tac Toe Game using C#
Samath | Jan 17, 2024
John Conway's Game of Life using C++
John Conway's Game of Life using C++
Samath | Jan 04, 2017
C++ Rock Paper Scissors Game
C++ Rock Paper Scissors Game
Samath | Jan 05, 2017
Tic Tac Toe Game using Python
Tic Tac Toe Game using Python
Samath | Jan 17, 2024
Casino Number Guessing Game in C++
Casino Number Guessing Game in C++
Samath | Jan 20, 2024