-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.cpp
228 lines (211 loc) · 4.8 KB
/
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
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
/*
* =====================================================================================
*
* Filename: life.cpp
*
* Description: Conway's game of life implementation because I never did this in
* highschool. Instead of copying the board I used a buffer to resuce the amount of
* memory used by the program. The basic idea is that every row that is two rows above
* the current row we can flush the boffer too. This way we only need to keep track of
* two extra rows in the buffer and flush them out the the game once that game row is not
* needed for calculations anymore.
*
* Version: 1.0
* Created: 11/08/2012 10:32:01 PM
* Revision: none
* Compiler: gcc
*
* Author: Ross Guarino(eos@csh.rit.edu)
* Organization: Computer Science House
*
* =====================================================================================
*/
#include <stdlib.h>
#include <iostream>
#include "life.h"
//Globals
const int HEIGHT = 10;
const int WIDTH = 30;
char board[HEIGHT][WIDTH];
char buffer[2][WIDTH];
using namespace std;
int main(int argc, char* argv[]){
/*
printBoard();
cout<<"----------------"<<endl;
generateGame();
cout<<"----------------"<<endl;
printBoard();
pushBuffer(0,0,0);
pushBuffer(0,1,0);
pushBuffer(0,2,0);
pushBuffer(0,3,0);
pushBuffer(0,4,0);
pushBuffer(0,5,0);
pushBuffer(0,6,0);
pushBuffer(0,7,0);
pushBuffer(0,8,0);
pushBuffer(0,9,0);
flushBuffer( 0);
cout<<"----------------"<<endl;
printBoard();
cout<<"----------------"<<endl;
gameTick();
printBoard();
cout<<"----------------"<<endl;
*/
//If no arguments are made play forever
if( argc == 1){
while( true){
int generation = 0;
generateGame();
bool empty = false;
while( generation <= 50 && !empty ){
gameTick();
cout<<"Generation: "<<generation<<endl;
generation++;
empty = printBoard();
usleep(500000);
}
}
}
//If an argument is given play the gam
}
void generateGame(){
srand ( time(NULL) );
for( int r=0; r<HEIGHT; r++){
for( int c=0; c<WIDTH; c++){
setBoard( r, c, rand()%2 );
}
}
for( int r=0; r<HEIGHT; r++){
for( int c=0; c<WIDTH; c++){
if( rand()%2 == 1){
setBoard( r, c, 0 );
}
}
}
}
int getBoard( int row, int column){
return board[row][column];
}
void setBoard( int row, int column, int value){
board[row][column] = value;
}
bool printBoard(){
bool empty = true;;
int value;
cout<<"----------"<<endl;
for( int r=0; r <HEIGHT; r++){
cout<<"|";
for( int c=0; c<WIDTH; c++){
value = getBoard( r, c);
if( value == 0){
cout<<" ";
}else{
cout<<"#";
}
if( value == 1){ empty = false;}
}
cout<<"|"<<endl;
}
cout<<"----------"<<endl;
return empty;
}
void pushBuffer(int row, int column, int value){
buffer[row][column] = value;
}
void flushBuffer(int row){
if( row >= 0){
for( int i=0;i<WIDTH; i++){
setBoard( row, i, buffer[0][i] );
buffer[0][i] = buffer[1][i];
}
}
else{
//If it is less than 0 then we know we are flushing the first time and we need to
//get rid of the old data
for( int i=0; i<WIDTH; i++){
buffer[0][i]= buffer[1][i];
}
}
}
void gameTick(){
int value;
int neighbors;
for( int r=0; r<HEIGHT; r++){
for( int c=0; c<WIDTH; c++){
value = getBoard(r, c);
neighbors = 0;
//cout<<"Starting to count neighbors"<<endl;
//Check north west
if( r>0 && c >0 && getBoard(r-1,c-1) == 1){
neighbors++;
}
//Check north
if( r>0 && getBoard(r-1,c) == 1){
neighbors++;
}
//Check north east
if(r>0 && c< WIDTH-1 && getBoard(r-1,c+1) == 1){
neighbors++;
}
//Check east
if( c<WIDTH-1 && getBoard( r,c+1) == 1){
neighbors++;
}
//Check south east
if( r<HEIGHT-1 && c <WIDTH-1 && getBoard(r+1,c+1) == 1){
neighbors++;
}
//Check south
if( r<HEIGHT-1 && getBoard(r+1,c) == 1){
neighbors++;
}
//Check south west
if( r<HEIGHT-1 && c > 0 && getBoard(r+1,c-1) == 1){
neighbors++;
}
//Check west
if( c>0 && getBoard(r,c-1) == 1){
neighbors++;
}
//cout<<"Finished counting neighbors"<<endl;
if(value == 1 && neighbors < 2){
//Not enough
//cout<<"L";
pushBuffer(1,c,0);
}
else if(value == 1 && neighbors == 2){
//Just right
//cout<<"2";
pushBuffer(1,c,1);
}
else if(value == 1 && neighbors == 3){
//Just right
//cout<<"3";
pushBuffer(1,c,1);
}
else if(value == 1 && neighbors > 3){
//Over crowded
//cout<<"O";
pushBuffer(1,c,0);
}
else if( value == 0 && neighbors == 3){
//Birth!
//cout<<"B";
pushBuffer(1,c,1);
}else{
//cout<<"E";
pushBuffer(1,c,0);
}
}
//cout<<"Finished pushing"<<endl;
//we need to flush here
flushBuffer(r-1);
//cout<<endl;
}
//cout<<"Finished"<<endl;
//Need to flush the last line
flushBuffer( HEIGHT-1);
}//gameTick();