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

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;
  }
}
}
}

 

   
  
 
 
   

 
 
Copy the code and paste it your compiler. Come on guys give it a try.
 
cool okay ^-^