/
no8_20070909_1431.c
716 lines (633 loc) · 16.5 KB
/
no8_20070909_1431.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/* no8.c - created 2007 by inhaesio zha (zha@inhesion.com) */
/* gcc -ansi -O3 -L/usr/X11R6/lib -lX11 -o no8 no8.c */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#define SEED 0
#define NODES 512
#define TIME 256
#define ACTION_2_SWAP_WITH_LINK_0 0
#define ACTION_2_SWAP_WITH_LINK_1 1
#define ACTION_2_SWAP_VALUE_WITH_LINK_0 2
#define ACTION_2_SWAP_VALUE_WITH_LINK_1 3
#define ACTION_2_CONNECT_LINKS_0_AND_1 4
#define ACTION_2_INVERT_VALUE 5
#define ACTION_2_LINK_0_BECOME_MY_VALUE 6
#define ACTION_2_LINK_0_BECOME_0 7
#define ACTION_2_LINK_0_BECOME_1 8
#define ACTION_2_LINK_0_BECOME_MY_INVERTED_VALUE 9
#define ACTION_2_LINK_1_BECOME_MY_VALUE 10
#define ACTION_2_LINK_1_BECOME_0 11
#define ACTION_2_LINK_1_BECOME_1 12
#define ACTION_2_LINK_1_BECOME_MY_INVERTED_VALUE 13
#define ACTION_2_DO_NOTHING 14
#define ACTION_2_BECOME_0 15
#define ACTION_2_BECOME_1 16
#define ACTION_2_COUNT 6
#define ACTION_3_SWAP_WITH_LINK_0 0
#define ACTION_3_SWAP_WITH_LINK_1 1
#define ACTION_3_SWAP_WITH_LINK_2 2
#define ACTION_3_SWAP_VALUE_WITH_LINK_0 3
#define ACTION_3_SWAP_VALUE_WITH_LINK_1 4
#define ACTION_3_SWAP_VALUE_WITH_LINK_2 5
#define ACTION_3_CONNECT_LINKS_0_AND_1 6
#define ACTION_3_CONNECT_LINKS_1_AND_2 7
#define ACTION_3_CONNECT_LINKS_2_AND_0 8
#define ACTION_3_DISCONNECT_LINK_0 9
#define ACTION_3_DISCONNECT_LINK_1 10
#define ACTION_3_DISCONNECT_LINK_2 11
#define ACTION_3_INVERT_VALUE 12
#define ACTION_3_LINK_0_BECOME_MY_VALUE 13
#define ACTION_3_LINK_0_BECOME_0 14
#define ACTION_3_LINK_0_BECOME_1 15
#define ACTION_3_LINK_0_BECOME_MY_INVERTED_VALUE 16
#define ACTION_3_LINK_1_BECOME_MY_VALUE 17
#define ACTION_3_LINK_1_BECOME_0 18
#define ACTION_3_LINK_1_BECOME_1 19
#define ACTION_3_LINK_1_BECOME_MY_INVERTED_VALUE 20
#define ACTION_3_LINK_2_BECOME_MY_VALUE 21
#define ACTION_3_LINK_2_BECOME_0 22
#define ACTION_3_LINK_2_BECOME_1 23
#define ACTION_3_LINK_2_BECOME_MY_INVERTED_VALUE 24
#define ACTION_3_DO_NOTHING 25
#define ACTION_3_BECOME_0 26
#define ACTION_3_BECOME_1 27
#define ACTION_3_COUNT 6
#define ACTION_TABLE_2_SIZE 2 * 2 * 2
#define ACTION_TABLE_3_SIZE 2 * 2 * 2 * 2
#define SLEEP 1024
struct node_t {
unsigned int value;
unsigned int link_count;
struct node_t *link_0;
struct node_t *link_1;
struct node_t *link_2;
unsigned int display_index;
};
typedef struct node_t node_t;
struct network_t {
node_t *nodes[NODES];
node_t *nodes_display_order[NODES];
};
typedef struct network_t network_t;
static void connect_nodes(node_t *this_node, node_t *node_a, node_t *node_b);
static network_t *create_network();
static void destroy_network(network_t *network);
static void disconnect_nodes(node_t *node_a, node_t *node_b);
static int get_action_2(node_t *node);
static int get_action_3(node_t *node);
static void init_action_tables();
static void invert_value(node_t *node);
static unsigned int inverted_value(node_t *node);
static void iterate_network(network_t *network);
static void iterate_node(node_t *node);
static void iterate_node_2(node_t *node);
static void iterate_node_3(node_t *node);
static node_t *link_not_me(node_t *node_to_search, node_t *me);
static unsigned int node_can_connect(node_t *node);
static unsigned int node_can_disconnect(node_t *node);
static void nothing();
static void redirect_link(node_t *node_to_change, node_t *old_link,
node_t *new_link);
static void shuffle_down_links(node_t *node);
static void swap_nodes(node_t *node_a, node_t *node_b);
static void swap_values(node_t *node_a, node_t *node_b);
int action_table_2[ACTION_TABLE_2_SIZE];
int action_table_3[ACTION_TABLE_3_SIZE];
network_t *network;
void connect_nodes(node_t *this_node, node_t *node_a, node_t *node_b)
{
node_t *node_a_link_not_me;
node_t *node_b_link_not_me;
if (node_can_connect(node_a)) {
node_a->link_2 = node_b;
node_a->link_count = 3;
}
else if (node_can_connect(node_b)) {
node_b->link_2 = node_a;
node_b->link_count = 3;
}
else {
node_a_link_not_me = link_not_me(node_a, this_node);
redirect_link(node_a, node_a_link_not_me, node_b);
redirect_link(this_node, node_a, node_a_link_not_me);
node_b_link_not_me = link_not_me(node_b, this_node);
redirect_link(node_b, node_b_link_not_me, node_a);
redirect_link(this_node, node_b, node_b_link_not_me);
}
}
network_t *create_network()
{
network_t *network;
unsigned int each_node;
node_t *node;
network = malloc(sizeof(network_t));
for (each_node = 0; each_node < NODES; each_node++) {
node = malloc(sizeof(node_t));
node->value = rand() % 2;
node->link_count = 2;
node->link_2 = 0;
node->display_index = each_node;
network->nodes[each_node] = node;
network->nodes_display_order[each_node] = node;
}
for (each_node = 1; each_node < (NODES - 1); each_node++) {
node = network->nodes[each_node];
node->link_0 = network->nodes[each_node - 1];
node->link_1 = network->nodes[each_node + 1];
}
node = network->nodes[0];
node->link_0 = network->nodes[NODES - 1];
node->link_1 = network->nodes[1];
node = network->nodes[NODES - 1];
node->link_0 = network->nodes[NODES - 2];
node->link_1 = network->nodes[0];
return network;
}
void destroy_network(network_t *network)
{
unsigned int each_node;
for (each_node = 0; each_node < NODES; each_node++) {
free(network->nodes[each_node]);
}
}
void disconnect_nodes(node_t *node_a, node_t *node_b)
{
if (node_can_disconnect(node_a)) {
if (node_b == node_a->link_0) {
node_a->link_0 = 0;
shuffle_down_links(node_a);
}
else if (node_b == node_a->link_1) {
node_a->link_1 = 0;
shuffle_down_links(node_a);
}
else if (node_a->link_2) {
if (node_b == node_a->link_2) {
node_a->link_2 = 0;
}
}
else {
exit(23);
}
node_a->link_count = 2;
}
if (node_can_disconnect(node_b)) {
if (node_a == node_b->link_0) {
node_b->link_0 = 0;
shuffle_down_links(node_b);
}
else if (node_a == node_b->link_1) {
node_b->link_1 = 0;
shuffle_down_links(node_b);
}
else if (node_b->link_2) {
if (node_a == node_b->link_2) {
node_b->link_2 = 0;
}
}
else {
exit(24);
}
node_b->link_count = 2;
}
}
/*
void disconnect_nodes(node_t *node_a, node_t *node_b)
{
if (node_can_disconnect(node_a) && node_can_disconnect(node_b)) {
if (node_b == node_a->link_0) {
node_a->link_0 = 0;
shuffle_down_links(node_a);
}
else if (node_b == node_a->link_1) {
node_a->link_1 = 0;
shuffle_down_links(node_a);
}
else if (node_a->link_2) {
if (node_b == node_a->link_2) {
node_a->link_2 = 0;
}
}
else {
exit(23);
}
if (node_a == node_b->link_0) {
node_b->link_0 = 0;
shuffle_down_links(node_b);
}
else if (node_a == node_b->link_1) {
node_b->link_1 = 0;
shuffle_down_links(node_b);
}
else if (node_b->link_2) {
if (node_a == node_b->link_2) {
node_b->link_2 = 0;
}
}
else {
exit(24);
}
node_a->link_count = 2;
node_b->link_count = 2;
}
}
*/
int get_action_2(node_t *node)
{
int action;
unsigned int action_index;
action_index = (1 * node->value) + (2 * node->link_0->value)
+ (4 * node->link_1->value);
action = action_table_2[action_index];
return action;
}
int get_action_3(node_t *node)
{
int action;
unsigned int action_index;
action_index = (1 * node->value) + (2 * node->link_0->value)
+ (4 * node->link_1->value) + (8 * node->link_2->value);
action = action_table_3[action_index];
return action;
}
void init_action_tables()
{
unsigned int each_rule;
for (each_rule = 0; each_rule < ACTION_TABLE_2_SIZE; each_rule++) {
action_table_2[each_rule] = rand() % ACTION_2_COUNT;
}
for (each_rule = 0; each_rule < ACTION_TABLE_3_SIZE; each_rule++) {
action_table_3[each_rule] = rand() % ACTION_3_COUNT;
}
}
void invert_value(node_t *node)
{
node->value = inverted_value(node);
}
unsigned int inverted_value(node_t *node)
{
unsigned int inverted_value;
if (0 == node->value) {
inverted_value = 1;
}
else {
inverted_value = 0;
}
return inverted_value;
}
void iterate_network(network_t *network)
{
unsigned int each_node;
for (each_node = 0; each_node < NODES; each_node++) {
iterate_node(network->nodes[each_node]);
}
}
void iterate_node(node_t *node)
{
if (2 == node->link_count) {
iterate_node_2(node);
}
else {
iterate_node_3(node);
}
}
void iterate_node_2(node_t *node)
{
int action;
action = get_action_2(node);
switch (action) {
case ACTION_2_DO_NOTHING:
break;
case ACTION_2_BECOME_0:
node->value = 0;
break;
case ACTION_2_BECOME_1:
node->value = 1;
break;
case ACTION_2_INVERT_VALUE:
invert_value(node);
break;
case ACTION_2_CONNECT_LINKS_0_AND_1:
connect_nodes(node, node->link_0, node->link_1);
break;
case ACTION_2_SWAP_VALUE_WITH_LINK_0:
swap_values(node, node->link_0);
break;
case ACTION_2_SWAP_VALUE_WITH_LINK_1:
swap_values(node, node->link_1);
break;
case ACTION_2_LINK_0_BECOME_MY_VALUE:
node->link_0->value = node->value;
break;
case ACTION_2_LINK_0_BECOME_0:
node->link_0->value = 0;
break;
case ACTION_2_LINK_0_BECOME_1:
node->link_0->value = 1;
break;
case ACTION_2_LINK_0_BECOME_MY_INVERTED_VALUE:
node->link_0->value = inverted_value(node);
break;
case ACTION_2_LINK_1_BECOME_MY_VALUE:
node->link_1->value = node->value;
break;
case ACTION_2_LINK_1_BECOME_0:
node->link_1->value = 0;
break;
case ACTION_2_LINK_1_BECOME_1:
node->link_1->value = 1;
break;
case ACTION_2_LINK_1_BECOME_MY_INVERTED_VALUE:
node->link_1->value = inverted_value(node);
break;
case ACTION_2_SWAP_WITH_LINK_0:
swap_nodes(node, node->link_0);
break;
case ACTION_2_SWAP_WITH_LINK_1:
swap_nodes(node, node->link_1);
break;
default:
exit(25);
}
}
void iterate_node_3(node_t *node)
{
int action;
action = get_action_3(node);
switch (action) {
case ACTION_3_DO_NOTHING:
break;
case ACTION_3_BECOME_0:
node->value = 0;
break;
case ACTION_3_BECOME_1:
node->value = 1;
break;
case ACTION_3_INVERT_VALUE:
invert_value(node);
break;
case ACTION_3_DISCONNECT_LINK_0:
disconnect_nodes(node, node->link_0);
break;
case ACTION_3_DISCONNECT_LINK_1:
disconnect_nodes(node, node->link_1);
break;
case ACTION_3_DISCONNECT_LINK_2:
disconnect_nodes(node, node->link_2);
break;
case ACTION_3_CONNECT_LINKS_0_AND_1:
connect_nodes(node, node->link_0, node->link_1);
break;
case ACTION_3_CONNECT_LINKS_1_AND_2:
connect_nodes(node, node->link_1, node->link_2);
break;
case ACTION_3_CONNECT_LINKS_2_AND_0:
connect_nodes(node, node->link_2, node->link_0);
break;
case ACTION_3_SWAP_VALUE_WITH_LINK_0:
swap_values(node, node->link_0);
break;
case ACTION_3_SWAP_VALUE_WITH_LINK_1:
swap_values(node, node->link_1);
break;
case ACTION_3_SWAP_VALUE_WITH_LINK_2:
swap_values(node, node->link_2);
break;
case ACTION_3_LINK_0_BECOME_MY_VALUE:
node->link_0->value = node->value;
break;
case ACTION_3_LINK_0_BECOME_0:
node->link_0->value = 0;
break;
case ACTION_3_LINK_0_BECOME_1:
node->link_0->value = 1;
break;
case ACTION_3_LINK_0_BECOME_MY_INVERTED_VALUE:
node->link_0->value = inverted_value(node);
break;
case ACTION_3_LINK_1_BECOME_MY_VALUE:
node->link_1->value = node->value;
break;
case ACTION_3_LINK_1_BECOME_0:
node->link_1->value = 0;
break;
case ACTION_3_LINK_1_BECOME_1:
node->link_1->value = 1;
break;
case ACTION_3_LINK_1_BECOME_MY_INVERTED_VALUE:
node->link_1->value = inverted_value(node);
break;
case ACTION_3_LINK_2_BECOME_MY_VALUE:
node->link_2->value = node->value;
break;
case ACTION_3_LINK_2_BECOME_0:
node->link_2->value = 0;
break;
case ACTION_3_LINK_2_BECOME_1:
node->link_2->value = 1;
break;
case ACTION_3_LINK_2_BECOME_MY_INVERTED_VALUE:
node->link_2->value = inverted_value(node);
break;
case ACTION_3_SWAP_WITH_LINK_0:
swap_nodes(node, node->link_0);
break;
case ACTION_3_SWAP_WITH_LINK_1:
swap_nodes(node, node->link_1);
break;
case ACTION_3_SWAP_WITH_LINK_2:
swap_nodes(node, node->link_2);
break;
default:
exit(26);
}
}
node_t *link_not_me(node_t *node_to_search, node_t *me)
{
node_t *link = 0;
if (node_to_search->link_0 != me) {
link = node_to_search->link_0;
}
else if (node_to_search->link_1 != me) {
link = node_to_search->link_1;
}
else if (node_to_search->link_2) {
if (node_to_search->link_2 != me) {
link = node_to_search->link_2;
}
}
if (!link) {
exit(13);
}
return link;
}
unsigned int node_can_connect(node_t *node)
{
unsigned int can_connect;
if (2 == node->link_count) {
can_connect = 1;
}
else {
can_connect = 0;
}
return can_connect;
}
unsigned int node_can_disconnect(node_t *node)
{
unsigned int can_disconnect;
if (3 == node->link_count) {
can_disconnect = 1;
}
else {
can_disconnect = 0;
}
return can_disconnect;
}
void nothing()
{
return;
}
void redirect_link(node_t *node_to_change, node_t *old_link, node_t *new_link)
{
if (node_to_change != new_link) {
if (node_to_change->link_0 == old_link) {
node_to_change->link_0 = new_link;
}
else if (node_to_change->link_1 == old_link) {
node_to_change->link_1 = new_link;
}
else if (node_to_change->link_2 == old_link) {
node_to_change->link_2 = new_link;
}
}
}
void shuffle_down_links(node_t *node)
{
if (!node->link_0) {
node->link_0 = node->link_1;
node->link_1 = node->link_2;
node->link_2 = 0;
}
else if (!node->link_1) {
node->link_1 = node->link_2;
node->link_2 = 0;
}
}
void swap_nodes(node_t *node_a, node_t *node_b)
{
unsigned int temporary_display_index;
redirect_link(node_a->link_0, node_a, node_b);
redirect_link(node_a->link_1, node_a, node_b);
if (node_a->link_2) {
redirect_link(node_a->link_2, node_a, node_b);
}
redirect_link(node_b->link_0, node_b, node_a);
redirect_link(node_b->link_1, node_b, node_a);
if (node_b->link_2) {
redirect_link(node_b->link_2, node_b, node_a);
}
network->nodes_display_order[node_a->display_index] = node_b;
network->nodes_display_order[node_b->display_index] = node_a;
temporary_display_index = node_a->display_index;
node_a->display_index = node_b->display_index;
node_b->display_index = temporary_display_index;
}
void swap_values(node_t *node_a, node_t *node_b)
{
unsigned int temporary_value;
temporary_value = node_a->value;
node_a->value = node_b->value;
node_b->value = temporary_value;
}
int main(int argc, char *argv[])
{
unsigned int moment;
unsigned int node_index;
node_t *node;
Display *display;
GC gc;
int screen_number;
int window_x;
int window_y;
unsigned int each_x;
unsigned int each_y;
unsigned int window_border_width;
unsigned int window_height;
unsigned int window_width;
unsigned long gc_value_mask;
unsigned long window_background_color;
unsigned long window_border_color;
Window root_window;
Window window;
XGCValues gc_values;
Visual* default_visual;
Colormap colormap;
XColor system_color;
XColor exact_color;
display = XOpenDisplay(NULL);
screen_number = DefaultScreen(display);
root_window = RootWindow(display, screen_number);
window_x = 0;
window_y = 0;
window_width = NODES;
window_height = TIME;
window_border_width = 0;
window_border_color = BlackPixel(display, screen_number);
window_background_color = WhitePixel(display, screen_number);
window = XCreateSimpleWindow(display, root_window, window_x, window_y,
window_width, window_height, window_border_width,
window_border_color, window_background_color);
XMapWindow(display, window);
XFlush(display);
gc_value_mask = 0;
gc = XCreateGC(display, window, gc_value_mask, &gc_values);
default_visual = DefaultVisual(display, DefaultScreen(display));
colormap = XCreateColormap(display, window, default_visual, AllocNone);
XSync(display, False);
srandom(SEED);
init_action_tables();
network = create_network();
for (moment = 0; moment < TIME; moment++) {
for (node_index = 0; node_index < NODES; node_index++) {
node = network->nodes_display_order[node_index];
system_color.red = 0;
if (2 == node->link_count) {
system_color.green = USHRT_MAX / 2;
}
else if (3 == node->link_count) {
system_color.green = USHRT_MAX;
}
else {
exit(99);
}
if (0 == node->value) {
system_color.blue = USHRT_MAX / 2;
}
else if (1 == node->value) {
system_color.blue = USHRT_MAX;
}
else {
exit(100);
}
/*
system_color.blue = 0;
*/
XAllocColor(display, colormap, &system_color);
XSetForeground(display, gc, system_color.pixel);
XDrawPoint(display, window, gc, node_index, moment);
}
XFlush(display);
iterate_network(network);
}
while (1) {
usleep(SLEEP);
}
destroy_network(network);
XUnmapWindow(display, window);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}