-
Notifications
You must be signed in to change notification settings - Fork 1
/
nudist_platypus_gold.c
204 lines (176 loc) · 4.83 KB
/
nudist_platypus_gold.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <time.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ncurses.h>
#define PLATYPUS_LENGTH 7
#define PLATYPUS_PATTERN "Oo....*"
#define NB_PLATYPUS 20
#define NB_COLORS 7
#define TIME_TO_SLEEP 100000
/*
TODO
Fix color bugs
Add custom colors
Choose an option syntax
*/
// Platypus, tail to head (...*oO)
typedef struct {
int tail;
int dir;
int *x;
int *y;
int length;
} Platypus;
void platypus_init(Platypus *p, int width, int height, int length) {
int i,
x = width / 2, //rand() % width,
y = height / 2; //rand() % height;
p->tail = 0;
p->dir = rand() % 8;
p->x = malloc(length * sizeof (int));
p->y = malloc(length * sizeof (int));
p->length = length;
for (i = 0; i < length; i++) {
p->x[i] = x;
p->y[i] = y;
}
}
void platypus_move(Platypus *p, char *pattern, int width, int height) {
int i;
int pattern_length = strlen(pattern);
mvprintw(p->y[p->tail], p->x[p->tail], " ");
p->dir = (p->dir + 7 + rand() % 3) % 8; // Turn left, right or don't turn.
switch (p->dir) {
case 0:
p->x[p->tail] = p->x[(p->tail - 1 + p->length) % p->length] + 1;
p->y[p->tail] = p->y[(p->tail - 1 + p->length) % p->length];
break;
case 1:
p->x[p->tail] = p->x[(p->tail - 1 + p->length) % p->length] + 1;
p->y[p->tail] = p->y[(p->tail - 1 + p->length) % p->length] - 1;
break;
case 2:
p->x[p->tail] = p->x[(p->tail - 1 + p->length) % p->length];
p->y[p->tail] = p->y[(p->tail - 1 + p->length) % p->length] - 1;
break;
case 3:
p->x[p->tail] = p->x[(p->tail - 1 + p->length) % p->length] - 1;
p->y[p->tail] = p->y[(p->tail - 1 + p->length) % p->length] - 1;
break;
case 4:
p->x[p->tail] = p->x[(p->tail - 1 + p->length) % p->length] - 1;
p->y[p->tail] = p->y[(p->tail - 1 + p->length) % p->length];
break;
case 5:
p->x[p->tail] = p->x[(p->tail - 1 + p->length) % p->length] - 1;
p->y[p->tail] = p->y[(p->tail - 1 + p->length) % p->length] + 1;
break;
case 6:
p->x[p->tail] = p->x[(p->tail - 1 + p->length) % p->length];
p->y[p->tail] = p->y[(p->tail - 1 + p->length) % p->length] + 1;
break;
case 7:
p->x[p->tail] = p->x[(p->tail - 1 + p->length) % p->length] + 1;
p->y[p->tail] = p->y[(p->tail - 1 + p->length) % p->length] + 1;
break;
}
if (p->x[p->tail] >= width || p->x[p->tail] < 0 || p->y[p->tail] >= height || p->y[p->tail] < 0) {
p->x[p->tail] = width / 2;
p->y[p->tail] = height / 2;
}
p->tail = (p->tail + 1) % p->length;
for (i = 0; i < p->length; i++) {
// Platypus example : 012345678 (p->length = 9)
// Pattern example : Oo*. (pattern_length = 4)
if (i < p->length - pattern_length) {
mvprintw(p->y[(i + p->tail) % p->length], p->x[(i + p->tail) % p->length], ".");
} else {
mvprintw(p->y[(i + p->tail) % p->length], p->x[(i + p->tail) % p->length], "%c", pattern[p->length - i - 1]);
}
}
}
void platypus_debug(Platypus *p) {
int i;
printf("Platypus:\n");
printf(" dir: %d\n", p->dir);
printf(" tail: %d\n", p->tail);
printf(" x,y :");
for (i = 0; i < p->length; i++) {
printf(" (%d,%d)", p->x[i], p->y[i]);
}
printf("\n");
}
void print_usage() {
printf("Usage: nudist_platypus_gold [option]\n");
printf("\t--help\tPrint this message.\n");
printf("\t-c\tColors, colors everywhere!\n");
}
int main(int argc, char *argv[]) {
Platypus platypus[NB_PLATYPUS];
int colors[NB_COLORS];
char *pattern = PLATYPUS_PATTERN;
time_t t;
int color_flag = 0;
int platypus_length = PLATYPUS_LENGTH;
/*nb_platypus = NB_PLATYPUS,
time_to_sleep = TIME_TO_SLEEP;*/
int i;
// TODO Parse more than one argument, then more than one option in one argument
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i],"--help")) {
print_usage();
return 0;
} else if (!strcmp(argv[i],"-c")) {
color_flag = 1;
}/* else if (!strcmp(argv[1],"-C")) {
changing_color_flag = 1;
}*/ else {
pattern = argv[i];
platypus_length = strlen(pattern);
if (platypus_length < 1) {
platypus_length = PLATYPUS_LENGTH;
}
}
}
if (color_flag) {
for (i = 0; i < NB_COLORS; i++) {
colors[i] = i+1;
}
}
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
int height = w.ws_row,
width = w.ws_col;
srand(time(&t));
initscr();
curs_set(0);
if (color_flag) {
if(has_colors() == FALSE) {
endwin();
printf("Your terminal does not support color, don't use -c option.\n");
exit(1);
}
start_color();
for (i = 0; i < NB_COLORS; i++) {
init_pair(i + 1, colors[i], COLOR_BLACK);
}
}
for (i = 0; i < NB_PLATYPUS; i++) {
platypus_init(&platypus[i], width, height, platypus_length);
}
while (1) {
for (i = 0; i < NB_PLATYPUS; i++) {
attron(COLOR_PAIR(i % NB_COLORS + 1));
platypus_move(&platypus[i], pattern, width, height);
attroff(COLOR_PAIR(i % NB_COLORS + 1));
//platypus_debug(&platypus[i]);
}
refresh();
usleep(TIME_TO_SLEEP);
}
endwin();
return 0;
}