-
Notifications
You must be signed in to change notification settings - Fork 0
/
teordle.c
323 lines (251 loc) · 7.36 KB
/
teordle.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define COLOR_GREEN "\x1B[32m"
#define COLOR_YELLOW "\x1B[33m"
#define COLOR_WHITE "\x1B[37m"
#define COLOR_DEFAULT_TEXT COLOR_WHITE
#define LETTER_SEPERATOR_STR "_ "
#define NULL_CHAR '\0'
#define NEWLINE_STR "\n"
#define NEWLINE_CHAR (char) '\n'
#define WORD_LENGTH 5
#define MAX_GUESSES 6
#define LAST_CHARACTER WORD_LENGTH
#define GUESS_ARRAY_SIZE MAX_GUESSES * WORD_LENGTH
#define NOT_IN_WORD 0
#define IN_WORD_WRONG_INDEX 1
#define IN_WORD_CORRECT_INDEX 2
#define PLAYER_WON true
#define PLAYER_LOST false
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
#define clear_screen() system("clear")
#endif
#if defined(_WIN32) || defined(_WIN64)
#define clear_screen() system("cls")
#endif
#define assert_print(condition, string) \
if (!(condition)) { \
printf("%s", string); \
terminate(1); \
}
#define enter_game_loop() get_input()
void get_input();
void terminate(uint8_t exit_code);
FILE* file_ptr;
size_t words_length = 0;
size_t current_line = 0;
unsigned char user_input[WORD_LENGTH + 1];
unsigned char current_word[WORD_LENGTH + 1];
unsigned char guess_list[GUESS_ARRAY_SIZE];
bool game_over = false;
uint8_t
check_letter_in_word(
unsigned char letter,
size_t guess_list_index)
{
if (current_word[guess_list_index] == letter)
return IN_WORD_CORRECT_INDEX;
for (uint8_t i = 0; i < WORD_LENGTH; ++i)
if (current_word[i] == letter)
return IN_WORD_WRONG_INDEX;
return NOT_IN_WORD;
}
unsigned char*
determine_letter_color(
unsigned char letter,
size_t guess_list_index)
{
switch (check_letter_in_word(
letter, guess_list_index))
{
case IN_WORD_CORRECT_INDEX:
return COLOR_GREEN;
case IN_WORD_WRONG_INDEX:
return COLOR_YELLOW;
default:
return COLOR_DEFAULT_TEXT;
}
}
void
print_letter(
unsigned char letter,
char* color)
{
// prints the letter in the specified color, then sets the
// color back to the default to avoid accidentally printing
// characters in colors they should not be printed in.
printf("%s%c%s ", color, letter, COLOR_DEFAULT_TEXT);
}
void
assign_random_word()
{
start_label:;
unsigned char ch;
if (!file_ptr) {
file_ptr = fopen("words.txt", "r");
// prints a message and exits if file_ptr is NULL;
assert_print(file_ptr, "Unable to find or open words.txt");
// get length of word list
fseek(file_ptr, 0, SEEK_END);
// subtract WORD_LENGTH to avoid starting in the last word
words_length = ftell(file_ptr) - WORD_LENGTH - 1;
}
// really should use a better random function, barely ever see
// any of the words in the last half of the word list
fseek(file_ptr, rand() % words_length, SEEK_SET);
// get a word from the list
// looking for a newline character to determine the start
// of a word is kind of dumb, seeing as we have the
// length of each word as well as the length of the word list.
//
// should be using the power of arithmetic, but unfortunately
// I very consistently got the worst math grades in my class.
while (ch = fgetc(file_ptr)) {
if (ch == NEWLINE_CHAR) {
for (size_t i = 0; i < WORD_LENGTH; ++i)
current_word[i] = toupper(fgetc(file_ptr));
return;
}
}
// EOF reached, try again
goto start_label;
}
void
string_toupper(
unsigned char* str)
{
while (*str)
*(str++) = toupper(*str);
}
bool
is_input_valid(
unsigned char* str)
{
// check the validity of the first 5
for (int i = 0; i < WORD_LENGTH; ++i)
if (!str[i] || !isalpha(str[i])) // yay ascii only!
return false;
// forcing NULL at input[WORD_LENGTH] just made things annoying
// when you accidentally type one letter too many
// or your brain fails to tell you that a certain word has
// six letters and not five, and it gets treated as a
// valid input because it was terminated after five letters.
return !str[LAST_CHARACTER];
}
void
render()
{
clear_screen();
unsigned char* color;
for (size_t i = 0; i < GUESS_ARRAY_SIZE; ++i) {
size_t letter_guess_array_index = i % WORD_LENGTH;
if (letter_guess_array_index == 0 && i != 0)
printf(NEWLINE_STR);
if (guess_list[i])
print_letter(
guess_list[i],
determine_letter_color(guess_list[i], letter_guess_array_index));
else
printf(LETTER_SEPERATOR_STR);
}
printf(NEWLINE_STR);
}
void
end_game(
bool victory)
{
game_over = true;
if (victory)
printf("\nYou win!\n\n");
else
printf("\nYou lose, the word was: %s\n\n", current_word);
printf("PLAY | EXIT\n");
}
void
check_victory()
{
// I'm not into the extra indentation level for a while (true) loop
// so we're getting jiggy with the goto up in here.
start_label:;
bool guessed_correct_word = strncmp(
user_input, current_word, WORD_LENGTH) == 0;
render();
if (guessed_correct_word)
end_game(PLAYER_WON);
if (current_line == MAX_GUESSES && !guessed_correct_word)
end_game(PLAYER_LOST);
get_input();
goto start_label;
}
void
clear_guess_list()
{
for (size_t i = 0; i < GUESS_ARRAY_SIZE; ++i)
guess_list[i] = NULL_CHAR;
}
void
start_game()
{
// current attempt number
current_line = 0;
game_over = false;
clear_screen();
clear_guess_list();
assign_random_word();
check_victory();
}
void
get_input()
{
start_label:;
unsigned char guess[7];
if (fgets(guess, WORD_LENGTH + 2, stdin)) {
guess[strcspn(guess, NEWLINE_STR)] = NULL_CHAR;
string_toupper(guess);
}
if (strlen(guess) > WORD_LENGTH)
goto start_label;
if (strcmp(guess, "PLAY") == 0) start_game();
if (strcmp(guess, "EXIT") == 0) terminate(0);
if (is_input_valid(guess) && !game_over) {
for (int i = 0; i < WORD_LENGTH; ++i) {
user_input[i] = guess[i];
guess_list[(5 * current_line) + i] = guess[i];
}
++current_line;
}
}
int
main()
{
srand(time(NULL));
printf("%s", COLOR_DEFAULT_TEXT);
printf("Teordle - Wordle, but in the terminal\n");
printf("---------------------------------------\n");
printf("PLAY | EXIT\n");
// enter game loop
enter_game_loop();
// should not even be able to reach this,
// the game loop will not end until the user
// enters the 'EXIT' command, at which point
// it will just terminate the program instead of
// breaking the loop and returning here
terminate(0);
}
// safer exit(), ensures the word list file is closed
// it should probably just be loaded into memory or
// re-opened every time a new word is needed.
void
terminate(
uint8_t exit_code)
{
if (file_ptr)
fclose(file_ptr);
exit(exit_code);
}