summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2020-07-02 19:11:01 +0000
committerrillig <rillig@NetBSD.org>2020-07-02 19:11:01 +0000
commitdf5cc7cf1cf61077ac0dcd2b376e61b3ed7fc388 (patch)
tree45b25fd134ccee949cd9da6cf705cf9f2ee83b73
parente51dd5d1fb713d15515575d8a5a600a11434a186 (diff)
downloadbsdgames-darwin-df5cc7cf1cf61077ac0dcd2b376e61b3ed7fc388.tar.gz
bsdgames-darwin-df5cc7cf1cf61077ac0dcd2b376e61b3ed7fc388.tar.zst
bsdgames-darwin-df5cc7cf1cf61077ac0dcd2b376e61b3ed7fc388.zip
cgram(6): use standard cursor keys, use standard shuffle algorithm
The previous shuffle algorithm asked for 100 random numbers, on average. The new algorithm asks exactly for 26 random numbers. Curses predefines numeric constants for keys, and there is no apparent reason not to use these standard keys for cursor movement.
-rw-r--r--cgram/cgram.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/cgram/cgram.c b/cgram/cgram.c
index 76ea55fb..5e5dd556 100644
--- a/cgram/cgram.c
+++ b/cgram/cgram.c
@@ -120,16 +120,13 @@ static void readquote(void) {
}
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 i=0; i<26; i++) key[i] = i;
+ for (int i=26; i>1; i--) {
+ int c = random() % i;
+ int t = key[i-1];
+ key[i-1] = key[c];
+ key[c] = t;
}
for (int y=0; y<lines.num; y++) {
@@ -244,9 +241,11 @@ static void loop(void) {
int ch = getch();
switch (ch) {
case 1: /* ^A */
+ case KEY_BEG:
curx=0;
break;
case 2: /* ^B */
+ case KEY_LEFT:
if (curx > 0) {
curx--;
}
@@ -256,9 +255,11 @@ static void loop(void) {
}
break;
case 5: /* ^E */
+ case KEY_END:
curx = strlen(lines.v[cury]);
break;
case 6: /* ^F */
+ case KEY_RIGHT:
if (curx < strlen(lines.v[cury])) {
curx++;
}
@@ -271,6 +272,7 @@ static void loop(void) {
clear();
break;
case 14: /* ^N */
+ case KEY_DOWN:
if (cury < lines.num-1) {
cury++;
}
@@ -282,6 +284,7 @@ static void loop(void) {
}
break;
case 16: /* ^P */
+ case KEY_UP:
if (cury > 0) {
cury--;
}
@@ -335,6 +338,7 @@ int main(void) {
encode();
opencurses();
+ keypad(stdscr, TRUE);
loop();
closecurses();