2025-05-22 00:47:42 +10:00
// Made with GNU/Linux
# include <stdio.h>
# include <stdbool.h>
2025-05-22 16:15:47 +10:00
# include <time.h>
# include <stdlib.h>
2025-05-24 12:04:12 +10:00
# include <string.h>
2025-06-13 08:25:06 +10:00
# include <math.h>
2025-06-17 22:31:03 +10:00
int isvalidnumber ( char entered [ ] ) {
int valid = 1 ;
for ( int i = 0 ; i < strlen ( entered ) ; i + + ) {
switch ( entered [ i ] ) {
case ' 0 ' :
case ' 1 ' :
case ' 2 ' :
case ' 3 ' :
case ' 4 ' :
case ' 5 ' :
case ' 6 ' :
case ' 7 ' :
case ' 8 ' :
case ' 9 ' :
case ' . ' :
case ' - ' :
break ;
default :
valid = 0 ;
break ;
}
}
return valid ;
}
2025-05-22 00:47:42 +10:00
int main ( void ) {
2025-06-17 22:31:03 +10:00
2025-05-22 16:15:47 +10:00
srand ( time ( NULL ) ) ;
2025-06-13 04:19:15 +10:00
char * username = getenv ( " USER " ) ;
printf ( " Hello %s! \n This is a game written for AgnoxGD. \n Would you like to see a tutorial \n Type 1 for no, or 0 for yes. \n " , username ) ;
2025-05-22 00:47:42 +10:00
int tempInput ;
bool tutorial ;
if ( scanf ( " %d " , & tempInput ) = = 1 ) {
tutorial = tempInput ; }
if ( tutorial ) {
printf ( " OK! Lets continue. \n " ) ;
} else {
2025-05-22 17:04:43 +10:00
printf ( " Welcome to Agg! This game tests your math skills against the clock. \n \n How to Play: \n \n 1. Set Your Playtime: First, you'll decide how long you want to play by entering a duration in seconds. \n 2. Solve Math Problems: Once the game starts, you'll be presented with a series of random math problems: addition, division, subtraction, and multiplication. Your goal is to solve as many as you can before time runs out. \n 3. Enter Your Answer: After each problem, type your answer and press Enter. The game will immediately tell you if you're correct or incorrect. \n 4. Rack Up Points: For every correct answer, you'll earn a point. Your score will be tallied at the end. \n 5. Time's Up! The game ends automatically when your chosen playtime runs out. Good luck, and have fun! \n " ) ; }
2025-05-22 16:15:47 +10:00
2025-06-13 04:19:15 +10:00
int accuracytype ;
printf ( " Would you like your accuracy as a float or as an int? Type 0 for float or 1 for int. \n " ) ;
scanf ( " %d " , & accuracytype ) ;
2025-05-22 16:59:23 +10:00
int playtime ;
2025-05-22 17:04:43 +10:00
printf ( " \n How long would you like to play for? Answer in seconds. \n " ) ;
2025-05-22 16:59:23 +10:00
scanf ( " %d " , & playtime ) ;
2025-05-22 17:38:22 +10:00
int number_length ;
printf ( " Please enter maximum possible number \n " ) ;
scanf ( " %d " , & number_length ) ;
2025-05-22 16:59:23 +10:00
time_t unix_time ;
unix_time = time ( NULL ) ;
int end_time = unix_time + playtime ;
int score = 0 ;
2025-06-13 04:19:15 +10:00
int questioncount = 0 ;
2025-05-22 16:59:23 +10:00
while ( unix_time < end_time ) {
2025-05-22 17:38:22 +10:00
float num1 = rand ( ) % number_length + 1 ;
float num2 = rand ( ) % number_length + 1 ;
2025-05-22 16:59:23 +10:00
int type = rand ( ) % 4 + 1 ;
float result ;
float answer ;
2025-05-24 12:04:12 +10:00
char entered [ 20 ] ;
2025-05-22 16:59:23 +10:00
// DEBUG
2025-05-22 17:08:32 +10:00
//printf("%g, %g and %d\n", num1, num2, type);
2025-05-22 16:59:23 +10:00
if ( type = = 1 ) {
printf ( " Add %g and %g \n " , num1 , num2 ) ;
result = num1 + num2 ;
} else if ( type = = 2 ) {
printf ( " Divide %g by %g \n " , num1 , num2 ) ;
result = num1 / num2 ;
} else if ( type = = 3 ) {
printf ( " Subtract %g from %g \n " , num2 , num1 ) ;
result = num1 - num2 ;
} else if ( type = = 4 ) {
printf ( " Multiply %g by %g \n " , num1 , num2 ) ;
result = num1 * num2 ;
}
2025-06-13 04:19:15 +10:00
unix_time = time ( NULL ) ;
questioncount + + ;
2025-05-22 16:59:23 +10:00
2025-05-24 12:04:12 +10:00
scanf ( " %s " , entered ) ;
2025-06-17 22:31:03 +10:00
if ( isvalidnumber ( entered ) ) {
2025-05-24 12:04:12 +10:00
sscanf ( entered , " %g " , & answer ) ;
} else {
printf ( " Error! An invalid character was entered. \n " ) ;
exit ( 1 ) ;
}
2025-05-22 16:59:23 +10:00
if ( result = = answer ) {
printf ( " Correct! \n " ) ;
score + + ;
} else {
2025-05-24 12:14:54 +10:00
printf ( " Incorrect! The correct answer is %g. \n " , result ) ;
2025-05-22 16:59:23 +10:00
}
// DEBUG
2025-05-22 17:08:32 +10:00
// printf ("Result is %g\n", result);
2025-05-22 16:15:47 +10:00
}
2025-06-13 04:19:15 +10:00
float accuracy ;
2025-06-13 08:25:06 +10:00
accuracy = ( float ) score / questioncount * 100 ;
2025-05-22 16:59:23 +10:00
2025-06-13 04:19:15 +10:00
printf ( " Your score is %d! \n " , score ) ;
if ( score > = 1 ) {
printf ( " Great job, %s! \n " , username ) ;
} else {
printf ( " Try again!, %s. \n " , username ) ;
}
if ( accuracytype = = 1 ) {
2025-06-13 08:27:21 +10:00
printf ( " You got an accuracy of %d%%! \n " , ( int ) accuracy ) ;
2025-06-13 04:19:15 +10:00
} else {
2025-06-13 08:27:21 +10:00
printf ( " You got an accuracy of %g%%! \n " , accuracy ) ;
2025-06-13 04:19:15 +10:00
}
2025-05-22 00:47:42 +10:00
return 0 ;
}