Advertisement
Guest User

Untitled

a guest
Sep 26th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <ctype.h>
  7.  
  8. const int iMaxAttempts = 4;
  9. char output[11];
  10. bool bCorrect = false;
  11. int iAttempt = 0;
  12.  
  13. void correct(int iRandom, int guess){
  14.     if(guess == iRandom){
  15.         strncpy(output,"Correct!\n", 10);
  16.         bCorrect =  true;
  17.     }
  18.     else if(guess < iRandom){
  19.         strncpy(output, "Too low!\n", 10);
  20.         bCorrect = false;
  21.     }else if(guess > iRandom){
  22.         strncpy(output, "Too high!\n", 11);
  23.         bCorrect = false;
  24.     }
  25. }
  26.  
  27. void gameplay(void){
  28.     int guess = 0,iRandom = 0; 
  29.     iAttempt = 0;
  30.     srand(time(NULL));
  31.     iRandom = (rand() %128) + 1;
  32.     while(iAttempt < iMaxAttempts){
  33.         //Asking for a number
  34.         printf("Please enter a guess between 1 and 128 \n");
  35.         printf("I think the number is: ");
  36.         //get input
  37.         scanf("%d", &guess);
  38.         //cleaning up text so next text will be the output will be on a new line
  39.         printf("\n");
  40.        
  41.         //checks if input is recieved
  42.         if(guess <= 0)continue;
  43.         iAttempt++;
  44.         //check if number is right
  45.         correct(iRandom, guess);
  46.         if(bCorrect){
  47.             printf("%s You guessed correctly within %d ties! \n", output, iAttempt);
  48.             bCorrect= true;
  49.             break; 
  50.         }else{
  51.             printf("%s You have %d tries left ", output, iMaxAttempts - iAttempt);
  52.         }
  53.                
  54.     }
  55.     if(!bCorrect)printf("Unlucky, the random number was %d, better luck next time!\n", iRandom);
  56.    
  57. }
  58. bool playAgain(void){
  59.     char cRetry[2];
  60.     cRetry[0] = '\0';
  61.  
  62.     while(cRetry != "y" && cRetry != "n"){
  63.         printf("Would you like to try again? [y/n]");
  64.         fflush(stdout);
  65.         fgets(cRetry, 2, stdin);   
  66.         if(cRetry[0]){
  67.             cRetry[0] = tolower(cRetry[0]);
  68.         }
  69.     }
  70.    
  71.     return (cRetry[0] == "y");
  72. }
  73. void printRatio(int wins, int loses, int attempts){
  74.     printf("Wins: %d | Loses: %d | Attempts: %d \n", wins, loses, attempts);
  75.     float avAttempts = attempts / wins;
  76.     printf("You have a win ratio of: %d/%d and an average attempts per win of: %f \n", wins, loses, avAttempts);
  77. }
  78. int main(void){
  79.     int *iTotalAttempts, *iTotalLoses, *iTotalWins;
  80.     while(true){
  81.         //Allocating memory
  82.         iTotalAttempts = (int *)malloc(sizeof(int));
  83.         iTotalLoses = (int *)malloc(sizeof(int));
  84.         iTotalWins = (int *)malloc(sizeof(int));
  85.         gameplay();
  86.  
  87.        
  88.         //Reallocating memory and storying data
  89.         if(bCorrect){
  90.             iTotalAttempts = (int *)realloc(iTotalAttempts, sizeof(*iTotalAttempts + iAttempt));
  91.             *iTotalAttempts+= iAttempt;
  92.             iTotalWins = (int *)realloc(iTotalWins, sizeof(*iTotalWins + 1));
  93.             *iTotalWins+= 1;
  94.         }
  95.         else{
  96.             iTotalLoses = (int *)realloc(iTotalLoses, sizeof(*iTotalWins + 1));
  97.             *iTotalLoses+= 1;
  98.         }
  99.        
  100.         //checking to see if they want to play again and acting accordingly
  101.         bool bPlayAgain = playAgain();
  102.         if(!bPlayAgain)break;
  103.        
  104.        
  105.     }
  106.     printRatio(*iTotalWins, *iTotalLoses, *iTotalAttempts);
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement