Advertisement
naorleizer

C Mathematic Game

Oct 30th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.61 KB | None | 0 0
  1. /**
  2.  * a game where you need to claculate right the mathemathical equations that appears
  3.  * the game counts points
  4.  * +1 point for right answer -2 points for wrong answer
  5.  * the game ends when the player arrives to 15 points
  6.  * */
  7.  
  8. #define _XOPEN_SOURCE 500
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14. #include <string.h>
  15.  
  16. // function prototype
  17. void clear (void);
  18. void showscore (int);
  19. char opera (int, int, int, int*);
  20. int clean_stdin (void);
  21.  
  22.  
  23. int main(void)
  24. {
  25.     // clear the screen
  26.     clear();
  27.  
  28.     // variables used in program
  29.     // var1 and var2 = math problam varibales
  30.     // input = user input
  31.     // seed = math opartion generator
  32.     // hardness = game dificulty chooser
  33.     // result = right answer
  34.     // score = user score counter
  35.     // wac, rac = "wrong answer counter", "right answer counter"
  36.     int var1, var2, input, seed, hardness, result = 0, score = 0, wac = 0, rac = 0;
  37.  
  38.     // op = "opartion" - char holder for printing: '-' || '+' || 'X'
  39.     // check = checking character in loops to make sure correct input is inserted
  40.     char op = '\0', check;
  41.  
  42.     // game difficulty chooser - 3 options
  43.     // 1 = var1 && var2 <=10 | 2 = var1 && var2 <= 15 | 3 = var1 && var2 <= 20
  44.     do
  45.     {
  46.         clear();
  47.         printf("Get 15 Points to Win!\nChoose Difficulty:\nEASY: 1     MEDIUM: 2       HARD: 3\n");
  48.     }
  49.     // makes sure input is a positive number between 1 and 3, if not, cleans stdin
  50.     while(((scanf("%d%c", &hardness, &check) != 2 || check != '\n') && clean_stdin()) || hardness < 1 || hardness > 3);
  51.        
  52.  
  53.     // convert hardness to in game top numbers for var1 & var2
  54.     hardness = (hardness +1) * 5 +1;
  55.  
  56.     // delay for animation sake
  57.     usleep(800000);
  58.     clear();
  59.  
  60.     // game loop when score is less than 15
  61.     while(score < 15)
  62.     {
  63.         // user input reset
  64.         input = 0;
  65.  
  66.         // pseudo random numbers generator, using system time as seed
  67.         srand48(time(NULL) % 257);
  68.         var1 = drand48() * 10000;
  69.         var1 = var1 % hardness;
  70.         srand48(time(NULL));
  71.         var2 = drand48() *257;
  72.         var2 = var2 % hardness;
  73.  
  74.         // seed generator for opartion decision
  75.         srand48(time(NULL) % 1234);
  76.         seed = (drand48() *12345);
  77.         seed = seed  % 100;
  78.  
  79.         // swap var1 and var2 if - opartions is generated and var2 > var1 to avoid negative numbers
  80.         if(seed > 33 && seed <= 66 && var1 < var2)
  81.         {
  82.             var1 = var1 ^ var2;
  83.             var2 = var1 ^ var2;
  84.             var1 = var1 ^ var2;
  85.         }
  86.  
  87.         // op for opartion printing and result sum for checking user answer
  88.         op = opera(seed, var1, var2, &result);
  89.  
  90.         // recive answer from user
  91.         do
  92.         {
  93.             clear();
  94.             // score print
  95.             showscore(score);
  96.  
  97.             // print exrcise
  98.             printf("\n%i %c %i = ", var1, op, var2);
  99.         }
  100.         while((scanf("%d%c", &input, &check) !=2 || check != '\n') && clean_stdin());
  101.  
  102.         // cheat
  103.         if(input == 257)
  104.             break;
  105.  
  106.         // clear screen for animation sake
  107.         clear();
  108.  
  109.         // check for right answer
  110.         if(input == result)
  111.         {
  112.             // score update and right answer counter update
  113.             score++;
  114.             rac++;
  115.    
  116.             // print score and right answer massage
  117.             showscore(score);
  118.             printf("\n%i is Correct! +1 Point!\n", result);
  119.  
  120.             // delay for animation sake
  121.             usleep(1200000);
  122.         }
  123.  
  124.         // wrong answer handle
  125.         else
  126.         {
  127.             // wrong answer counter update
  128.             wac++;
  129.  
  130.             // 0 or 1 score handle (to avoid negate score)
  131.             if(score == 0 || score == 1)
  132.             {
  133.                 score = 0;
  134.                 showscore(score);
  135.                 printf("\n%i is Wrong!", input);
  136.             }
  137.  
  138.             // 1 > score handle
  139.             else
  140.             {
  141.                 // score update
  142.                 score-=2;
  143.  
  144.                 // score print wrong answer massage
  145.                 showscore(score);
  146.                 printf("\n%i is Wrong! -2 Points!", input);
  147.             }
  148.  
  149.             // right answer print
  150.             printf("\n\nThe Right answer is: %i\n\n", result);
  151.  
  152.             // delay for animation sake
  153.             usleep(2000000);
  154.         }
  155.  
  156.         // clear screen for animation sake
  157.         clear();
  158.     }
  159.  
  160.     // winning message
  161.     for(int i = 0; i < 6; i++)
  162.     {
  163.         clear();
  164.         usleep(400000);
  165.         printf("########    VICTORY!!   ########\n\nRight answers: %i       Wrong answers: %i\n", rac, wac);
  166.         printf("            Well Done!\n    Your'e great at 2nd Grade Math!\n");
  167.         usleep(600000);
  168.     }
  169.    
  170.     // delay for animation sake
  171.     usleep(2500000);
  172.  
  173.     // program exit
  174.     return 0;
  175. }
  176.  
  177. void clear (void)
  178. {
  179.     // clear the screen
  180.     printf("\033[2J");
  181.     printf("\033[%d;%dH", 0, 0);
  182. }
  183.  
  184. int clean_stdin (void)
  185. {
  186.     while(getchar() != '\n');
  187.     return 1;
  188. }
  189.  
  190. void showscore (score)
  191. {
  192.     // print score - usage at the begining of every turn during the game
  193.     printf("score: %i points\n", score);
  194. }
  195.  
  196. char opera (int seed, int var1, int var2, int* presult)
  197. // this function return a relevant mathmetical opartion acording to the generated seed
  198. {
  199.     if(seed <= 33)
  200.     {
  201.         *presult = var1 * var2;
  202.         return 'X';
  203.     }
  204.     else if(seed > 33 && seed < 66)
  205.     {
  206.         *presult = var1 - var2;
  207.         return '-';
  208.     }
  209.     else
  210.     {
  211.         *presult = var1 + var2;
  212.         return '+';
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement