This is my first full program I've written in C. It took me about 2 hours to pull together today. Looking for any sort of feedback on the code, formatting, cleanliness etc. I didn't follow any guides or tutorials, but did have some existing C++ knowledge and knowledge in other languages.
The game is Mastermind. The way it works is that the computer generates a 4 color code randomly from a pool of 7 colors. Then, the user must guess what the code is in as few guesses as possible. The user must guess 4 colors, and the feedback given is how many colors of the guess are a part of the code, and how many of the colors are placed correctly.
There's more on the Wikipedia page: https://en.m.wikipedia.org/wiki/Mastermind_(board_game)
Thanks
#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 "\x1B[31m" BLOCK RESET
#define GRN "\x1B[32m" BLOCK RESET
#define YEL "\x1B[33m" BLOCK RESET
#define BLU "\x1B[34m" BLOCK RESET
#define MAG "\x1B[35m" BLOCK RESET
#define CYN "\x1B[36m" BLOCK RESET
#define WHT "\x1B[37m" BLOCK RESET
#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 COLORS 6
#define LENGTH 4
enum Colors {
red,
green,
yellow,
blue,
magenta,
cyan,
white
};
static const enum Colors Color_map[] = {red, green, yellow, blue, magenta, cyan, white};
void *generate_colors(enum Colors *buffer)
{
int power = pow(COLORS + 1, LENGTH);
int colors_integer = rand() % power;
for (int i = 0; i < LENGTH; ++i)
{
int remainder = colors_integer % (COLORS + 1);
int divisor = colors_integer / (COLORS + 1);
buffer[i] = Color_map[remainder];
colors_integer = divisor;
}
}
int convert_input(char *input, enum Colors *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;
}
char *color_to_char(enum Colors 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 Colors selected_colors[4];
generate_colors(selected_colors);
int guessed = 0;
do {
char input[LENGTH];
scanf("%s", input);
if (strlen(input) == LENGTH)
{
enum Colors 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 Colors color = converted[i];
enum Colors actual = selected_colors[i];
if (color == actual)
++correct_place;
else
{
for (int j = 0; j < LENGTH; ++j)
{
if (j != i)
{
enum Colors 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");
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");
}
} while (!guessed);
return 0;
}
RRRR
it can state:2 correct color 2 correct place and color
. How to I swap myR
's to the correct order :-) If the hidden code only has twoR
it should report0 correct color 2 correct place and color
. \$\endgroup\$