Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <time.h>
- #include <math.h>
- // Operator Ranomization Information
- char const *operators[] = {"addition",
- "subtraction",
- "multiplication",
- "division"};
- int o_lower = 0, o_upper = 3;
- char const *chosen_operator;
- int chosen_operator_number;
- // Setting up #s for equations
- int number1;
- int number2;
- int n_lower = 0, n_upper = 1000;
- // Setting up user awnser
- int user_awnser;
- float real_awnser;
- // Starting # of lives
- int lives = 5;
- // Starting # of score
- int score = 0;
- // Pre-calling functions
- void operator_setup(int o_lower, int o_upper);
- void number_setup(int n_lower, int n_upper);
- void equation_setup();
- void user_input();
- void awnser_check();
- void log_game();
- // Main funciton
- int main(int argc, char **argv)
- {
- printf("Welcome to MathGame V0.1 \nBy: Maksim\n");
- printf("All number should be rounded to the nearest whole number.\n");
- printf("Press 'Enter' to begin.\n");
- while(getchar() != '\n');
- while (lives != 0)
- {
- operator_setup(o_lower, o_upper);
- number_setup(n_lower, n_upper);
- equation_setup();
- user_input();
- awnser_check();
- }
- printf("Game Over\nScore: %i\n", score);
- log_game();
- }
- // Randomizing operator
- void operator_setup(int o_lower, int o_upper)
- {
- srand(time(NULL));
- int o_ran = (rand() % (o_upper - o_lower)) + o_lower;
- chosen_operator = operators[o_ran];
- }
- // Randomizing Numbers
- void number_setup(int n_lower, int n_upper)
- {
- srand(time(NULL) / 1.0415);
- number1 = (rand() % (n_upper - n_lower)) + n_lower;
- srand(time(NULL) / 9.4281);
- number2 = (rand() % (n_upper - n_lower)) + n_lower;
- }
- // Creating equation and awnsering it
- void equation_setup()
- {
- if (chosen_operator == operators[0])
- {
- printf("%i + %i = ", number1, number2);
- chosen_operator_number = 0;
- real_awnser = number1 + number2;
- }
- if (chosen_operator == operators[1])
- {
- printf("%i - %i = ", number1, number2);
- chosen_operator_number = 1;
- real_awnser = number1 - number2;
- }
- if (chosen_operator == operators[2])
- {
- printf("%i * %i = ", number1, number2);
- chosen_operator_number = 2;
- real_awnser = number1 * number2;
- }
- if (chosen_operator == operators[3])
- {
- printf("%i / %i = ", number1, number2);
- chosen_operator_number = 3;
- real_awnser = number1 / number2;
- }
- }
- void user_input()
- {
- scanf("%i", &user_awnser);
- }
- void awnser_check()
- {
- real_awnser = roundf(real_awnser);
- if (user_awnser == real_awnser)
- {
- score++;
- printf("Corrent!\nLives: %i Score: %i\n\n", lives, score);
- }
- if (user_awnser != real_awnser)
- {
- lives = lives -1;
- printf("Not corrent.\nLives: %i Score: %i\n\n", lives, score);
- }
- }
- void log_game()
- {
- time_t t;
- time(&t);
- FILE *file;
- file = fopen("score.log", "a+");
- fprintf(file, "Score: %i Game ended at: %s", score, ctime(&t));
- fclose(file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement