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 2
Oldest  Newest  Rating
+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
i guessing you skipped over this: http://en.wikipedia.org/wiki/Spaghetti_code
0 Emir ARITURK · February 12, 2015
I can't understand what you mean. Are we on an exam or smth like that? :D  but anyway, here is what I know about goto : http://en.wikipedia.org/wiki/Goto . And the work is right above. If I didn't have any idea about goto, believe me all those above would be quite stupid
0 c student · February 12, 2015
what do you know about goto?
0 Emir ARITURK · February 12, 2015
Nope, never. Why did you ask that? :D
0 c student · February 11, 2015
ohh boyyy...

did you ever do some form of (visual) basic programming in a point of your life?
  • 1
  • 2

C

107,130 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator