Last active
March 24, 2019 11:56
-
-
Save JellyWX/48cad31598e5532508a132bf67eb91db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <math.h> | |
#include <string.h> | |
#include <ctype.h> | |
#define RESET "\x1B[0m" | |
#define BLOCK "█" | |
#define RED_C "\x1B[31m" | |
#define GRN_C "\x1B[32m" | |
#define YEL_C "\x1B[33m" | |
#define BLU_C "\x1B[34m" | |
#define MAG_C "\x1B[35m" | |
#define CYN_C "\x1B[36m" | |
#define WHT_C "\x1B[37m" | |
#define RED RED_C BLOCK RESET | |
#define GRN GRN_C BLOCK RESET | |
#define YEL YEL_C BLOCK RESET | |
#define BLU BLU_C BLOCK RESET | |
#define MAG MAG_C BLOCK RESET | |
#define CYN CYN_C BLOCK RESET | |
#define WHT WHT_C BLOCK RESET | |
#define LENGTH 4 | |
enum Color { | |
red, | |
green, | |
yellow, | |
blue, | |
magenta, | |
cyan, | |
white, | |
COLORS | |
}; | |
void generate_colors(enum Color *buffer) | |
{ | |
int power = pow(COLORS, LENGTH); | |
int colors_integer = rand() % power; | |
for (int i = 0; i < LENGTH; ++i) | |
{ | |
int remainder = colors_integer % (COLORS); | |
int divisor = colors_integer / (COLORS); | |
buffer[i] = (enum Color)remainder; | |
colors_integer = divisor; | |
} | |
} | |
int convert_input(char *input, enum Color *buffer) | |
{ | |
for (int c = 0; c < strlen(input); ++c) | |
{ | |
char character = tolower(input[c]); | |
switch (character) | |
{ | |
case 'r': | |
buffer[c] = red; | |
break; | |
case 'g': | |
buffer[c] = green; | |
break; | |
case 'y': | |
buffer[c] = yellow; | |
break; | |
case 'b': | |
buffer[c] = blue; | |
break; | |
case 'm': | |
buffer[c] = magenta; | |
break; | |
case 'c': | |
buffer[c] = cyan; | |
break; | |
case 'w': | |
buffer[c] = white; | |
break; | |
default: | |
return 1; | |
} | |
} | |
return 0; | |
} | |
const char *color_to_char(enum Color color) | |
{ | |
switch (color) | |
{ | |
case red: | |
return RED; | |
case green: | |
return GRN; | |
case yellow: | |
return YEL; | |
case blue: | |
return BLU; | |
case magenta: | |
return MAG; | |
case cyan: | |
return CYN; | |
default: | |
return WHT; | |
} | |
} | |
int main() | |
{ | |
srand(time(NULL)); | |
enum Color selected_colors[4]; | |
generate_colors(selected_colors); | |
char* line = NULL; | |
size_t line_len = 0u; | |
while (getline(&line, &line_len, stdin) != -1) | |
{ | |
char input[LENGTH + 1]; | |
sscanf(line, "%4s", input); | |
if (strlen(input) == LENGTH) | |
{ | |
enum Color converted[LENGTH]; | |
int contains_unmatched = convert_input(input, converted); | |
int correct_place = 0; | |
int correct_color = 0; | |
if (contains_unmatched) | |
{ | |
printf("Please only choose characters from " RED_C "R, " GRN_C "G, " YEL_C "Y, " BLU_C "B, " MAG_C "M, " CYN_C "C, " WHT_C "W" RESET ".\n"); | |
} | |
else | |
{ | |
printf("You guessed: "); | |
for (int i = 0; i < LENGTH; ++i) | |
{ | |
enum Color color = converted[i]; | |
enum Color actual = selected_colors[i]; | |
if (color == actual) | |
++correct_place; | |
else | |
{ | |
for (int j = 0; j < LENGTH; ++j) | |
{ | |
if (j != i) | |
{ | |
enum Color current = selected_colors[j]; | |
if (color == current) | |
{ | |
++correct_color; | |
break; | |
} | |
} | |
} | |
} | |
printf("%s", color_to_char(converted[i])); | |
} | |
if (correct_place == LENGTH) | |
{ | |
printf("Well done! You got it right. Goodbye\n"); | |
return 0; | |
} | |
else | |
{ | |
printf("\n %d correct color\n %d correct place and color\n", correct_color, correct_place); | |
} | |
} | |
} | |
else | |
{ | |
printf("Please enter 4 characters.\n"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment