Last active
May 26, 2022 13:12
-
-
Save lolarobins/854f7de0bb8ecf63c1ca7385e91ba95c to your computer and use it in GitHub Desktop.
Simple wordle C program for ICS4U
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 <ctype.h> | |
#include <time.h> | |
#include <stdlib.h> | |
typedef char *string; | |
typedef enum boolean | |
{ | |
false, | |
true | |
} boolean; | |
typedef enum Color | |
{ | |
REGULAR, | |
GRAY, | |
YELLOW, | |
GREEN | |
} Color; | |
string word; | |
char guesses[6][6]; | |
int stage = 0; | |
string getColor(Color color) | |
{ | |
switch (color) | |
{ | |
case REGULAR: | |
return "\033[0m"; | |
case GRAY: | |
return "\033[1;30m"; | |
case YELLOW: | |
return "\033[1;33m"; | |
case GREEN: | |
return "\033[1;32m"; | |
} | |
} | |
boolean contains(char character) | |
{ | |
for (int i = 0; i < 5; i++) | |
if (word[i] == character) | |
return true; | |
return false; | |
} | |
boolean win() { | |
for (int i = 0; i < 5; i++) | |
if (word[i] != guesses[stage][i]) | |
return false; | |
return true; | |
} | |
void randomWord() { | |
srand(time(NULL)); | |
int random = rand() % 5; | |
switch(random) { | |
case 0: | |
word = "THROW"; | |
break; | |
case 1: | |
word = "LOVED"; | |
break; | |
case 2: | |
word = "TREAT"; | |
break; | |
case 3: | |
word = "REPOS"; | |
break; | |
case 4: | |
word = "BLADE"; | |
break; | |
} | |
} | |
void convertUpper() | |
{ | |
for (int i = 0; i < 5; i++) | |
guesses[stage][i] = toupper(guesses[stage][i]); | |
} | |
void printChart() | |
{ | |
printf("\n\nStage %d/6\n", stage + 1); | |
for (int i = 0; i < stage + 1; i++) | |
{ | |
for (int j = 0; j < 5; j++) | |
{ | |
string color = getColor(GRAY); | |
if (word[j] == guesses[i][j]) | |
color = getColor(GREEN); | |
else if (contains(guesses[i][j])) | |
color = getColor(YELLOW); | |
printf("%s%c ", color, guesses[i][j]); | |
} | |
printf("%s\n", getColor(REGULAR)); | |
} | |
} | |
int main() | |
{ | |
randomWord(); | |
printf("\nWelcome to %sWO%sR%sD%sLE%s!\n", getColor(GREEN), getColor(YELLOW), getColor(GRAY), getColor(GREEN), getColor(REGULAR)); | |
for (; stage < 6; stage++) | |
{ | |
printf("\nPlease enter a 5 letter word: "); | |
scanf("%s", guesses[stage]); | |
convertUpper(); | |
printChart(); | |
if (win()) { | |
printf("\nYou guessed the correct word in %d rounds. Good job!\n", stage + 1); | |
return 0; | |
} | |
} | |
printf("\nYou were unable to guess the correct word.\nThe correct word was %s%s%s. Feel free to play again!\n", getColor(GREEN), word, getColor(REGULAR)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment