Information Technology - Computer Programming - Source Code - Homebrew - Open Source - Software - Hardware - 8 bit - 16 bit - 32 bit - 64 bit - x86 - x64 - DOS - Windows - Linux - Arduino - Embedded - Development - Retro - Vintage - Math - Science - History - Hobby - Beginners - Professionals - Experiment - Research - Study - Fun - Games

ATTENTION NEW USERS: Due to bot traffic, we are forced to manually approve registrations. We get thousands of bots trying to register, causing us to delete registrations in bulk with little ability to filter what is real or not. If you're having trouble getting approved, then send an email to ptrworkmails@gmail.com explaining that you are a real user. Use the same email you're trying to register with. Thank you.

Mastermind for Atari ST (Pure C)

Anything C/C++ related.
Post Reply
User avatar
Beronica
Posts: 16
Joined: Wed Feb 22, 2023 8:19 am

Mastermind for Atari ST (Pure C)

Post by Beronica »

This is my first program for the Atari ST and in Pure C.

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

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

Source code preview:

Code: Select all

/* Mastermind by Beronica Smothers */
/* Lucid Apogee 2024 */
/* www.lucidapogee.com */
 
/* Compile with Pure C on Atari ST */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <string.h>
#include <ext.h>

void game(int mode);
void easy(void);
void standard(void);
void help(void);
float rnd(void);
void cls(void);

char guess[6];
char answer[5];

void main(void){

	int selection = 0;

	srand(time(NULL));

	do {

		cls();

		puts("- - - Mastermind - - -");
		puts("");
		puts("[1] Easy");
		puts("[2] Standard");
		puts("[3] Help");
		puts("[4] Exit");

		selection = getch();

		switch (selection){

			case 49:

				game(1);
				break;

			case 50:

				game(2);
				break;

			case 51:

				help();
				break;

			case 52:

				return;

		}

	}while (selection);
	
	return;

}

void game(int mode){

	int i = 0;
	int j = 0;
	int k = 0;
	int correct = 0;
	int wrong = 0;

	char rndstr[2];

	clock_t timestart;
	clock_t timeend;
	int timetotal = 0;

	cls();

	strcpy(answer, "");

	for (i = 0; i < 4; i++){

		sprintf(rndstr, "%d", (int) (rnd() * 9));
		strcat(answer, rndstr);

	}

	puts("Okay, I've picked a 4 digit code. Can you guess it?\n");

	timestart = clock();

	for (i = 0; i < 10; i++){

		printf("\nGuess # %d: ", i + 1);
		gets(guess);

		if (strcmp(guess, answer) == 0){

			timeend = clock();
			timetotal = (int) (timeend - timestart) / CLK_TCK;

			printf("\nYou cracked the code in %d seconds!\n\n", timetotal);
			puts("Press any key to continue...");
			getch();
			return;

		}

		if (strcmpi(guess, "exit") == 0){return;}

		if (mode == 1){

			easy();

		}else if (mode == 2){

			standard();

		}

	}

	printf("\nOut of tries! The code was %s. Try harder, ok?\n\n", answer);
	puts("Press any key to continue...");
	getch();	
	return;

}

void easy(void){

	int j = 0;
	int k = 0;
	int wrong = 0;

	for (j = 0; j < 4; j++){

		if (guess[j] == answer[j]){

			printf("Y ");

		}else{

			wrong = 0;

			for (k = 0; k < 4; k++){

				if (guess[j] == answer[k]){wrong++;}

			}

			if (wrong > 0){

				printf("N ");

			}else if (wrong == 0){

				printf("X ");

			}

		}

	}

	puts("");

	return;

}

void standard(void){

	int j = 0;
	int k = 0;
	int correct = 0;
	int wrong = 0;

	for (j = 0; j < 4; j++){

		if (guess[j] == answer[j]){

			correct++;

		}else{

			for (k = 0; k < 4; k++){

				if (guess[k] == answer[j]){wrong++;}

			}

		}

	}

	printf("Correct: %d Wrong: %d\n", correct, wrong);

	return;

}

void help(void){

	cls();

	puts("I will pick a 4 digit code. Try to guess it in 10 tries or less.\n");
	puts("Each digit must be between 0 to 9. The code may have repeating digits.\n");
	puts("If on EASY mode, I will tell you if each digit was in the correct place [Y].");
	puts("the wrong place [N], or does not exist in the code [X].\n");
	puts("If on STANDARD mode, I will tell you how many are in the correct place");
	puts("and how many are in the wrong place.\n");
	puts("Enter EXIT to exit the game at any time.\n");
	puts("Good luck!\n");
	puts("Press any key to continue...");
	
	getch();
	return;

}

float rnd(void){

	float rndnum;

	rndnum = (float) rand() / RAND_MAX;

	while (rndnum == 1){

		rndnum = (float) rand() / RAND_MAX;

	}

	return rndnum;

}

void cls(void){

	puts("\33E");
	return;

}
Post Reply