Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/cgram cgram(6): use standard cursor keys, use standard...



details:   https://anonhg.NetBSD.org/src/rev/e884542d01a0
branches:  trunk
changeset: 935435:e884542d01a0
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Jul 02 19:11:01 2020 +0000

description:
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.

diffstat:

 games/cgram/cgram.c |  22 +++++++++++++---------
 1 files changed, 13 insertions(+), 9 deletions(-)

diffs (74 lines):

diff -r 76c7b9bd813a -r e884542d01a0 games/cgram/cgram.c
--- a/games/cgram/cgram.c       Thu Jul 02 17:15:00 2020 +0000
+++ b/games/cgram/cgram.c       Thu Jul 02 19:11:01 2020 +0000
@@ -120,16 +120,13 @@
 }
 
 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 @@
       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 @@
        }
        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 @@
        clear();
        break;
        case 14: /* ^N */
+       case KEY_DOWN:
        if (cury < lines.num-1) {
           cury++;
        }
@@ -282,6 +284,7 @@
        }
        break;
        case 16: /* ^P */
+       case KEY_UP:
        if (cury > 0) {
           cury--;
        }
@@ -335,6 +338,7 @@
    encode();
    opencurses();
 
+   keypad(stdscr, TRUE);
    loop();
 
    closecurses();



Home | Main Index | Thread Index | Old Index