// Program to practice alphabet words used
// in communication with ATC.
// KuchiKuu
// 2022-08-24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char CHAR_MAX = 15*3+1;
// #define CHAR_MAX 46 <-- const or define?
// which one would be "better"?

int main(void){
	srandom(1);
	char *alphabet[26] = {	"alpha",
				"beta",
				"charlie",
				"delta",
				"echo",
				"foxtrot",
				"golf",
				"hotel",
				"india",
				"juliet",
				"kilo",
				"lima",
				"mike",
				"november",
				"oscar",
				"papa",
				"quebeck",
				"romeo",
				"sierra",
				"tango",
				"uniform",
				"victor",
				"whiskey",
				"x-ray",
				"yankee",
				"zulu"};
	unsigned int correct = 0;
	unsigned int incorrect = 0;

	char exit_state = 1; // 0 = break while loop
	char *exit_string = "exit\n";

	while(exit_state){
		printf("Correct: %i\tIncorrect: %i\n",correct,incorrect);
		char firstletter[15];
		char secondletter[15];
		char thirdletter[15];
		long int number = random()%26;

		strcpy(firstletter,alphabet[number]);
		number = random()%26;
		strcpy(secondletter,alphabet[number]);
		number = random()%26;
		strcpy(thirdletter,alphabet[number]);

		char all_letters[CHAR_MAX]; 	// = (char *) malloc(1 + 15 + 15 + 15);
		strcpy(all_letters,firstletter);
		strcat(all_letters," ");
		strcat(all_letters,secondletter);
		strcat(all_letters," ");
		strcat(all_letters,thirdletter);
		strcat(all_letters,"\n"); 	// required to match fgets (man 3 fgets)

//		printf("Example: %s",all_letters);
		printf("Type: %c%c%c :",firstletter[0],secondletter[0],thirdletter[0]);
		char input[40]; 		// = (char *) malloc(1+15+15+15);
		fgets((char *) &input,34,stdin);
			// very long input will trigger this loop
			// multiple times
//		printf("You typed: %s\n",input);
		if(strcmp(exit_string,input) == 0){
			exit_state = 0;
		}else if(strcmp(all_letters,input) == 0){
			printf("CORRECT!\n\n");
			correct++;
		}else{
			printf("INCORRECT!\tShould be %s\n",all_letters);
			incorrect++;
		};
	}; // end of while loop
	return 0;
}