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
|
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by David A. Holland.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <err.h>
#include <assert.h>
#include <curses.h>
#include "pathnames.h"
////////////////////////////////////////////////////////////
static char *xstrdup(const char *s) {
char *ret;
ret = malloc(strlen(s) + 1);
if (ret == NULL) {
errx(1, "Out of memory");
}
strcpy(ret, s);
return ret;
}
////////////////////////////////////////////////////////////
struct stringarray {
char **v;
int num;
};
static void stringarray_init(struct stringarray *a) {
a->v = NULL;
a->num = 0;
}
static void stringarray_cleanup(struct stringarray *a) {
free(a->v);
}
static void stringarray_add(struct stringarray *a, const char *s) {
a->v = realloc(a->v, (a->num + 1) * sizeof(a->v[0]));
if (a->v == NULL) {
errx(1, "Out of memory");
}
a->v[a->num] = xstrdup(s);
a->num++;
}
////////////////////////////////////////////////////////////
static struct stringarray lines;
static struct stringarray sollines;
static bool hinting;
static int scrolldown;
static unsigned curx;
static int cury;
static void readquote(void) {
FILE *f = popen(_PATH_FORTUNE, "r");
if (!f) {
err(1, "%s", _PATH_FORTUNE);
}
char buf[128], buf2[8*sizeof(buf)];
while (fgets(buf, sizeof(buf), f)) {
char *s = strrchr(buf, '\n');
assert(s);
assert(strlen(s)==1);
*s = 0;
int i,j;
for (i=j=0; buf[i]; i++) {
if (buf[i]=='\t') {
buf2[j++] = ' ';
while (j%8) buf2[j++] = ' ';
}
else if (buf[i]=='\b') {
if (j>0) j--;
}
else {
buf2[j++] = buf[i];
}
}
buf2[j] = 0;
stringarray_add(&lines, buf2);
stringarray_add(&sollines, buf2);
}
pclose(f);
}
static void encode(void) {
int used[26];
for (int i=0; i<26; i++) used[i] = 0;
int key[26];
int keypos=0;
while (keypos < 26) {
int c = random()%26;
if (used[c]) continue;
key[keypos++] = c;
used[c] = 1;
}
for (int y=0; y<lines.num; y++) {
for (unsigned x=0; lines.v[y][x]; x++) {
if (islower((unsigned char)lines.v[y][x])) {
int q = lines.v[y][x]-'a';
lines.v[y][x] = 'a'+key[q];
}
if (isupper((unsigned char)lines.v[y][x])) {
int q = lines.v[y][x]-'A';
lines.v[y][x] = 'A'+key[q];
}
}
}
}
static int substitute(int ch) {
assert(cury>=0 && cury<lines.num);
if (curx >= strlen(lines.v[cury])) {
beep();
return -1;
}
int och = lines.v[cury][curx];
if (!isalpha((unsigned char)och)) {
beep();
return -1;
}
int loch = tolower((unsigned char)och);
int uoch = toupper((unsigned char)och);
int lch = tolower((unsigned char)ch);
int uch = toupper((unsigned char)ch);
for (int y=0; y<lines.num; y++) {
for (unsigned x=0; lines.v[y][x]; x++) {
if (lines.v[y][x]==loch) {
lines.v[y][x] = lch;
}
else if (lines.v[y][x]==uoch) {
lines.v[y][x] = uch;
}
else if (lines.v[y][x]==lch) {
lines.v[y][x] = loch;
}
else if (lines.v[y][x]==uch) {
lines.v[y][x] = uoch;
}
}
}
return 0;
}
////////////////////////////////////////////////////////////
static void redraw(void) {
erase();
bool won = true;
for (int i=0; i<LINES-1; i++) {
move(i, 0);
int ln = i+scrolldown;
if (ln < lines.num) {
for (unsigned j=0; lines.v[i][j]; j++) {
int ch = lines.v[i][j];
if (ch != sollines.v[i][j] && isalpha((unsigned char)ch)) {
won = false;
}
bool bold=false;
if (hinting && ch==sollines.v[i][j] &&
isalpha((unsigned char)ch)) {
bold = true;
attron(A_BOLD);
}
addch(lines.v[i][j]);
if (bold) {
attroff(A_BOLD);
}
}
}
clrtoeol();
}
move(LINES-1, 0);
if (won) {
addstr("*solved* ");
}
addstr("~ to quit, * to cheat, ^pnfb to move");
move(LINES-1, 0);
move(cury-scrolldown, curx);
refresh();
}
static void opencurses(void) {
initscr();
cbreak();
noecho();
}
static void closecurses(void) {
endwin();
}
////////////////////////////////////////////////////////////
static void loop(void) {
bool done=false;
while (!done) {
redraw();
int ch = getch();
switch (ch) {
case 1: /* ^A */
curx=0;
break;
case 2: /* ^B */
if (curx > 0) {
curx--;
}
else if (cury > 0) {
cury--;
curx = strlen(lines.v[cury]);
}
break;
case 5: /* ^E */
curx = strlen(lines.v[cury]);
break;
case 6: /* ^F */
if (curx < strlen(lines.v[cury])) {
curx++;
}
else if (cury < lines.num - 1) {
cury++;
curx = 0;
}
break;
case 12: /* ^L */
clear();
break;
case 14: /* ^N */
if (cury < lines.num-1) {
cury++;
}
if (curx > strlen(lines.v[cury])) {
curx = strlen(lines.v[cury]);
}
if (scrolldown < cury - (LINES-2)) {
scrolldown = cury - (LINES-2);
}
break;
case 16: /* ^P */
if (cury > 0) {
cury--;
}
if (curx > strlen(lines.v[cury])) {
curx = strlen(lines.v[cury]);
}
if (scrolldown > cury) {
scrolldown = cury;
}
break;
case '*':
hinting = !hinting;
break;
case '~':
done = true;
break;
default:
if (isalpha(ch)) {
if (!substitute(ch)) {
if (curx < strlen(lines.v[cury])) {
curx++;
}
if (curx==strlen(lines.v[cury]) && cury < lines.num-1) {
curx=0;
cury++;
}
}
}
else if (curx < strlen(lines.v[cury]) && ch==lines.v[cury][curx]) {
curx++;
if (curx==strlen(lines.v[cury]) && cury < lines.num-1) {
curx=0;
cury++;
}
}
else {
beep();
}
break;
}
}
}
////////////////////////////////////////////////////////////
int main(void) {
stringarray_init(&lines);
stringarray_init(&sollines);
srandom(time(NULL));
readquote();
encode();
opencurses();
loop();
closecurses();
stringarray_cleanup(&sollines);
stringarray_cleanup(&lines);
return 0;
}
|