]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cgram/cgram.c
98f8bece421afd0839e9851a2da6302c254e1c9a
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
42 #include "pathnames.h"
44 ////////////////////////////////////////////////////////////
47 xstrdup(const char *s
)
51 ret
= malloc(strlen(s
) + 1);
53 errx(1, "Out of memory");
59 ////////////////////////////////////////////////////////////
67 stringarray_init(struct stringarray
*a
)
74 stringarray_cleanup(struct stringarray
*a
)
80 stringarray_add(struct stringarray
*a
, const char *s
)
82 a
->v
= realloc(a
->v
, (a
->num
+ 1) * sizeof(a
->v
[0]));
84 errx(1, "Out of memory");
86 a
->v
[a
->num
] = xstrdup(s
);
90 ////////////////////////////////////////////////////////////
92 static struct stringarray lines
;
93 static struct stringarray sollines
;
95 static int scrolldown
;
102 FILE *f
= popen(_PATH_FORTUNE
, "r");
104 err(1, "%s", _PATH_FORTUNE
);
107 char buf
[128], buf2
[8 * sizeof(buf
)];
108 while (fgets(buf
, sizeof buf
, f
) != NULL
) {
109 char *s
= strrchr(buf
, '\n');
111 assert(strlen(s
) == 1);
115 for (i
= j
= 0; buf
[i
] != '\0'; i
++) {
116 if (buf
[i
] == '\t') {
120 } else if (buf
[i
] == '\b') {
129 stringarray_add(&lines
, buf2
);
130 stringarray_add(&sollines
, buf2
);
140 for (int i
= 0; i
< 26; i
++)
142 for (int i
= 26; i
> 1; i
--) {
143 int c
= random() % i
;
149 for (int y
= 0; y
< lines
.num
; y
++) {
150 for (unsigned x
= 0; lines
.v
[y
][x
] != '\0'; x
++) {
151 if (islower((unsigned char)lines
.v
[y
][x
])) {
152 int q
= lines
.v
[y
][x
] - 'a';
153 lines
.v
[y
][x
] = 'a' + key
[q
];
155 if (isupper((unsigned char)lines
.v
[y
][x
])) {
156 int q
= lines
.v
[y
][x
] - 'A';
157 lines
.v
[y
][x
] = 'A' + key
[q
];
166 assert(cury
>= 0 && cury
< lines
.num
);
167 if (curx
>= strlen(lines
.v
[cury
])) {
172 int och
= lines
.v
[cury
][curx
];
173 if (!isalpha((unsigned char)och
)) {
178 int loch
= tolower((unsigned char)och
);
179 int uoch
= toupper((unsigned char)och
);
180 int lch
= tolower((unsigned char)ch
);
181 int uch
= toupper((unsigned char)ch
);
183 for (int y
= 0; y
< lines
.num
; y
++) {
184 for (unsigned x
= 0; lines
.v
[y
][x
] != '\0'; x
++) {
185 if (lines
.v
[y
][x
] == loch
) {
187 } else if (lines
.v
[y
][x
] == uoch
) {
189 } else if (lines
.v
[y
][x
] == lch
) {
190 lines
.v
[y
][x
] = loch
;
191 } else if (lines
.v
[y
][x
] == uch
) {
192 lines
.v
[y
][x
] = uoch
;
199 ////////////////////////////////////////////////////////////
206 for (int i
= 0; i
< LINES
- 1; i
++) {
208 int ln
= i
+ scrolldown
;
209 if (ln
< lines
.num
) {
210 for (unsigned j
= 0; lines
.v
[i
][j
] != '\0'; j
++) {
211 int ch
= lines
.v
[i
][j
];
212 if (ch
!= sollines
.v
[i
][j
] &&
213 isalpha((unsigned char)ch
)) {
217 if (hinting
&& ch
== sollines
.v
[i
][j
] &&
218 isalpha((unsigned char)ch
)) {
222 addch(lines
.v
[i
][j
]);
235 addstr("~ to quit, * to cheat, ^pnfb to move");
239 move(cury
- scrolldown
, curx
);
258 ////////////////////////////////////////////////////////////
276 } else if (cury
> 0) {
278 curx
= strlen(lines
.v
[cury
]);
283 curx
= strlen(lines
.v
[cury
]);
287 if (curx
< strlen(lines
.v
[cury
])) {
289 } else if (cury
< lines
.num
- 1) {
299 if (cury
< lines
.num
- 1) {
302 if (curx
> strlen(lines
.v
[cury
])) {
303 curx
= strlen(lines
.v
[cury
]);
305 if (scrolldown
< cury
- (LINES
- 2)) {
306 scrolldown
= cury
- (LINES
- 2);
314 if (curx
> strlen(lines
.v
[cury
])) {
315 curx
= strlen(lines
.v
[cury
]);
317 if (scrolldown
> cury
) {
328 if (isascii(ch
) && isalpha(ch
)) {
329 if (substitute(ch
)) {
330 if (curx
< strlen(lines
.v
[cury
])) {
333 if (curx
== strlen(lines
.v
[cury
]) &&
334 cury
< lines
.num
- 1) {
339 } else if (curx
< strlen(lines
.v
[cury
]) &&
340 ch
== lines
.v
[cury
][curx
]) {
342 if (curx
== strlen(lines
.v
[cury
]) &&
343 cury
< lines
.num
- 1) {
355 ////////////////////////////////////////////////////////////
361 stringarray_init(&lines
);
362 stringarray_init(&sollines
);
368 keypad(stdscr
, true);
372 stringarray_cleanup(&sollines
);
373 stringarray_cleanup(&lines
);