Code:
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#define title "MathTest"
#define quests 9 // questions in one game\set
#define miss 6 // wrong answers allowed before gameover
//Addition test params (number range):
#define add_min 1
#define add_max 9
//Subtraction test params (number range):
#define sub_min 1
#define sub_max 9
//Multiplication test params (number range):
#define mul_min 2
#define mul_max 9
//Division test params (number range):
#define div_min 2
#define div_max 9
char uname[16]; // users name
int key = -1; // for main menu, keyboard input
int one, two, sol; // variables for test
int i, q;
int tries; // tries left (to give right answer)
//statistics:
int st_right, st_wrong, st_total, st_questions;
int sl_right, sl_wrong, sl_total;
bool solved, playing; // booleans
int random(int min, int max) //returns random in range min..max
{
return rand() % (max - min + 1) + min;
}
int random(int max) //returns a random in range 1..max
{
return rand() % max + 1;
}
void comment(char con) // returns a random comment\taunt for a specified situation
{
if (con == '~') // wrong answer
{
switch (random(3))
{
case 1: printf("Wrong answer (>_<)"); break;
case 2: printf("You should try again."); break;
case 3: printf("Be carefull."); break;
}
}
if (con == '@') // lost
{
switch (random(3))
{
case 1: printf("You lost the game (;_;)"); break;
case 2: printf("Game over."); break;
case 3: printf("How could you let this happen? (T_T)"); break;
}
}
if (con == '!') // right answer
{
printf("Right!");
// todo: add a switch statement here
}
if (con == 'v')
{
printf("Victory!");
// todo: add a switch statement here
}
if (con == '<') // lower!
{
printf("Try smaller number.");
// todo: add a switch statement here
}
if (con == '>') // higher!
{
printf("Try higher number.");
// todo: add a switch statement here
}
if (con == '+') // just a bit higher
{
printf("A bit higher...");
// todo: add a switch statement here
}
if (con == '-') // just a bit lower
{
printf("A bit lower...");
// todo: add a switch statement here
}
}
void stats() // displays global statistics
{
printf("\n\t***Game statistics***\n");
printf("\tTotal questions: %d\n", st_questions);
printf("\tTotal answers: %d\n", st_total);
printf("\tRight answers: %d\n", st_right);
printf("\tWrong answers: %d\n", st_wrong);
if (st_total != 0)printf("\tAccuracy: %d%c\n", st_right * 100 / st_total, '%');
}
void stats_local() // displays local statistics
{
printf("\n\t***Round statistics***\n");
printf("\tRound result: ");
if (playing) printf("Victory\n"); else printf("Defeat\n");
printf("\tTotal questions: %d\n", q);
printf("\tTotal answers: %d\n", sl_total);
printf("\tRight answers: %d\n", sl_right);
printf("\tWrong answers: %d\n", sl_wrong);
printf("\tAccuracy: %d%c\n", sl_right * 100 / sl_total, '%');
}
void test(char type) // test\game function.
{
sl_right = 0; sl_wrong = 0; sl_total = 0;
playing = true;
for (q = 1; q <= quests && playing; q++)
{
tries = miss;
solved = false;
//Generate math problem
if (type == '+')
{
one = random(add_min, add_max);
two = random(add_min, add_max);
sol = one + two;
printf("\n%d. %d + %d = ?\n", q, one, two);
}
if (type == '-')
{
sol = random(sub_min, sub_max);
two = random(sub_min, sub_max);
one = sol + two;
printf("\n%d. %d - %d = ?\n", q, one, two);
}
if (type == '/')
{
sol = random(div_min, div_max);
two = random(div_min, div_max);
one = sol * two;
printf("\n%d. %d / %d = ?\n", q, one, two);
}
if (type == '*')
{
one = random(mul_min, mul_max);
two = random(mul_min, mul_max);
sol = one * two;
printf("\n\n%d. %d * %d = ?\n", q, one, two);
}
//Answering loop
while (tries > 0 && !solved)
{
//Get the answer from user
printf(" Your answer > "); scanf("%d", &i);
sl_total++;
if (i == sol) // right answer
{
sl_right++;
comment('!');
solved = true;
}
else // wrong answer
{
sl_wrong++;
tries--;
if (tries > 0)
{
// Provide different comments, depending on how near the answer was.
if (i < sol + 4 && i > sol) comment('-');
else if (i > sol - 4 && i < sol) comment('+');
else if (i < sol + 16 && i > sol) comment('<');
else if (i > sol - 16 && i < sol) comment('>');
else comment('~');
printf("\nYou have %d tries remaining!\n", tries);
}
else // no tries\lives remaining - game over
{
solved = true;
playing = false;
comment('@');
printf("\nThe correct answer was... %d!", sol);
}
}
}
}
q--; // decrease q by one to hold actual number of passed questions
st_questions += q;
st_total += sl_total;
st_right += sl_right;
st_wrong += sl_wrong;
if (playing) comment('v');
stats_local();
}
int main()
{
srand(time(NULL)); // set random seed from current time
//Get users name:
printf("What is your name?\n > "); gets(uname);
while (1) // main loop:
{
printf("\n\n");
printf("Welcome, %s, to %s!\n", uname, title);
printf("Please choose one of the following options: \n");
printf(" [1] Addition\n");
printf(" [2] Subtraction\n");
printf(" [3] Division\n");
printf(" [4] Multiplication\n");
printf(" [5] Statistics\n");
printf(" [0] Exit\n");
//Get input:
printf(" > "); scanf("%d", &key);
//Cases:
if (key == 1) test('+');
if (key == 2) test('-');
if (key == 3) test('/');
if (key == 4) test('*');
if (key == 5) stats();
if (key == 0) return 1;
}
return 0;
}