Page 1 of 1

Math Quiz for Atari ST (Pure C)

Posted: Mon Aug 26, 2024 11:36 pm
by admin
Now that we're starting to mess around with Pure C programming on the Atari ST, it felt like a good idea to make a few little games for the platform. Nothing serious, just some examples. For starters, I have this Math Quiz game taken from my Express BASIC example program.

Download it here: http://www.lucidapogee.com/download.php ... THQUIZ.zip

The download comes with a .PRG executable and .C source.

Source code preview:

Code: Select all

#include <stdio.h>
/* Math Quiz by Gemino Smothers */
/* Lucid Apogee 2024 */
/* www.lucidapogee.com */

/* Compile with Pure C on Atari ST */

#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <time.h>

char operators[] = "+-*/";
char response;

float rnum = 0;
int difficulty = 0;
int operand1 = 0;
int operand2 = 0;
int operation = 0;
int answer = 0;
int answered = 0;
int incorrect = 0;
int attempt = 0;

void game(void);
void generateproblem(void);
void displayproblem(void);
void checkattempt(void);
void checkscore(void);
void printscore(void);
void r(void);

void main(void){

	srand(time(NULL));
	puts("\33E");

	printf("Math Quiz by Gemino Smothers 2024\n\n");
	printf("Solve 100 addition, subtraction, multiplication, and division problems.\n");
	printf("For division operations, calculate the largest whole quotient.\n\n");

	do{

		printf("Start game (y/n)? ");
		response = getch();
		puts("\33E");

		if (response == 'y' || response == 'Y'){

			game();

		}

	}while (response != 'n' && response != 'N');

	return;

}

void game(void){

	do{

		generateproblem();
		displayproblem();
		checkattempt();
		checkscore();

	}while (response != 'n' && response != 'N');

	printscore();
	response = 0;

	return;

}

void generateproblem(void){

	answered++;
	difficulty += 2;

	r();
	operation = (int) (rnum * 4);

	r();
	operand1 = (int) (rnum * difficulty);

	do{

		r();
		operand2 = (int) (rnum * difficulty);

	}while (operation == 3 && operand2 == 0);

	switch (operation){

		case 0:

			answer = operand1 + operand2;
			break;

		case 1:

			answer = operand1 - operand2;
			break;

		case 2:

			answer = operand1 * operand2;
			break;

		case 3:

			answer = (int) (operand1 / operand2);

	}

}

void displayproblem(void){

	printf("Problem %d: %d %c %d = ", answered, operand1, operators[operation], operand2);
	scanf("%d", &attempt);
	printf("\n\n");

	return;

}

void checkattempt(void){

	while (attempt != answer){

		incorrect++;

		if (attempt < answer){

			printf("Too low.\n\n");

		}

		if (attempt > answer){

			printf("Too high.\n\n");

		}

		displayproblem();

	}

	printf("Correct!\n\n");

	return;

}

void checkscore(void){

	if (answered < 100 && incorrect < 100){

		printf("Continue (y/n)? ");
		response = getch();
		puts("\33E");

	}else{

		response = 'n';

	}

	return;

}

void printscore(void){

	printf("Questions answered: %d / 100\n", answered);
	printf("Incorrect attempts: %d\n\n", incorrect);

	if (incorrect >= 25){

		printf("That's a lot of wrong answers. You need to practice more!\n\n");

	}

	if (answered == 100){

		printf("You answered all of the math problems. Good work!\n\n");

	}

	if (answered < 100){

		printf("You're going to leave all these problems unsolved? Try again later!\n\n");

	}

	difficulty = 0;
	answered = 0;
	incorrect = 0;

	return;

}

void r(void){

	rnum = (float) rand() / RAND_MAX;

	while (rnum == 1){

		rnum = (float) rand() / RAND_MAX;

	}

	return;	

}