-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
86 lines (69 loc) · 1.03 KB
/
util.c
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
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include "util.h"
int limit(int i, int l) {
if (i>l) return l;
else return i;
}
int ourrand(int min, int max) {
if (min == max) return min;
if (max < min) {
fprintf(stderr, "Max is smaller than min: %d %d\n", min, max);
exit(1);
}
return (random() % (max - min + 1)) + min;
}
struct coord key_to_direction(int key)
{
struct coord c;
switch(key) {
case '1':
c.x = -1;
c.y = 1;
break;
case '2':
case KEY_DOWN:
c.x = 0;
c.y = 1;
break;
case '3':
c.x = 1;
c.y = 1;
break;
case '4':
case KEY_LEFT:
c.x = -1;
c.y = 0;
break;
case '6':
case KEY_RIGHT:
c.x = 1;
c.y = 0;
break;
case '7':
c.x = -1;
c.y = -1;
break;
case '8':
case KEY_UP:
c.x = 0;
c.y = -1;
break;
case '9':
c.x = 1;
c.y = -1;
break;
default:
c.x = 0;
c.y = 0;
}
return c;
}
/* Pick a random element from a NULL-terminated list */
const void *random_element(const void **list)
{
int n = 0;
while(list[n]) n++;
return list[random() % n];
}