-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAIN.C
More file actions
291 lines (248 loc) · 5.51 KB
/
Copy pathMAIN.C
File metadata and controls
291 lines (248 loc) · 5.51 KB
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
/*
WOR-DOS: WORDLE for MS-DOS
C89 with Borland Turbo-C++ 3
by vitawrap @ github (2022)
*/
#include <STDLIB.H>
#include <TIME.H>
#include <STDIO.H>
#include <CONIO.H>
#include "WORDLIST.H"
#include "STATS.H"
#define VER_MAJ 1
#define VER_MIN 1
/* Text mode attributes */
const char attr_exists = 0x6F;
const char attr_unknown = 0x4F;
const char attr_match = 0x2F;
const char attr_mismatch= 0x1F;
const char attr_input = 0x78;
#define ROW_OFFSET 16
void print_word(char* seq, char tattr, int col, int wid)
{
int i;
const char* clword = NULL;
int rof = ROW_OFFSET;
int cof = (col*2)+3;
char creg = 0;
char seqstr[LETTER_COUNT+1] = {0};
int len = 0;
char ltrcount[26] = {0};
char attrs[LETTER_COUNT] = {0};
int matchcnt = 0;
if (!seq) return;
strncpy(seqstr, seq, LETTER_COUNT);
strlwr(seqstr);
len = strlen(seqstr);
/* find_word is called before this
we know we don't need to sanitize
our input word besides strlwr.
*/
if (wid != -1)
{
clword = get_word(wid);
memset(attrs, attr_mismatch, sizeof(attrs));
/* first establish a letter count list */
for (i = 0; i < LETTER_COUNT; ++i)
++ltrcount[clword[i] - 97];
/* remove 1 to letters with a pos match */
for (i = 0; i < len; ++i)
{
creg = seqstr[i];
if (creg == clword[i])
{
--ltrcount[creg - 97];
attrs[i] = attr_match;
++matchcnt;
}
}
/* pass 2 if we didnt match the word */
if (matchcnt < LETTER_COUNT)
{
for (i = 0; i < len; ++i)
{
creg = seqstr[i];
if (ltrcount[creg - 97] && (attrs[i] != attr_match))
{
attrs[i] = attr_exists;
--ltrcount[creg - 97];
}
}
}
}
/* then color appropriately */
for (i = 0; i < LETTER_COUNT; ++i)
{
creg = seqstr[i];
window(rof+(i*2), cof, rof+1+(i*2), cof+1);
if (wid == -1)
textattr(tattr);
else
{
textattr(attrs[i]);
}
/* wid should be -1 when we are missing chars */
putch(creg? creg - 32 : ' ');
}
/* show cursor */
if (len < LETTER_COUNT)
{
window(rof+(len*2), cof, rof+1+(len*2), cof+1);
gotoxy(1, 1);
}
/* debug */
/*
window(1,1,40,2);
gotoxy(1,1);
cputs(seqstr);
*/
}
unsigned day_hash()
{
time_t timer;
struct tm* gmt;
unsigned thash = 0;
timer = time(NULL);
gmt = gmtime(&timer);
thash = gmt->tm_yday; /* lower 9 bits */
thash |= gmt->tm_year << 9; /* higher 7 bits */
return thash;
}
int main(int argc, char* argv[])
{
unsigned char creg = 0xFF;
unsigned char clast= 0xFF;
int ltr = 0;
unsigned id;
unsigned thash = day_hash();
char attempts[MAX_ATTEMPTS][LETTER_COUNT+1] = {0};
int attempt = 0;
int success = 0;
int i = 0;
int quitreq = 0;
int dispred = 0;
int serz = 0;
savedata_t sdata = {0};
/* must do this fist */
setup_word_map();
srand(thash);
id = rand() % WORD_COUNT;
memset(attempts, 0, sizeof(attempts));
/* check on saved data */
serz = load_data("w.sav", &sdata, thash);
if (serz)
{
memcpy(attempts, sdata.attempts, sizeof(attempts));
attempt = sdata.attempt;
success = sdata.success;
}
lowvideo();
textmode(C40);
textattr(0x0F);
clrscr();
gotoxy(17, 1);
cputs("WOR-DOS");
while ((attempt < MAX_ATTEMPTS) && !quitreq && !success)
{
/* print all attempts */
for (i = 0; i < attempt; ++i)
print_word(attempts[i], attr_match, i, id);
/* print current attempt */
print_word(attempts[attempt],dispred?attr_unknown:attr_input, i, -1);
dispred = 0;
while (!quitreq)
{
if (kbhit())
{
clast = creg;
creg = getch();
/* are we quitting? (esc) */
if (creg == 27)
{
quitreq = 1;
break;
}
/* are we removing a letter? */
if ((creg == 8) && (ltr > 0))
{
--ltr;
attempts[attempt][ltr] = '\x0';
break;
}
/* or are we submitting a word? */
if ((creg == '\r') && (ltr == LETTER_COUNT))
{
if (find_word(attempts[attempt]) != -1)
{
if (!strcmp(attempts[attempt],get_word(id)))
{
success = 1;
quitreq = 1;
} else {
ltr = 0;
}
++attempt;
}
else
{
dispred = 1;
}
break;
}
/* are we giving a letter? */
creg = tolower(creg);
if (islower(creg) && (ltr < LETTER_COUNT))
{
attempts[attempt][ltr] = creg;
attempts[attempt][ltr+1]= '\x0'; /* :-) */
++ltr;
break;
}
}
}
}
/* end screen, display all attempts, no input. */
for (i = 0; i < attempt; ++i)
print_word(attempts[i], attr_match, i, id);
/* clear current attempt buffer */
if (attempt < MAX_ATTEMPTS)
memcpy(attempts[i], 0, sizeof(attempts[0]));
/* serialize game data */
memcpy(sdata.attempts, attempts, sizeof(attempts));
sdata.attempt = attempt;
if (sdata.timehash != thash) /* upd. stats only after a game */
{
if (success)
++sdata.streak;
else
sdata.streak = 0;
sdata.wins += success;
sdata.losses += !success;
}
sdata.success = success;
sdata.timehash= thash;
serz = save_data("w.sav", &sdata);
/* display stats and quit message */
textattr(0x0F);
window(1,16,40,25);
if (success || (attempt == MAX_ATTEMPTS))
{
cprintf("You %s the word!\r\nCheck again tomorrow!\r\n",
success? "\aFOUND": "DID NOT find"
);
cprintf("Attempts: %c/%i\r\n",
success? attempt + 48 : 'X', MAX_ATTEMPTS);
cprintf("Streak: %i\r\n", sdata.streak);
cprintf("All-time W: %i L: %i\r\n", sdata.wins, sdata.losses);
cprintf("Win ratio: %.3f%%\r\n",
((float)sdata.wins / (sdata.wins + sdata.losses)) * 100.f);
}
/* demake credits :-) */
textattr(0x1E);
cprintf("\n WOR-DOS %i.%i by @viwrap \r\n", VER_MAJ, VER_MIN);
getch();
textmode(C80);
textattr(0x0F);
normvideo();
return 0;
}