-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.c
181 lines (152 loc) · 4.75 KB
/
wordle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// each of our text files contains 1000 words
#define LISTSIZE 1000
// values for colors and score (EXACT == right letter, right place; CLOSE ==
// right letter, wrong place; WRONG == wrong letter)
#define EXACT 2
#define CLOSE 1
#define WRONG 0
// ANSI color codes for boxed in letters
#define GREEN "\e[38;2;255;255;255;1m\e[48;2;106;170;100;1m"
#define YELLOW "\e[38;2;255;255;255;1m\e[48;2;201;180;88;1m"
#define RED "\e[38;2;255;255;255;1m\e[48;2;220;20;60;1m"
#define RESET "\e[0;39m"
// user-defined function prototypes
string get_guess(int wordsize);
int check_word(string guess, int wordsize, int status[], string choice);
void print_word(string guess, int wordsize, int status[]);
int main(int argc, string argv[]) {
// ensure proper usage
// TODO #1
if (argc != 2) {
printf("Usage: ./wordle wordsize\n");
return 1;
}
int wordsize = 0;
// ensure argv[1] is either 5, 6, 7, or 8 and store that value in wordsize
// instead
// TODO #2
wordsize = atoi(argv[1]);
if (wordsize > 8 || wordsize < 5) {
printf("Error: wordsize must be either 5, 6, 7, or 8\n");
return 1;
}
// open correct file, each file has exactly LISTSIZE words
char wl_filename[6];
sprintf(wl_filename, "wordle_data/%i.txt", wordsize);
FILE *wordlist = fopen(wl_filename, "r");
if (wordlist == NULL) {
printf("Error opening file %s.\n", wl_filename);
return 1;
}
// load word file into an array of size LISTSIZE
char options[LISTSIZE][wordsize + 1];
for (int i = 0; i < LISTSIZE; i++) {
fscanf(wordlist, "%s", options[i]);
}
// pseudorandomly select a word for this game
srand(time(NULL));
string choice = options[rand() % LISTSIZE];
// allow one more guess than the length of the word
int guesses = wordsize + 1;
bool won = false;
// print greeting, using ANSI color codes to demonstrate
printf(GREEN "This is WORDLE50" RESET "\n");
printf("You have %i tries to guess the %i-letter word I'm thinking of\n",
guesses, wordsize);
// main game loop, one iteration for each guess
for (int i = 0; i < guesses; i++) {
// obtain user's guess
string guess = get_guess(wordsize);
// array to hold guess status, initially set to zero
int status[wordsize];
// set all elements of status array initially to 0, aka WRONG
// TODO #4
for (int j = 0; j < wordsize; j++) {
status[j] = 0;
}
// Calculate score for the guess
int score = check_word(guess, wordsize, status, choice);
printf("Guess %i: ", i + 1);
// Print the guess
print_word(guess, wordsize, status);
// if they guessed it exactly right, set terminate loop
if (score == EXACT * wordsize) {
won = true;
break;
}
}
// Print the game's result
// TODO #7
if (won == true) {
printf("You won!\n");
} else {
printf("The target word was: %s\n", choice);
}
// that's all folks!
return 0;
}
string get_guess(int wordsize) {
string guess = "";
// ensure users actually provide a guess that is the correct length
// TODO #3
while (strlen(guess) != wordsize) {
guess = get_string("Input a %i-letter word: ", wordsize);
}
return guess;
}
int check_word(string guess, int wordsize, int status[], string choice) {
int score = 0;
// compare guess to choice and score points as appropriate, storing points in
// status
// TODO #5
for (int i = 0; i < wordsize; i++) {
for (int j = 0; j < wordsize; j++) {
if (guess[i] == choice[j]) {
if (i == j) {
status[i] = EXACT;
score += EXACT;
break;
} else {
status[i] = CLOSE;
score += CLOSE;
}
}
}
}
// HINTS
// iterate over each letter of the guess
// iterate over each letter of the choice
// compare the current guess letter to the current choice letter
// if they're the same position in the word, score EXACT points
// (green) and break so you don't compare that letter further if
// it's in the word, but not the right spot, score CLOSE point
// (yellow)
// keep track of the total score by adding each individual letter's score
// from above
return score;
}
void print_word(string guess, int wordsize, int status[]) {
// print word character-for-character with correct color coding, then reset
// terminal font to normal
// TODO #6
for (int i = 0; i < wordsize; i++) {
switch (status[i]) {
case EXACT:
printf(GREEN "%c" RESET, guess[i]);
break;
case CLOSE:
printf(YELLOW "%c" RESET, guess[i]);
break;
default:
printf(RED "%c" RESET, guess[i]);
}
}
printf("\n");
return;
}