-
Notifications
You must be signed in to change notification settings - Fork 2
/
single.cpp
85 lines (73 loc) · 1.9 KB
/
single.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
#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 <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>> field(8, 80);
field.generateRandom(35);
bool field_done = false;
int repeats_to_do = 0;
field.print(std::cout);
while(!field_done || repeats_to_do > 0) {
if(repeats_to_do > 0) {
--repeats_to_do;
}
field.nextState();
field.print(std::cout);
if(!field_done) {
check_stop_condition(field, earlier_hashes, field_done, repeats_to_do);
}
usleep(microsleeptime);
continue;
}
}