C Challenge 2 Dice game

+3 Saya Minatsuki · January 25, 2016
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>


int main()
{
    int x;
    int opt;
    int dice1;
    int dice2;
    int dice3;


   srand(time(0));
printf("Welcome to dice game! \n \n");
printf(" DICE ROLLING . . . . . . . . \n \n");
printf(". . . . . . . . \n \n");
   for (x =1; x<2 ; x++){
        dice1=1+rand()%6;
   printf("First dice roll: %d \n \n \n", dice1);
   }

printf("DICE ROLLING . . . . . . . . \n \n");
printf(". . . . . . . . \n \n");

    for (x =1; x<2 ; x++){
             dice2=1+rand()%6;
    printf("Second dice roll : %d \n \n \n", dice2);
   }

   printf("********************** \n ");
   printf("Last dice roll is: %d \n",dice2);
   printf("********************** \n \n \n");


     for (x =1; x<2 ; x++){
    dice3 = 1+rand()%6;

   }

    printf("Please guess if the third dice will be lower (1), higher(2) or same(0) to the last dice roll: \n");
   scanf( " %d", &opt);


if ((opt == 1)&& (dice3<dice2)){

        printf("DICE ROLLING . . . . . . . . \n \n");
        printf(". . . . . . . . \n \n");

        printf("********************** \n ");
        printf("Third dice roll is: %d \n",dice3);
        printf("********************** \n \n \n");
        printf("GOOD CHOICE !!, Third dice is lower to the last dice roll buddy! \n");



}
else if((opt == 2) && (dice3 > dice2)){
        printf("DICE ROLLING . . . . . . . . \n \n");
        printf(". . . . . . . . \n \n");

        printf("********************** \n ");
        printf("Third dice roll is: %d \n",dice3);
        printf("********************** \n \n \n");
        printf("GOOD CHOICE !!, Third dice is higher to the last dice roll buddy! \n");

}

else if((opt == 0) && (dice3 == dice2)){

        printf("DICE ROLLING . . . . . . . . \n \n");
        printf(". . . . . . . . \n \n");

        printf("********************** \n ");
        printf("Third dice roll is: %d \n",dice3);
        printf("********************** \n \n \n");
        printf("GOOD CHOICE !!, Third dice is equal to the last dice roll buddy! \n");


}
else {

    printf("Wrong , Please try again !! \n");
}


}



----- Just trying to give it a shot, I'm open to suggestions thanks 

Post a Reply

Replies

Oldest  Newest  Rating
0 Saya Minatsuki · January 28, 2016
Yeah I see I put char there, I think I should declare it 1 as a char too, but I made some revisions on my code again , I didn't use while (opt != 1) ,


 I made it bool done=true;
while(done){


 for (int x =1; x<2 ; x++){

        a=1+rand()%6;
        b=1+rand()%6;
        c=1+rand()%6;
        sum1 = a+b+c;

     cout<<"****************************\n";
     cout<<"DICE ROLLING . . . . . . . . "<<endl;
     cout<<"FIRST DICE ROLL !"<<endl;
     cout<<"dice 1 ="<< a<<endl;
     cout<<"dice 2 ="<< b<<endl;
     cout<<"dice 3 ="<< c<<endl;
     cout<<"sum is :"<< sum1 <<endl<<endl;
 }


     cout<<" \n Please guess what will be the sum of the next dice roll !" <<endl;
     cout<<"(Press (l)-lower,(h)-higher, (s)same or 1 to quit the game )"<<endl;
     cin>>opt;


     for (int x =1; x<2 ; x++){
        a=1+rand()%6;
        b=1+rand()%6;
        c=1+rand()%6;
        sum2 = a+b+c;

     }

     switch(opt){


     case 'l':
         if(sum1 > sum2){
          cout<< "\n";
          cout<<"DICE ROLLING . . . . . . . . "<<endl;
          cout<<"****RESULTS****"<<endl;
          cout<<"SECOND DICE ROLL !"<<endl;
              cout<<"dice 1 ="<< a<<endl;
              cout<<"dice 2 ="<< b<<endl;
              cout<<"dice 3 ="<< c<<endl;

         cout<< "The sum of the second dice roll is "<<sum2<<" "<< "which is lower!"<<endl;
         cout<< "You got it right congrats buddy !" <<endl <<endl;
         }
         else{
              cout<<"You guessed it wrong!, Please try again"<<endl;
         }
         break;

     case 'h':
         if(sum1 < sum2){
              cout<< "\n";
              cout<<"DICE ROLLING . . . . . . . . "<<endl;
              cout<<"****RESULTS****"<<endl;
              cout<<"SECOND DICE ROLL !"<<endl;
              cout<<"dice 1 ="<< a<<endl;
              cout<<"dice 2 ="<< b<<endl;
              cout<<"dice 3 ="<< c<<endl;

         cout<< "The sum of the second dice roll is "<<sum2<<" "<< "which is higher!"<<endl;
         cout<< "You got it right congrats buddy !" <<endl <<endl ;
         }
            else{
              cout<<"You guessed it wrong!, Please try again"<<endl;
         }
         break;

     case 's':
        if(sum1 == sum2){
              cout<< "\n";
              cout<<"DICE ROLLING . . . . . . . . "<<endl;
              cout<<"SECOND DICE ROLL !"<<endl;
              cout<<"****RESULTS****"<<endl;
              cout<<"dice 1 ="<< a<<endl;
              cout<<"dice 2 ="<< b<<endl;
              cout<<"dice 3 ="<< c<<endl;

         cout<< "The sum of the second dice roll is "<<sum2<<" "<< "which is same!"<<endl;
         cout<< "You got it right congrats buddy !"<<endl <<endl ;
        }
           else{
              cout<<"You guessed it wrong!, Please try again"<<endl <<endl;
         }
         break;

       default:
                done = false;
             cout<<"Thank you for playing!"<<endl;
                 break;

and it stopped the loop, by the way thank you for debugging my program ! :D
0 Linguist Llama · January 28, 2016
What is this cout doing in the C forum? cout is for C++... Which language are you learning? Choose the correct forum...
0 Linguist Llama · February 3, 2016
Please migrate to the C++ forum with your C++ code.
0 Linguist Llama · January 26, 2016
Consider rolling all of the dice in the same loop...

Also, in many of your branches there's logic that could be hoisted into a separate function and called to significantly reduce unnecessarily redundant code. For example::

if (opt == 1 && dice3 < dice2) {
    success(dice3, "lower than");
}
else if (opt == 2 && dice3 > dice2) {
    success(dice3, "higher than");
}
else if (opt == 3 && dice3 == dice2) {
    success(dice3, "equal to");
}
else {
    puts("Wrong. Please try again...\n");
}

Does that make sense?
0 Saya Minatsuki · January 27, 2016
I see so to make it short , to remove repetitive codes, you want me to create a funtion that will loop the dices to call it?
0 Saya Minatsuki · January 27, 2016
Hi I revised my code, however I'm getting a problem ending the while loop with a switch case:


Here's my code:




#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(){

int a;
int b;
int c;
int sum1;
int sum2;
char opt;



   srand(time(0));

//while(opt !=1){


 for (int x =1; x<2 ; x++){

        a=1+rand()%6;
        b=1+rand()%6;
        c=1+rand()%6;
        sum1 = a+b+c;

     cout<<"****************************\n";
     cout<<"DICE ROLLING . . . . . . . . "<<endl;
     cout<<"FIRST DICE ROLL !"<<endl;
     cout<<"dice 1 ="<< a<<endl;
     cout<<"dice 2 ="<< b<<endl;
     cout<<"dice 3 ="<< c<<endl;
     cout<<"sum is :"<< sum1 <<endl<<endl;
 }


     cout<<"Please guess if the sum of the next dice roll will be lower-(l), higher-(h), or same-(s), 1 to quit:"<<endl;
     cin>>opt;


     for (int x =1; x<2 ; x++){
        a=1+rand()%6;
        b=1+rand()%6;
        c=1+rand()%6;
        sum2 = a+b+c;

     }

     switch(opt){


     case 'l':
         if(sum1 > sum2){
          cout<< "\n";
          cout<<"DICE ROLLING . . . . . . . . "<<endl;
          cout<<"SECOND DICE ROLL !"<<endl;
              cout<<"dice 1 ="<< a<<endl;
              cout<<"dice 2 ="<< b<<endl;
              cout<<"dice 3 ="<< c<<endl;

         cout<< "The sum of the second dice roll is "<<sum2<<" "<< "which is lower!"<<endl;
         cout<< "You got it right congrats buddy !" <<endl <<endl;
         }
         else{
              cout<<"You guessed it wrong!, Please try again"<<endl;
         }
         break;

     case 'h':
         if(sum1 < sum2){
              cout<< "\n";
              cout<<"DICE ROLLING . . . . . . . . "<<endl;
              cout<<"SECOND DICE ROLL !"<<endl;
              cout<<"dice 1 ="<< a<<endl;
              cout<<"dice 2 ="<< b<<endl;
              cout<<"dice 3 ="<< c<<endl;

         cout<< "The sum of the second dice roll is "<<sum2<<" "<< "which is higher!"<<endl;
         cout<< "You got it right congrats buddy !" <<endl <<endl ;
         }
            else{
              cout<<"You guessed it wrong!, Please try again"<<endl;
         }
         break;

     case 's':
        if(sum1 == sum2){
              cout<< "\n";
              cout<<"DICE ROLLING . . . . . . . . "<<endl;
              cout<<"SECOND DICE ROLL !"<<endl;
              cout<<"dice 1 ="<< a<<endl;
              cout<<"dice 2 ="<< b<<endl;
              cout<<"dice 3 ="<< c<<endl;

         cout<< "The sum of the second dice roll is "<<sum2<<" "<< "which is same!"<<endl;
         cout<< "You got it right congrats buddy !"<<endl <<endl ;
        }
           else{
              cout<<"You guessed it wrong!, Please try again"<<endl <<endl;
         }
         break;

     default:{
        cout<<"Thanks for playing!"<<endl;
        break;

       }
     }
}



-- Can you please help me with this thanks


  • 1

C

107,323 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator