Code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// GLOBAL VARIABLES
//************************************************** ****************************
int num_easy, num_medium, num_hard, num_easy2, num_medium2, num_hard2,
point;
char x, y;
FILE *score;
char menu;
char login[100], name[100], data[100];
//************************************************** ****************************
// PROTOTYPE FUNCTIONS
//************************************************** ****************************
int random_easy(void);
int random_easy2(void);
int random_medium(void);
int random_medium2(void);
int random_hard(void);
int random_hard2(void);
int campur(int num1, int num2);
int tolak(int num1, int num2);
int darab(int num1, int num2);
float bahagi(float num1, float num2);
float round(float f);
void operasi(void);
void start_game_easy(void);
void start_game_medium(void);
void start_game_hard(void);
void menu_level(void);
void operasi(void);
void scoreboard();
void play();
void mainmenu();
void robot(void);
void robot2(void);
void robot3(void);
void robot4(void);
void animation(void);
//************************************************** ****************************
// LOGIN
//************************************************** ****************************
void log()
{
textcolor(RED);
puts("\n\n\n\n");
printf("\t");
cprintf
("************************************************** ************");
printf("\t\t\t\t\t");
cprintf("Welcome to the MATHGAME!");
printf("\n");
printf("\t");
cprintf
("************************************************** ************\n");
puts("\n\n\n\t\t\tPlease enter your name");
printf("\n\t\t\t>> ");
gets(login);
}
//************************************************** ****************************
// MAIN FUNCTION
//************************************************** ****************************
void main()
{
animation();
log();
mainmenu();
}
//************************************************** ****************************
// MAIN MENU
//************************************************** ****************************
void mainmenu()
{
menu:
clrscr();
textcolor(RED);
puts("\n");
printf("\t");
cprintf
("************************************************** ************");
printf("\t\t\t\t\t\t");
cprintf("MAIN MENU");
printf("\n");
printf("\t");
cprintf
("************************************************** ************\n");
printf("\n\t\t\tWelcome %s !", login);
score = fopen("data.txt", "r+");
while (!feof(score)) {
fscanf(score, "%s", &name);
if (strcmp(login, name) == 0) {
fscanf(score, "%d", &point);
printf("\n\t\t\tYour previous point was %d.", point);
break;
}
if (feof(score))
puts("\n\t\t\t<<Welcome, new player!>>");
}
puts("\n\n\n\t\t\t1. START GAME!");
puts("\t\t\t2. Scoreboard");
puts("\t\t\t3. Quit\n");
printf("\n\n\t\t Please enter selection... ");
choose:
menu = getch();
switch (menu) {
case '1':
operasi();
break;
case '2':
scoreboard();
break;
case '3':{
puts("\n\n\n\n\t\t\t<< Game ended >>");
sleep(1);
exit(1);
}
default:
goto choose;
}
getch();
}
//************************************************** ****************************
// RANDOMIZING EASY QUESTIONS
//************************************************** ****************************
int random_easy(void)
{
randomize();
num_easy = rand() % 10;
return (num_easy);
}
int random_easy2(void)
{
randomize();
num_easy2 = random(10);
return (num_easy2);
}
//************************************************** ****************************
// RANDOMIZING MEDIUM QUESTIONS
//************************************************** ****************************
int random_medium(void)
{
randomize();
num_medium = random(100) % 100;
if (num_medium < 10)
num_medium += 10;
return (num_medium);
}
int random_medium2(void)
{
randomize();
num_medium2 = rand() % 100;
if (num_medium2 < 10)
num_medium2 += 10;
return (num_medium2);
}
//************************************************** ****************************
// RANDOMIZING HARD QUESTIONS
//************************************************** ****************************
int random_hard(void)
{
randomize();
num_hard = rand() % 1000;
if (num_hard < 100)
num_hard += 100;
return (num_hard);
}
int random_hard2(void)
{
randomize();
num_hard2 = random(1000);
if (num_hard2 < 100)
num_hard2 += 100;
return (num_hard2);
}
//************************************************** ****************************
// ADDITION
//************************************************** ****************************
int campur(int num1, int num2)
{
int jumlah;
if (x == '1') {
num1 = random_easy();
num2 = random_easy2();
} else if (x == '2') {
num1 = random_medium();
num2 = random_medium2();
} else if (x == '3') {
num1 = random_hard();
num2 = random_hard2();
}
printf(" %d + %d", num1, num2);
jumlah = num1 + num2;
return (jumlah);
}
//************************************************** ****************************
// SUBSTRACTION
//************************************************** ****************************
int tolak(int num1, int num2)
{
int jumlah;
if (x == '1') {
num1 = random_easy();
num2 = random_easy2();
} else if (x == '2') {
num1 = random_medium();
num2 = random_medium2();
} else if (x == '3') {
num1 = random_hard();
num2 = random_hard2();
}
printf(" %d - %d", num1, num2);
jumlah = num1 - num2;
return (jumlah);
}
//************************************************** ****************************
// MULTIPLICATION
//************************************************** ****************************
int darab(int num1, int num2)
{
int jumlah;
if (x == '1') {
num1 = random_easy();
num2 = random_easy2();
} else if (x == '2') {
num1 = random_medium();
num2 = random_medium2();
} else if (x == '3') {
num1 = random_hard();
num2 = random_hard2();
}
printf(" %d x %d", num1, num2);
jumlah = num1 * num2;
return (jumlah);
}
//************************************************** ****************************
// DIVISION
//************************************************** ****************************
float bahagi(float num1, float num2)
{
float jumlah;
if (x == '1') {
num1 = random_easy();
num2 = random_easy2();
} else if (x == '2') {
num1 = random_medium();
num2 = random_medium2();
} else if (x == '3') {
num1 = random_hard();
num2 = random_hard2();
}
printf(" %.0f / %.0f", num1, num2);
jumlah = num1 / num2;
jumlah = round(jumlah * 100) / 100;
return (jumlah);
}
//************************************************** ****************************
// STARTING EASY GAME
//************************************************** ****************************
void start_game_easy(void)
{
clrscr();
int n = 1;
float jumlah_game_easy;
float answer;
while (n <= 10) {
clrscr();
textcolor(RED);
puts("\n");
printf("\t");
cprintf
("************************************************** ************");
printf("\t\t\t\t\t\t");
if (y == '1')
printf("ADD IT UP: ");
else if (y == '2')
printf("MINUS IT: ");
else if (y == '3')
printf("TIMES IT: ");
else if (y == '4')
printf("DIVIDE IT: ");
printf("Peanut!");
printf("\n");
printf("\t");
cprintf
("************************************************** ************\n");
printf("\n\n\tAnswer these 10 questions..");
printf("\n\n\n\tTry question no. %d :\n", n);
puts("\t\t\t\t--------");
printf("\t\t\t\t");
if (y == '1')
jumlah_game_easy = campur(num_easy, num_easy2);
else if (y == '2')
jumlah_game_easy = tolak(num_easy, num_easy2);
else if (y == '3')
jumlah_game_easy = darab(num_easy, num_easy2);
else if (y == '4')
jumlah_game_easy = bahagi(num_easy, num_easy2);
puts("\n\t\t\t\t--------");
if (y == '4')
puts("\n\tHint:Give you answer in two decimal places");
printf("\n\n\tYour answer >> ");
scanf("%f", &answer);
if (answer == jumlah_game_easy) {
printf("\n\n\tWELL DONE!\n\t10 points for you!");
point += 10;
sleep(1);
} else if (answer != jumlah_game_easy) {
printf("\n\tNope, wrong answer!");
sleep(1);
}
n++;
}
printf("\n\n\t<<Your total point is %d>>", point);
score = fopen("data.txt", "r+");
fprintf(score, "%s %d", login, point);
play();
}
//************************************************** ****************************
// STARTING MEDIUM GAME
//************************************************** ****************************
void start_game_medium(void)
{
clrscr();
int n = 1;
float answer;
float jumlah_game_medium;
while (n <= 10) {
clrscr();
textcolor(RED);
puts("\n");
printf("\t");
cprintf
("************************************************** ************");
printf("\t\t\t\t\t\t");
if (y == '1')
printf("ADD IT UP: ");
else if (y == '2')
printf("MINUS IT: ");
else if (y == '3')
printf("TIMES IT: ");
else if (y == '4')
printf("DIVIDE IT: ");
printf("Piece of cake!");
printf("\n");
printf("\t");
cprintf
("************************************************** ************\n");
printf("\n\n\tAnswer these 10 questions..");
printf("\n\n\n\tTry question no. %d :\n", n);
puts("\t\t\t\t---------");
printf("\t\t\t\t");
if (y == '1')
jumlah_game_medium = campur(num_medium, num_medium2);
else if (y == '2')
jumlah_game_medium = tolak(num_medium, num_medium2);
else if (y == '3')
jumlah_game_medium = darab(num_medium, num_medium2);
else if (y == '4')
jumlah_game_medium = bahagi(num_medium, num_medium2);
puts("\n\t\t\t\t---------");
printf("\n\n\tYour answer >> ");
scanf("%f", &answer);
if (answer == jumlah_game_medium) {
printf("\n\n\tWELL DONE!\n\t20 points for you!");
point += 20;
sleep(1);
} else if (answer != jumlah_game_medium) {
printf("\n\tNope, wrong answer!");
sleep(1);
}
n++;
}
printf("\n\n\t<<Your total point is %d>>", point);
score = fopen("data.txt", "r+");
fprintf(score, "%s %d", login, point);
play();
}
//************************************************** ****************************
// STARTING HARD GAME
//************************************************** ****************************
void start_game_hard(void)
{
clrscr();
int n;
float answer, jumlah_game_hard;
for (n = 1; n <= 10; n++) {
clrscr();
textcolor(RED);
puts("\n");
printf("\t");
cprintf
("************************************************** ************");
printf("\t\t\t\t\t\t");
if (y == '1')
printf("ADD IT UP: ");
else if (y == '2')
printf("MINUS IT: ");
else if (y == '3')
printf("TIMES IT: ");
else if (y == '4')
printf("DIVIDE IT: ");
printf("Wow!");
printf("\n");
printf("\t");
cprintf
("************************************************** ************\n");
printf("\n\n\tAnswer these 10 questions..");
printf("\n\n\n\tTry question no. %d :\n", n);
puts("\t\t\t\t-----------");
printf("\t\t\t\t");
if (y == '1')
jumlah_game_hard = campur(num_hard, num_hard2);
else if (y == '2')
jumlah_game_hard = tolak(num_hard, num_hard2);
else if (y == '3')
jumlah_game_hard = darab(num_hard, num_hard2);
else if (y == '4')
jumlah_game_hard = bahagi(num_hard, num_hard2);
puts("\n\t\t\t\t-----------");
printf("\n\n\tYour answer >> ");
scanf("%f", &answer);
if (answer == jumlah_game_hard) {
printf("\n\n\tWELL DONE!\n\t20 points for you!");
point += 20;
sleep(1);
} else if (answer != jumlah_game_hard) {
printf("\n\tNope, wrong answer!");
sleep(1);
}
}
printf("\n\n\t<<Your total point is %d>>", point);
score = fopen("data.txt", "r+");
fprintf(score, "%s %d", login, point);
play();
}
//************************************************** ****************************
// CHOOSE LEVEL
//************************************************** ****************************
void menu_level(void)
{
clrscr();
textcolor(RED);
puts("\n");
printf("\t");
cprintf
("************************************************** ************");
printf("\t\t\t\t\t\t");
cprintf("GAME MENU");
printf("\n");
printf("\t");
cprintf
("************************************************** ************\n");
puts("\n\n\t\t Please select level... ");
puts("\n\n\t\t\t1. Easy");
puts("\t\t\t2. Medium ");
puts("\t\t\t3. Difficult ");
puts("\n\t\t\t4. Back to Main Menu\n");
printf("\t\t\t");
printf("\n\n\t\t Selection:");
repeat:
x = getch();
switch (x) {
case '1':
start_game_easy();
case '2':
start_game_medium();
case '3':
start_game_hard();
case '4':
operasi();
default:
goto repeat;
}
}
//************************************************** ****************************
// CHOOSE OPERATION
//************************************************** ****************************
void operasi(void)
{
clrscr();
repeat:
textcolor(RED);
puts("\n");
printf("\t");
cprintf
("************************************************** ************");
printf("\t\t\t\t\t\t");
cprintf("GAME MENU");
printf("\n");
printf("\t");
cprintf
("************************************************** ************\n");
puts("\n\n\t\t Please select operation... ");
puts("\n\n\t\t\t1. Addition +");
puts("\t\t\t2. Substraction -");
puts("\t\t\t3. Multiplication X");
puts("\t\t\t4. Division /\n");
puts("\t\t\t5. Back to Main Menu");
printf("\n\n\t\t Selection:");
game:
y = getch();
switch (y) {
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
mainmenu();
default:
goto game;
}
menu_level();
}
//************************************************** ****************************
// VIEW SCOREBOARD
//************************************************** ****************************
void scoreboard()
{
char data[100];
clrscr();
textcolor(RED);
puts("\n");
printf("\t");
cprintf
("************************************************** ************");
printf("\t\t\t\t\t\t");
cprintf("SCOREBOARD");
printf("\n");
printf("\t");
cprintf
("************************************************** ************\n");
score = fopen("data.txt", "r+");
if ((score = fopen("data.txt", "r+")) == NULL) {
puts("\n\n\n\n\t\t\tError in reading database..");
puts("\n\n\n\t\t\tReturning to Main Menu..");
sleep(2);
clrscr();
return;
}
puts("\n");
puts("\t\tNAME\t\t\tSCORE\n");
while (!feof(score)) {
fscanf(score, "%s", &data);
printf("\t\t%s", data);
fscanf(score, "%s", &data);
printf("\t\t\t%s\n", data);
}
puts("\n\n\n\n\n\tPress any key to return to Main Menu..");
getch();
clrscr();
mainmenu();
}
//************************************************** ****************************
// PLAY AGAIN?
//************************************************** ****************************
// Query to play again?
void play()
{
char again;
printf("\n\n\tPlay again? (Y/N)");
playagain:
again = getch();
if ((again == 'Y') | (again == 'y'))
operasi();
else if ((again == 'N') | (again == 'n')) {
clrscr();
puts("\n\n\n\n\n\n\n\tThank you for playing!");
puts("\tHave a Nice Day!\n\n");
sleep(2);
exit(1);
} else
goto playagain;
}
//************************************************** ****************************
//ROUNDING UP THE NUMBERS
//************************************************** ****************************
float round(float f)
{
return floor(f + 0.5);
}
//************************************************** ****************************
//animation
//************************************************** ****************************
void animation(void)
{
robot();
sleep(3);
clrscr();
robot2();
sleep(3);
clrscr();
robot3();
sleep(3);
clrscr();
robot4();
clrscr();
}
void robot(void)
{
// snipped due
printf("\n\n");
}
void robot2(void)
{
// to them being
printf("\n\n");
}
void robot3(void)
{
// unrelated to the problems at hand
printf("\n\n");
}
void robot4(void)
{
printf("\n\n");
sleep(2);
printf("\t\t\t Please wait.\n");
printf("\t\t\t\tLoading");
for (int j = 0; j < 2; j++) {
clreol();
sleep(1);
for (int i = 0; i < 3; i++) {
printf(".");
sleep(1);
}
printf("\b\b\b");
}
}