-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.c
682 lines (607 loc) · 15.2 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// earth
// mound
// sicky
//Used for exit constants mostly
#include <stdlib.h>
//printf and puts
#include <stdio.h>
//string comparison functions
#include <string.h>
//Used to seed the rng for random games
#include <time.h>
#include <string.h>
//Length of a word.
//Note: You cannot change this without changing the word list too.
//You also must adapt the scanf calls for the new length
#define WORD_LENGTH 5
//Number of tries allowed to find the word
#define MAX_TRIES 300
//Number of characters in the alphabet + 1
#define ALPHA_SIZE 27
//If set, the word and a few stats are printed before the game starts
//#define DEBUG
//Very cheap error termination script
#define err(x) fprintf(stderr, EOL "[%s:%i] Fatal error: %s" EOL, __FILE__, __LINE__, x);abort();
//Note: CRLF is also used for telnet.
//If you want to make it available in a BBS you may want to force a CRLF
#ifdef WIN32
#define EOL "\r\n"
#else
#define EOL "\n"
#endif
//Cheap boolean
#define bool int
#define false (0)
#define true (!false)
//Files for lists that contain all words and solutions
FILE * fpAll, * fpSol;
//Number of words in the solution list
long wordCount = 0;
//Selected word from solution list
char word[WORD_LENGTH + 1] = {0};
//Possible characters (used to show unused characters)
//The size specifier is necessary or its value will be readonly
char alpha[ALPHA_SIZE] = "abcdefghijklmnopqrstuvwxyz";
//Memory cache:
//0-25 File position in the complete list with words that start with the given letter a-z
//26: Number of words in the solution list
long memcache[ALPHA_SIZE];
//Reads solution list and picks a random word
long setup(void);
//Pick the given word
int pickWord(char * word, int index);
//Checks if the supplied word is in the list
bool hasWord(const char * word);
//Convert to lowercase
int toLower(char * str);
//Checks the entered word against the solution
bool checkWord(const char * guess);
//Checks if the entered string is a potentially valid word
bool isWord(const char* word);
//Gets the first position of the given char in the given string
int strpos(const char * str, char search);
//Removes characters in the supplied argument from the alphabet
void removeAlpha(const char * guess);
//Runs the main game loop
void gameLoop(void);
//Runs the menu
int menu(void);
//Shows the help text
void help(void);
// Felix here
char impossible_array[26];
unsigned int impossible_current;
char **filterWords(const char *);
//Main function
int main() {
int gameId;
setbuf(stdout, NULL);
//Note: This will search for the file in the current directory
#ifdef ORIGINAL_SRC
fpAll = fopen("LISTS\\ALL.TXT", "r");
fpSol = fopen("LISTS\\SOLUTION.TXT", "r");
#else
fpAll = fopen("ALL.TXT", "r");
fpSol = fopen("SOL.TXT", "r");
#endif /* ORIGINAL_SRC */
if (fpAll == NULL || fpSol == NULL) {
err("error opening wordle lists");
}
#ifdef DEBUG
printf("Word count: %i" EOL, setup());
#else
setup();
#endif
impossible_current = 0;
gameId = menu();
if (gameId >= 0) {
pickWord(word, gameId);
// Felix hacking
#define DEBUG
#ifdef DEBUG
unsigned int i;
for (i = 0; i < 5; i++)
{
if ((((char) ('a' + i)) != word[0]) &&
(((char) ('a' + i)) != word[1]) &&
(((char) ('a' + i)) != word[2]) &&
(((char) ('a' + i)) != word[3]) &&
(((char) ('a' + i)) != word[4]))
{
impossible_array[impossible_current] = (char) ('a' + i);
impossible_current++;
}
}
// printf("Word: %s" EOL, word);
#ifdef LATER
for (i = 0; i < 26; i++)
{
if (alpha[i] == '_')
{
impossible_array[impossible_current] = (char) ('a' + i);
impossible_current++;
}
}
#endif /*LATER */
printf("candidates (after filtering) are \n");
filterWords(impossible_array);
#endif
} else {
return EXIT_SUCCESS;
}
printf("Running game #%i" EOL, gameId + 1);
gameLoop();
fclose(fpAll);
fclose(fpSol);
return EXIT_SUCCESS;
}
int menu() {
char buffer[21];
int gameId = 0;
int scan = 0;
puts(
"Main Menu" EOL
"=========" EOL
"NEW: start a new game." EOL
"LOAD <num>: load a specific game" EOL
"HELP: More information" EOL
"EXIT: End game");
printf("The game number must be in range of 1-%li" EOL, wordCount);
while (true) {
printf("Input: ");
while ((scan = scanf("%20s", buffer)) != 1) {
if (scan == EOF) {
return -1;
}
}
toLower(buffer);
if (strcmp(buffer, "exit") == 0) {
return -1;
} else if (strcmp(buffer, "help") == 0) {
help();
} else if (strcmp(buffer, "new") == 0) {
return rand() % wordCount;
} else if (strcmp(buffer, "load") == 0) {
if (scanf("%i", & gameId) == 1) {
if (gameId > 0 && gameId <= wordCount) {
return gameId - 1;
}
}
puts("Invalid number");
} else {
puts("Invalid input");
}
}
}
void help() {
printf("Wordle is a simple game:" EOL "Guess the %i letter word within %i tries" EOL, WORD_LENGTH, MAX_TRIES);
puts(
"After every guess, hints are shown for each character." EOL
"They look like this:" EOL
" _ = Character not found at all" EOL
" # = Character found and position correct" EOL
" o = Character found but position wrong" EOL
"Unused letters of the alphabet are shown next to the hint" EOL
EOL
"The game prefers valid positions over invalid positions," EOL
"And it handles double letters properly." EOL
"Guessing \"RATES\" when the word is \"TESTS\" shows \"__oo#\"");
}
void gameLoop() {
char guess[WORD_LENGTH + 1] = {0};
int guesses = 0;
int scan = 0;
puts(
"word\tunused alphabet" EOL
"====\t===============");
unsigned int i;
for (i = 0; i < 26; i++)
{
if (alpha[i] == '_')
{
impossible_array[impossible_current] = (char) ('a' + i);
impossible_current++;
}
}
printf("candidates (after filtering) are \n");
char ** possibleWords = filterWords(impossible_array);
int w = 0;
while (guesses < MAX_TRIES && strcmp(guess, word)) {
printf("\nGuess %i: ", guesses + 1);
/* unsigned int i;
for (i = 0; i < 26; i++)
{
if (alpha[i] == '_')
{
impossible_array[impossible_current] = (char) ('a' + i);
impossible_current++;
}
}
printf("candidates (after filtering) are \n"); */
char result[500];
//filterWords(impossible_array);
int r =0;
int c =0;
// char ** possibleWords = filterWords(impossible_array);
printf("\n%s\n", possibleWords[w]);
/* Size of possibleWords printout
printf("\n%lu\n", (sizeof(possibleWords)));
printf("\n%lu\n", (sizeof(possibleWords[0])));
printf("\n%lu\n", (sizeof(possibleWords)/sizeof(possibleWords[0]))); */
// test logic
//if(w > 10 /*sizeof(possibleWords)/sizeof(possibleWords[0])*/ ){
//break;
//}
strcpy(guess, possibleWords[w]);
//printf("Value of w %d \n", w);
++w;
/* for (int i =0; i<sizeof(possibleWords)/sizeof(possibleWords[0]); i++){
for (int j =0; j<6; j++){
guess[j] = possibleWords[i][j];
//printf("%c", possibleWords[i][j]);
} */
//printf("\n");
// }
//possibleWords[r][c];
// char * word = possibleWords[r];
// guess = word;
/*for(int i =0; i< (WORD_LENGTH+1); ++i){
guess [i] = possibleWords[r][i];
} */
/* for (row from 0 to possibleWords.size/ ){
for (col form 0 to psoze/ ossible words[0])
}
// ++r;
*/
// strcpy(result, * possibleWords);
int z = 0;
// printf("The result array is %s", result);
/* for (int i =0; i<(WORD_LENGTH+1); i++){
guess [i] = result [i];
} */
/* for(int i =0; i<(MAX_TRIES+1);i++) {
if( ((i)%6 ) ==0){
z=0;
printf("Run time: %d \n, it is time i: %d \n", z, i);
}
guess[z]= result[i];
// ++z; */
int counter =0;
// strcpy(), one by one for loop through the array
if ( true )
{
// (scan = scanf("%5s", guess)) == 1 &&
// ecs36b students -- implementing your solution roughtly here <===
/* if(counter == 0) {
for (int i =0; i<(WORD_LENGTH+1); i++){
guess [i] = result [i];
}
}
else if (counter ==1 ){
for(int i =0; i<(WORD_LENGTH+2); i++){
printf("%c", result[i+6]);
guess[i] = result[i+6];
}
}
if (counter ==2 ){
for(int i =0; i<(WORD_LENGTH+1); i++){
guess[i] = result[i+12];
}
}
if (counter ==3 ){
for(int i =0; i<(WORD_LENGTH+1); i++){
guess[i] = result[i+18];
}
}
if (counter ==4 ){
for(int i =0; i<(WORD_LENGTH+1); i++){
guess[i] = result[i+24];
}
}
if (counter ==5 ){
for(int i =0; i<(WORD_LENGTH+1); i++){
guess[i] = result[i+30];
}
} */
toLower(guess);
//Do not bother doing all the test logic if we've found the word.
if (strcmp(guess, word)) {
if (isWord(guess) && hasWord(guess)) {
++guesses;
//TODO: Check guess against word
if (checkWord(guess)) {
removeAlpha(guess);
printf("\t%s\n", alpha);
}
} else {
puts("Word not in list");
}
}
}
else {
if (scan == EOF) {
exit(EXIT_FAILURE);
}
printf("Invalid word. Must be %i characters\n", WORD_LENGTH);
}
//++ counter;
// ++r;
//printf(" value of r %d\n", r);
//++z;
}
// ++z;
if (strcmp(guess, word)) {
printf("You lose. The word was %s\n", word);
} else {
printf("You win. The word was %s\n", word);
}
}
//extranseous brace }
void removeAlpha(const char * guess) {
int i = 0;
int pos = 0;
if (guess != NULL) {
while (guess[i]) {
pos = strpos(alpha, guess[i]);
if (pos >= 0) {
alpha[pos] = '_';
}
++i;
}
}
}
int strpos(const char * str, char search) {
int i = 0;
if (str != NULL) {
while (str[i]) {
if (str[i] == search) {
return i;
}
++i;
}
}
return -1;
}
bool checkWord(const char * guess) {
if (strlen(guess) == strlen(word)) {
int i = 0;
int pos = -1;
//Copy is used to blank found characters
//This avoids wrong reports for double letters, for example "l" in "balls"
char copy[WORD_LENGTH + 1];
char result[WORD_LENGTH + 1];
result[WORD_LENGTH] = 0;
strcpy(copy, word);
//Do all correct positions first
while (copy[i]) {
if (copy[i] == guess[i]) {
//Character found and position correct
result[i] = '#';
copy[i] = '_';
} else {
//Fills remaining slots with blanks
//We could do this before the loop as well
result[i] = '_';
}
++i;
}
i = 0;
while (copy[i]) {
pos = strpos(copy, guess[i]);
//Char must exist but do not overwrite a good guess
if (pos >= 0 && result[i] != '#') {
//Character found but position wrong
result[i] = 'o';
copy[pos] = '_';
}
++i;
}
printf("%s", result);
return true;
}
return false;
}
char global_result[WORD_LENGTH + 1];
void
checkFilterWord(const char * guess)
{
if (strlen(guess) == strlen(word))
{
int i = 0;
int pos = -1;
//Copy is used to blank found characters
//This avoids wrong reports for double letters, for example "l" in "balls"
char copy[WORD_LENGTH + 1];
global_result[WORD_LENGTH] = 0;
strcpy(copy, word);
//Do all correct positions first
while (copy[i])
{
if (copy[i] == guess[i])
{
//Character found and position correct
global_result[i] = '#';
copy[i] = '_';
}
else
{
//Fills remaining slots with blanks
//We could do this before the loop as well
global_result[i] = '_';
}
++i;
}
i = 0;
while (copy[i])
{
pos = strpos(copy, guess[i]);
//Char must exist but do not overwrite a good guess
if (pos >= 0 && global_result[i] != '#')
{
//Character found but position wrong
global_result[i] = 'o';
copy[pos] = '_';
}
++i;
}
// printf("%s", result);
return;
}
return;
}
int toLower(char * str) {
int i = 0;
while (str[i]) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] |= 0x20; //Make lowercase
}
++i;
}
return i;
}
int hasWord(const char * word) {
//A bit longer to also contain the line terminator
char buffer[WORD_LENGTH + 4];
//Don't bother if the argument is invalid
if (word == NULL || strlen(word) != WORD_LENGTH || !isWord(word)) {
return false;
}
fseek(fpAll, memcache[word[0]-'a'], SEEK_SET);
//Stop comparing once we are beyond the current letter
while (fgets(buffer, WORD_LENGTH + 4, fpAll) != NULL && buffer[0]==word[0]) {
buffer[WORD_LENGTH]=0;
if (strcmp(word, buffer) == 0) {
return true;
}
}
return false;
}
#define MAXWORDS 12946 // from "wc ALL.TXT"
#define WORLD_LENGTH 5
char **
filterWords(const char * impossible)
{
unsigned int i, j, flag;
if (impossible == NULL) return NULL;
unsigned int char_count = strlen(impossible);
if (char_count > 26) return NULL;
char ** result = (char **) malloc(MAXWORDS * sizeof(char *));
char buffer[32];
fseek(fpAll, 0, SEEK_SET);
unsigned int result_count = 0;
bzero(buffer, 32);
// %[^\n]
int bc;
while ((bc = fscanf(fpAll, "%[^\n]", buffer)) != EOF)
{
// printf("bc = %d\n", bc);
flag = 0;
for (i = 0; i < char_count; i++)
{
for (j = 0; j < 5; j++)
{
if (buffer[j] == impossible[i]) flag = 1;
}
}
if (flag == 0)
{
checkFilterWord(buffer);
unsigned int check = 0;
for (i = 0; i < 5; i++)
{
if (global_result[i] == '_')
{
check = 1;
}
}
if (check == 0)
{
result[result_count++] = strdup(buffer);
fprintf(stderr, "%s\n", buffer);
}
}
bzero(buffer, 32);
fgetc(fpAll);
}
return result;
}
bool isWord(const char* word){
int i=-1;
if(strlen(word) == WORD_LENGTH){
while(word[++i]){
if(word[i]<'a' || word[i]>'z'){
return false;
}
}
return true;
}
return false;
}
long setup() {
FILE* cache;
char currentChar;
char currentWord[WORD_LENGTH + 1];
bool success = false;
//Don't bother if setup was already performed
if (wordCount > 0) {
return wordCount;
}
srand((int)time(0));
if ((cache = fopen("LISTS\\CACHE.BIN","rb")) != NULL) {
printf("Reading cache... ");
success = fread(memcache, sizeof(long), ALPHA_SIZE, cache) == ALPHA_SIZE;
fclose(cache);
if(success){
puts(" [OK]");
return wordCount = memcache[ALPHA_SIZE - 1];
}
else{
puts(" [FAIL]");
}
}
printf("Loading word list...");
fseek(fpSol, 0, SEEK_SET);
while (fgets(currentWord, WORD_LENGTH + 1, fpSol) != NULL) {
//Only increment if word length is correct
if (strlen(currentWord) == WORD_LENGTH) {
++wordCount;
}
}
puts(" [OK]");
memcache[ALPHA_SIZE-1] = wordCount;
if (!success) {
printf("Building cache...");
currentChar = 'a';
memcache[0] = 0;
fseek(fpAll, 0, SEEK_SET);
while (fgets(currentWord, WORD_LENGTH + 1, fpAll) != NULL) {
//Only proceed if word length is correct
if (strlen(currentWord) == WORD_LENGTH) {
if (currentChar != currentWord[0]) {
currentChar = currentWord[0];
memcache[currentChar - 'a'] = ftell(fpAll) - 5;
}
}
}
cache = fopen("LISTS\\CACHE.BIN", "wb");
if (cache == NULL) {
puts(" [FAIL]");
}
else{
fwrite(memcache, sizeof(long), ALPHA_SIZE, cache);
fclose(cache);
puts(" [OK]");
}
}
return wordCount;
}
int pickWord(char * word, int index) {
int i = 0;
fseek(fpSol, 0, SEEK_SET);
while (i <= index && fgets(word, WORD_LENGTH + 1, fpSol) != NULL) {
if (strlen(word) == WORD_LENGTH) {
++i;
}
}
return index;
}