-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmulti_indep.cpp
109 lines (96 loc) · 2.79 KB
/
multi_indep.cpp
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
#include <iostream>
#include <vector>
#include <sstream>
#include "clife.hpp"
#include "util.hpp"
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
/** Written by Sjors Gielen, eth0 winter 2014
* Feel free to use this for anything you like
*/
template <typename ValueType>
void print_rgb(GameOfLifeField<ValueType> red, GameOfLifeField<ValueType> green, GameOfLifeField<ValueType> blue) {
char on = 0x80;
char off = 0x00;
for(int y = 0; y < red.get_height(); ++y) {
for(int x = 0; x < red.get_width(); ++x) {
std::cout << (red.is_set(y, x) ? on : off);
std::cout << (green.is_set(y, x) ? on : off);
std::cout << (blue.is_set(y, x) ? on : off);
}
}
std::cout << std::flush;
}
template <char ifSet, char ifUnset>
struct SimpleValue {
bool value;
SimpleValue() : value(false) {}
SimpleValue(std::vector<SimpleValue> vec) : value(true) {}
SimpleValue(bool value) : value(value) {}
operator bool() const { return value; }
std::string hash() const {
return (value ? "1" : "0");
}
char getChar() const {
return (value ? 'o' : ' ');
}
void print(std::ostream &os) const {
os << (value ? ifSet : ifUnset);
}
void age_once() const {/* ignore */}
};
template <typename FieldType>
void check_stop_condition(FieldType field, std::vector<uint64_t> &earlier_hashes, bool &done, int &repeats_to_do) {
uint64_t hash = field.field_hash();
for(size_t i = 0; i < earlier_hashes.size(); ++i) {
if(earlier_hashes[i] == hash) {
done = true;
repeats_to_do = 10;
break;
}
}
earlier_hashes.push_back(hash);
}
int main(int argc, char *argv[]) {
int microsleeptime = 100000;
if(argc == 2) {
std::stringstream ss;
ss << argv[1];
ss >> microsleeptime;
if(!ss) {
std::cerr << "Usage: " << argv[0] << " [microsleeptime]" << std::endl;
}
} else if(argc > 2) {
std::cerr << "Usage: " << argv[0] << " [microsleeptime]" << std::endl;
}
init_random();
std::vector<uint64_t> earlier_hashes;
GameOfLifeField<SimpleValue<char(0x80), 0>> red(8, 80);
GameOfLifeField<SimpleValue<char(0x80), 0>> green(8, 80);
GameOfLifeField<SimpleValue<char(0x80), 0>> blue(8, 80);
red.generateRandom(35);
green.generateRandom(35);
blue.generateRandom(35);
bool red_done = false;
bool green_done = false;
bool blue_done = false;
int repeats_to_do = 0;
print_rgb(red, green, blue);
while(!red_done || !green_done || !blue_done || repeats_to_do > 0) {
if(repeats_to_do > 0) {
--repeats_to_do;
}
red.nextState();
green.nextState();
blue.nextState();
print_rgb(red, green, blue);
if(!red_done || !green_done || !blue_done) {
check_stop_condition(red, earlier_hashes, red_done, repeats_to_do);
check_stop_condition(green, earlier_hashes, green_done, repeats_to_do);
check_stop_condition(blue, earlier_hashes, blue_done, repeats_to_do);
}
usleep(microsleeptime);
continue;
}
}