-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-of-life.cpp
191 lines (186 loc) · 5.35 KB
/
game-of-life.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
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
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std;
typedef unsigned char byte;
class world {
public:
world( int x, int y ) : _wid( x ), _hei( y ) {
int s = _wid * _hei * sizeof( byte );
_cells = new byte[s];
memset( _cells, 0, s );
}
~world() {
delete [] _cells;
}
int wid() const {
return _wid;
}
int hei() const {
return _hei;
}
byte at( int x, int y ) const {
return _cells[x + y * _wid];
}
void set( int x, int y, byte c ) {
_cells[x + y * _wid] = c;
}
void swap( world* w ) {
memcpy( _cells, w->_cells, _wid * _hei * sizeof( byte ) );
}
private:
int _wid, _hei;
byte* _cells;
};
class rule {
public:
rule( world* w ) : wrd( w ) {
wid = wrd->wid();
hei = wrd->hei();
wrdT = new world( wid, hei );
}
~rule() {
if( wrdT ) delete wrdT;
}
bool hasLivingCells() {
for( int y = 0; y < hei; y++ )
for( int x = 0; x < wid; x++ )
if( wrd->at( x, y ) ) return true;
cout << "*** All cells are dead!!! ***\n\n";
return false;
}
void swapWrds() {
wrd->swap( wrdT );
}
void setRuleB( vector<int>& birth ) {
_birth = birth;
}
void setRuleS( vector<int>& stay ) {
_stay = stay;
}
void applyRules() {
int n;
for( int y = 0; y < hei; y++ ) {
for( int x = 0; x < wid; x++ ) {
n = neighbours( x, y );
if( wrd->at( x, y ) ) {
wrdT->set( x, y, inStay( n ) ? 1 : 0 );
} else {
wrdT->set( x, y, inBirth( n ) ? 1 : 0 );
}
}
}
}
private:
int neighbours( int xx, int yy ) {
int n = 0, nx, ny;
for( int y = -1; y < 2; y++ ) {
for( int x = -1; x < 2; x++ ) {
if( !x && !y ) continue;
nx = ( wid + xx + x ) % wid;
ny = ( hei + yy + y ) % hei;
n += wrd->at( nx, ny ) > 0 ? 1 : 0;
}
}
return n;
}
bool inStay( int n ) {
return( _stay.end() != find( _stay.begin(), _stay.end(), n ) );
}
bool inBirth( int n ) {
return( _birth.end() != find( _birth.begin(), _birth.end(), n ) );
}
int wid, hei;
world *wrd, *wrdT;
vector<int> _stay, _birth;
};
class cellular {
public:
cellular( int w, int h ) : rl( 0 ) {
wrd = new world( w, h );
}
~cellular() {
if( rl ) delete rl;
delete wrd;
}
void start( int r ) {
rl = new rule( wrd );
gen = 1;
vector<int> t;
switch( r ) {
case 1: // conway
t.push_back( 2 ); t.push_back( 3 ); rl->setRuleS( t );
t.clear(); t.push_back( 3 ); rl->setRuleB( t );
break;
case 2: // amoeba
t.push_back( 1 ); t.push_back( 3 ); t.push_back( 5 ); t.push_back( 8 ); rl->setRuleS( t );
t.clear(); t.push_back( 3 ); t.push_back( 5 ); t.push_back( 7 ); rl->setRuleB( t );
break;
case 3: // life34
t.push_back( 3 ); t.push_back( 4 ); rl->setRuleS( t );
rl->setRuleB( t );
break;
case 4: // maze
t.push_back( 1 ); t.push_back( 2 ); t.push_back( 3 ); t.push_back( 4 ); t.push_back( 5 ); rl->setRuleS( t );
t.clear(); t.push_back( 3 ); rl->setRuleB( t );
break;
}
/* just for test - should read from a file */
/* GLIDER */
wrd->set( 6, 1, 1 ); wrd->set( 7, 2, 1 );
wrd->set( 5, 3, 1 ); wrd->set( 6, 3, 1 );
wrd->set( 7, 3, 1 );
/* BLINKER */
wrd->set( 1, 3, 1 ); wrd->set( 2, 3, 1 );
wrd->set( 3, 3, 1 );
/******************************************/
generation();
}
private:
void display() {
system( "cls" );
int wid = wrd->wid(),
hei = wrd->hei();
cout << "+" << string( wid, '-' ) << "+\n";
for( int y = 0; y < hei; y++ ) {
cout << "|";
for( int x = 0; x < wid; x++ ) {
if( wrd->at( x, y ) ) cout << "#";
else cout << ".";
}
cout << "|\n";
}
cout << "+" << string( wid, '-' ) << "+\n";
cout << "Generation: " << gen << "\n\nPress [ENTER] for the next generation...";
cin.get();
}
void generation() {
do {
display();
rl->applyRules();
rl->swapWrds();
gen++;
}
while ( rl->hasLivingCells() );
}
rule* rl;
world* wrd;
int gen;
};
int main( int argc, char* argv[] ) {
int h,w;
cout<<"\nEnter height and width:\n";
cin>>h>>w;
cellular c( h,w );
cout << "\n*** CELLULAR AUTOMATA ***" << "\n\n Which do you want to run?\n\n\n";
cout << " [1]\tConway's Life\n [2]\tAmoeba\n [3]\tLife 34\n [4]\tMaze\n\n > ";
int o;
do {
cin >> o;
}
while( o < 1 || o > 4 );
cin.ignore();
c.start( o );
return system( "pause" );
}