-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.c
195 lines (163 loc) · 5.87 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <regex.h>
#include <time.h>
#include <security/pam_modules.h>
#include <security/pam_ext.h>
char DICT[] = "/usr/share/dict/words";
// Fetches a 5 character word from a dict
// Places the nth word in the char[] pointed to by word (assumes word is not null)
// Returns the count of 5 character words in the dict on success or a negative int on failure
int fetch_word(char* dict, int n, char* word) {
int word_count = 0;
int err;
regex_t regex;
err = regcomp(®ex, "^[abcdefghijklmnopqrstuvwxyz]{5}$", REG_EXTENDED);
if (err != 0) {
return -1; // Failed to compile regex
}
// Read dict
FILE *stream;
if(stream = fopen(dict, "r")) {
// Read words from dict
char w[16];
int rmatch;
while (fgets(w, 16, stream) != NULL) {
// Remove newlines
int wlen = strlen(w);
if (w[wlen-1] == '\n') {
w[wlen-1] = '\0';
}
// If word matches regex add it to the count
rmatch = regexec(®ex, w, 0, NULL, 0);
if (rmatch == 0) {
if (word_count == n && word != NULL) {
for (size_t i = 0; i < strlen(word) && i < strlen(w); i++) {
word[i] = w[i];
}
}
word_count += 1;
}
}
fclose(stream);
} else {
return -2; // Failed to read dict
}
return word_count;
}
// Check if word is in dict
// Returns 1 if yes, 0 if no, negative int on error
int check_word(char *dict, char *word) {
FILE *stream;
if(stream = fopen(dict, "r")) {
// Read words from dict
char w[16];
while (fgets(w, 16, stream) != NULL) {
// Remove newlines
int wlen = strlen(w);
if (w[wlen-1] == '\n') {
w[wlen-1] = '\0';
}
if (strlen(w) == strlen(word) && strncmp(w, word, strlen(word)) == 0) {
return 1; // Word is in dict
}
}
fclose(stream);
} else {
return -1; // Failed to read dict
}
return 0; // Word is not in dict
}
// Handles a round of guessing
// Prompts for a guess, checks if the guess is valid, and checks if the guess is correct
// Displays the result of the guess as follows:
// ? - incorrect char, * - correct char in wrong location, or the char itself if it is correct and properly placed
// Returns 1 on a valid and correct guess, 0 on a valid and incorrect guess, or a negative int on error
int wordle_guess(pam_handle_t *pamh, char* word) {
// Prompt for valid guess
int valid = 0;
char *resp = NULL;
while (valid == 0) {
int retval;
retval = pam_prompt(pamh, PAM_PROMPT_ECHO_ON, &resp, "Word: ");
if (retval == PAM_SUCCESS) {
if (strlen(word) == strlen(resp)) {
int known = check_word(DICT, resp);
if (known == 1) {
valid = 1;
} else if (known == 0) {
pam_info(pamh, "Invalid guess: unknown word.");
} else {
pam_info(pamh, "Warning: error reading dictionary.");
valid = 1;
}
} else {
pam_info(pamh, "Invalid guess: guess length != word length.");
}
} else {
return -1;
}
}
// Build the next hint based on the guess
char hint[strlen(word)];
for (size_t i = 0; i < strlen(word); i++) {
if (word[i] == resp[i]) { // Letter is correct and properly placed
hint[i] = word[i];
} else {
int misplacements = 0;
for (size_t j = 0; j < strlen(word); j++) {
if (resp[i] == word[j] && word[j] != resp[j]) {
misplacements += 1;
}
}
int handled_misplacements = 0;
for (size_t j = 0; j < i; j++) {
if (resp[i] == resp[j] && word[j] != resp[j]) {
handled_misplacements += 1;
}
}
if (handled_misplacements < misplacements) {
hint[i] = '*'; // Letter is correct but in the wrong place
} else {
hint[i] = '?'; // Letter is incorrect
}
}
}
if (strncmp(hint, word, strlen(word)) == 0) { // Guess is correct
pam_info(pamh, "Correct!");
return 1;
}
pam_info(pamh, "Hint->%s", hint);
return 0;
}
int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) {
int rounds = 6; // Maximum number of guessing rounds
pam_info(pamh, "--- Welcome to PAM-Wordle! ---\n\nA five character [a-z] word has been selected.\nYou have %d attempts to guess the word.\n\nAfter each guess you will recieve a hint which indicates:\n? - what letters are wrong.\n* - what letters are in the wrong spot.\n[a-z] - what letters are correct.\n", rounds);
char word[5] = "linux";
int word_count = fetch_word(DICT, 0, NULL);
if (word_count > 0) {
srand(time(0));
int n = rand() % word_count;
fetch_word(DICT, n, word);
} else {
pam_info(pamh, "Warning: error reading dictionary.");
}
for (int i = 0; i < rounds; i++) {
pam_info(pamh, "--- Attempt %d of %d ---", i + 1, rounds);
int status = wordle_guess(pamh, word);
if (status == 1) {
return PAM_SUCCESS;
} else if (status < 0) {
return PAM_AUTH_ERR;
}
}
pam_info(pamh, "You lose.\nThe word was: %s", word);
return PAM_AUTH_ERR;
}
int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return PAM_IGNORE;
}
int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return PAM_SUCCESS;
}