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.
}
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++) {
int ch = getch();
switch (ch) {
case 1: /* ^A */
+ case KEY_BEG:
curx=0;
break;
case 2: /* ^B */
+ case KEY_LEFT:
if (curx > 0) {
curx--;
}
}
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++;
}
clear();
break;
case 14: /* ^N */
+ case KEY_DOWN:
if (cury < lines.num-1) {
cury++;
}
}
break;
case 16: /* ^P */
+ case KEY_UP:
if (cury > 0) {
cury--;
}
encode();
opencurses();
+ keypad(stdscr, TRUE);
loop();
closecurses();