Guest User

Untitled

a guest
Apr 17th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <time.h>
  5. #include <math.h>
  6.  
  7. // Operator Ranomization Information
  8. char const *operators[] = {"addition",
  9.                            "subtraction",
  10.                            "multiplication",
  11.                            "division"};
  12. int o_lower = 0, o_upper = 3;
  13. char const *chosen_operator;
  14. int chosen_operator_number;
  15.  
  16. // Setting up #s for equations
  17. int number1;
  18. int number2;
  19. int n_lower = 0, n_upper = 1000;
  20.  
  21. // Setting up user awnser
  22. int user_awnser;
  23. float real_awnser;
  24.  
  25. // Starting # of lives
  26. int lives = 5;
  27.  
  28. // Starting # of score
  29. int score = 0;
  30.  
  31. // Pre-calling functions
  32. void operator_setup(int o_lower, int o_upper);
  33. void number_setup(int n_lower, int n_upper);
  34. void equation_setup();
  35. void user_input();
  36. void awnser_check();
  37. void log_game();
  38.  
  39. // Main funciton
  40. int main(int argc, char **argv)
  41. {
  42.   printf("Welcome to MathGame V0.1 \nBy: Maksim\n");
  43.   printf("All number should be rounded to the nearest whole number.\n");
  44.   printf("Press 'Enter' to begin.\n");
  45.   while(getchar() != '\n');
  46.   while (lives != 0)
  47.   {
  48.     operator_setup(o_lower, o_upper);
  49.     number_setup(n_lower, n_upper);
  50.     equation_setup();
  51.     user_input();
  52.     awnser_check();
  53.   }
  54.   printf("Game Over\nScore: %i\n", score);
  55.   log_game();
  56. }
  57.  
  58. // Randomizing operator
  59. void operator_setup(int o_lower, int o_upper)
  60. {
  61.   srand(time(NULL));
  62.   int o_ran = (rand() % (o_upper - o_lower)) + o_lower;
  63.   chosen_operator = operators[o_ran];
  64. }
  65.  
  66. // Randomizing Numbers
  67. void number_setup(int n_lower, int n_upper)
  68. {
  69.   srand(time(NULL) / 1.0415);
  70.   number1 = (rand() % (n_upper - n_lower)) + n_lower;
  71.  
  72.   srand(time(NULL) / 9.4281);
  73.   number2 = (rand() % (n_upper - n_lower)) + n_lower;
  74. }
  75.  
  76. // Creating equation and awnsering it
  77. void equation_setup()
  78. {
  79.   if (chosen_operator == operators[0])
  80.   {
  81.     printf("%i + %i = ", number1, number2);
  82.     chosen_operator_number = 0;
  83.     real_awnser = number1 + number2;
  84.   }
  85.   if (chosen_operator == operators[1])
  86.   {
  87.     printf("%i - %i = ", number1, number2);
  88.     chosen_operator_number = 1;
  89.     real_awnser = number1 - number2;
  90.   }
  91.   if (chosen_operator == operators[2])
  92.   {
  93.     printf("%i * %i = ", number1, number2);
  94.     chosen_operator_number = 2;
  95.     real_awnser = number1 * number2;
  96.   }
  97.   if (chosen_operator == operators[3])
  98.   {
  99.     printf("%i / %i = ", number1, number2);
  100.     chosen_operator_number = 3;
  101.     real_awnser = number1 / number2;
  102.   }
  103. }
  104.  
  105. void user_input()
  106. {
  107.   scanf("%i", &user_awnser);
  108. }
  109.  
  110. void awnser_check()
  111. {
  112.   real_awnser = roundf(real_awnser);
  113.   if (user_awnser == real_awnser)
  114.   {
  115.     score++;
  116.     printf("Corrent!\nLives: %i Score: %i\n\n", lives, score);
  117.   }
  118.   if (user_awnser != real_awnser)
  119.   {
  120.     lives = lives -1;
  121.     printf("Not corrent.\nLives: %i Score: %i\n\n", lives, score);
  122.   }
  123. }
  124.  
  125. void log_game()
  126. {
  127.   time_t t;
  128.   time(&t);
  129.   FILE *file;
  130.   file = fopen("score.log", "a+");
  131.   fprintf(file, "Score: %i Game ended at: %s", score, ctime(&t));
  132.   fclose(file);
  133. }
Add Comment
Please, Sign In to add comment