Challenge #2 Extended and Completed!!

+5 Emir ARITURK · February 11, 2015
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>

int diceTotal(){
         
       int diceSum ;
        srand(time(0));
      
      diceSum =  (1 + rand()%6 ) +  (1 + rand()%6 ) + (1 + rand()%6 );
        
        return diceSum;
        
}

int main(){
      

      char guess; //Whether the sum is greater or less (Greater : G,g ; Less : L,l)  
       char decision; //Whether the player wants to continue or stop playing (Yes : Y,y ; No: N,n)
        int PrevRoll; //Rolling the dice first
         int NewRoll; //Rolling the dice to compare current and previous results
          int startingCash = 400; // 400$ starting cash
       
       printf("\n   Welcome !! Here we go!!");
       
      
      GAME_START : diceTotal(); //Rolling the dice
        PrevRoll = diceTotal();
        printf("\n\n  The first sum of three dice is : %d \n", PrevRoll );
      COMPARE : puts("\n  How about the next number?");
        printf("  Would it come out greater(G,g) or less(L,l) than %d or equal(E,e) to %d ? : ",PrevRoll,PrevRoll);
          scanf(" %c", &decision); //User enters G , L or E for greater,less or equal
      
      diceTotal(); //Rolling the dice again
         NewRoll = diceTotal();
           printf("\nThe next sum of three dice is : %d \n", NewRoll );
         
         if (NewRoll > PrevRoll){
             
             if ( decision == 'G' || decision == 'g' ){
                puts("Congrats! Right guess, you've won 100$ !! ");
                 startingCash += 100;
                  printf("Current cash is : %d$. ", startingCash);
                 
                 printf("\n\n  Do you want to play again ? (Y/N) : ");
                 scanf(" %c", &decision);
               
                switch(decision){
                    case 'Y' : case 'y' :
                     goto GAME_START;
                    
                     case 'N' : case'n' :
                          goto  END_GAME;
               
                     default:
                         printf(" Wrong decision..");  
                          goto RIGHT_DECISION;
               
                }//end of switch
             }//end of inner if
             
             else if (decision == 'L' || decision == 'l' || decision == 'E' || decision == 'e'){
              printf("Turns out you've guessed it wrong..You lost 120$.."); startingCash-=120; printf("Current cash is : %d$. ", startingCash);
                     if (startingCash <= 0)   
                {
                   puts("\n\n   ---------- You're out of money! Turns out today is not your day.. ----------");
                   goto END_GAME;
                }
                    printf("\n\n Do you want to play again ? (Y/N) : ");
                   scanf(" %c", &decision);
               
                switch(decision){
                    case 'Y' : case 'y' :
                     goto COMPARE;
                    
                     case 'N' : case'n' :
                           goto  END_GAME;
               
                     default:
                         printf(" Wrong decision.. ");  
                          goto RIGHT_DECISION;
              
               } //end of switch
            } //end of inner else if
        } //end of outer if
            
            
         if (NewRoll < PrevRoll){
             
             if (decision == 'L' || decision == 'l'){
                puts("Congrats! Right guess, you've won 100$ !! ");
                 startingCash += 100;
                  printf("Current cash is : %d$. ", startingCash);
                 
                 printf("\n\n Do you want to play again ? (Y/N) : ");
                 scanf(" %c", &decision);
               
                switch(decision){
                    case 'Y' : case 'y' :
                     goto GAME_START;
                    
                     case 'N' : case'n' :
                           goto  END_GAME;
               
                     default:
                         printf(" Wrong decision..");  
                          goto RIGHT_DECISION;
               
                }//end of switch
             }//end of inner if
             
             else if ( decision == 'G' || decision == 'g'|| decision == 'E' || decision == 'e' ){
              printf("Turns out you've guessed it wrong..You lost 120$.."); startingCash-=120; printf("Current cash is : %d$. ", startingCash);
                   if (startingCash <= 0)
                {
                   puts("\n\n   ---------- You're out of money! Turns out today is not your day.. ----------");
                   goto END_GAME;
                }
                  printf("\n\n Do you want to play again ? (Y/N) : ");
                   scanf(" %c", &decision);
               
                switch(decision){
                
                    case 'Y' : case 'y' :
                      goto COMPARE;
                    
                     case 'N' : case'n' :
                          goto  END_GAME;
               
                     default:
                         printf(" Wrong decision.. ");  
                          goto RIGHT_DECISION;
              
               } //end of switch
            } //end of inner else if
         } //end of outer if
         
         
          if (NewRoll == PrevRoll){
             
             if (decision == 'E' || decision == 'e'){
                 puts("Congrats! Right guess, you've won 130$ !! ");
                 startingCash += 130;
                  printf("Current cash is : %d. ", startingCash);
                 
                printf("\n\n Do you want to play again ? (Y/N) : ");
                 scanf(" %c", &decision);
               
                switch(decision){
                    case 'Y' : case 'y' :
                     goto GAME_START;
                    
                     case 'N' : case'n' :
                           goto  END_GAME;
               
                     default:
                         printf(" Wrong decision..");  
                          goto RIGHT_DECISION;
               
                }//end of switch
             }//end of inner if
             
            
                else if ( decision == 'G' || decision == 'g'|| decision == 'L' || decision == 'l' ){
                   printf("Turns out you've guessed it wrong..You lost 120$.."); startingCash-=120; printf("Current cash is : %d\n", startingCash);
                     
                     if (startingCash <= 0)  
                {
                  puts("\n\n   ---------- You're out of money! Turns out today is not your day.. ----------");
                   goto END_GAME;
                }
                   
                    printf("\n\n Do you want to play again ? (Y/N) : ");
                   scanf(" %c", &decision);
               
                    switch(decision){
                    case 'Y' : case 'y' :
                     goto COMPARE;
                    
                     case 'N' : case'n' :
                          goto  END_GAME;
               
                     default:
                         printf(" Wrong decision.. ");  
                          goto RIGHT_DECISION;
              
                   } //end of switch
               } //end of inner else if
            } //end of outer if
         
             RIGHT_DECISION : printf("\n  Do you want to play again ? (Y/N) : ");
                  scanf(" %c", &decision);
               
                  switch(decision){
                      case 'Y' : case 'y' :
                      goto GAME_START;
                    
                       case 'N' : case'n' :
                           goto  END_GAME;
               
                      default:
                         printf(" Wrong decision..");  
                          goto RIGHT_DECISION;
                 
                 }//end of switch
                
         END_GAME : printf("\n\t\t **********  Thanks for joining!  **********\n");
         
    return 0; // Lovin' you babe.. :D
}

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 c student · February 18, 2015
you're supposed to lose in gambling over the long run.  teaches you a lesson to not gamble.
+1 c student · February 12, 2015
i guessing you skipped over this: http://en.wikipedia.org/wiki/Spaghetti_code
+1 Emir ARITURK · February 12, 2015
I know what the spaghetti code is and it occurs when goto's used a few times or a lot. But does it mean that goto is never used? The reason why I used 3 goto terms in this code is, the related pieces of codes that doing similar tasks, had to stand far from each other, they couldn't stand adjacent I mean. This is the reason. But thanks for reminding again for this goto&spaghetti thing ;)
+1 c student · February 12, 2015
goto is used for more extreme cases where it would provide a much cleaner solution than any other method, meaning it should be avoided where possible.  if you wanted to use common sections of code, it would be much better to use functions instead of bulking everything inside main.
0 stephen kessy · February 17, 2015
congrats brother...u didi it 

 but your code doesn,t tell, when the user enters wrong entry ( i.e if the user enter another character other than, g, l and e).. it still print out the value of the next roll even if the user enter, wrong entry....
0 Emir ARITURK · February 18, 2015
a yeah man you're right, turns out I was just stuck in the other bug fixings :P :D
0 Robert Grunn · February 18, 2015
Man it works awesome!! But turns out you used also probabilty theory to build the game because after a particular number of games it's becoming almost impossible to win!! You rip everyone off!! :P
0 c student · February 11, 2015
ohh boyyy...

did you ever do some form of (visual) basic programming in a point of your life?
0 Emir ARITURK · February 18, 2015
Whoaa dude I had really not expected that someone would've realized that!! :D Yes I used some probability theory stuffs such Expected Value, discrete density function etc. So the values are not randomly arranged, every value has got a meaning for expected value's result , because in the end expected value turns into NEGATIVE, that is, you'll have lost money in the end. Ahh bad boy me :P :D :D
0 Emir ARITURK · February 12, 2015
Nope, never. Why did you ask that? :D
  • 1
  • 2

C

107,322 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator