summaryrefslogtreecommitdiffstats
path: root/2.01.TheGameOfLife.cc
blob: 9e9431f5122cd16df10c1ca1c2cec5a27b4f8193 (plain)
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
/*
 * =====================================================================================
 *
 *       Filename:  2.01.TheGameOfLife.cc
 *
 *    Description:  Conways game of life
 *
 *        Version:  1.0
 *        Created:  07.04.2014 09:36:39
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de
 *   Organization:  FH Südwestfalen, Iserlohn
 *
 * =====================================================================================
 */

#include	<cstdlib>
#include	<iostream>
#include	<ctime>

using namespace std;

//-----------------------------------------------------------------------------
//  Typedefs
//-----------------------------------------------------------------------------
typedef unsigned int uint;

//-----------------------------------------------------------------------------
//  Prototypes
//-----------------------------------------------------------------------------
void  zufallsbelegung( int **feld, uint hoehe, uint breite );
uint  next_generation( int **feld1, int **feld2, uint breite, uint hoehe );
void  print_feld( int **feld, uint hoehe, uint breite );
int** new_int_matrix( int rows, int columns );
void  delete_int_matrix( int **m );

// ===  FUNCTION  ======================================================================
//         Name:  main
//  Description:  
// =====================================================================================
int main ( int argc, char *argv[] )
{
	// Makes rand actually random
	srand(time(NULL));

	// Helper variables
	int **feld1,
		**feld2;
	uint hoehe = 0,
		 breite = 0,
		 generation = 0,
		 population = 0;
	char menu;

	// Get the field size
	do{
		if(cin.fail()){
			cin.clear();
			cin.ignore(1000,'\n');
		}
		cout << "Hoehe: ";
	}while(!(cin >> hoehe));
	do{
		if(cin.fail()){
			cin.clear();
			cin.ignore(1000,'\n');
		}
		cout << "Breite: ";
	}while(!(cin >> breite));
	if(cin.fail()){
		cin.clear();
		cin.ignore(1000,'\n');
	}
	
	// Initilize the fields
	feld1 = new_int_matrix( hoehe+2, breite+2 );
	feld2 = new_int_matrix( hoehe+2, breite+2 );

	// Randomize feld1
	zufallsbelegung( feld1, hoehe, breite );

	// The next generation loop
	do{
		// Increment the generation counter
		generation++;

		// Calculate the next generation
		population = next_generation( feld1, feld2, breite, hoehe );

		// Print next generation
		print_feld( feld1, breite, hoehe );

		// Print stats for the field
		cout << "[" << hoehe 
			 << "],[" << breite 
			 << "] Generation " << generation
			 << " / Population " << population << " / ";
		// Ask user for comfirmation
		cout << "q = Quit, else next generation" << endl;
		cin.get(menu);

	}while(menu != 'q');
	
	// Free memory
	delete_int_matrix(feld1);
	delete_int_matrix(feld2);

	return EXIT_SUCCESS;
}				// ----------  end of function main  ---------- 

// ===  FUNCTION  ======================================================================
//         Name:  zufallsbelegung
//  Description:  Randomizes the feld inside the hoehe and breite boundaries
// =====================================================================================
void  zufallsbelegung( int **feld, uint hoehe, uint breite ){
	// Inserts 0 into the left and right side
	for(uint i = 0; i <= hoehe+1; i++){
		feld[i][0] = 0;
		feld[i][hoehe+1] = 0;
	}
	// Inserts 0 into the upper and lower side
	for(uint i = 0; i <= breite+1; i++){
		feld[0][i] = 0;
		feld[breite+1][i] = 0;
	}
	// Insert random 0, 1 into the inner field
	for(uint i = 1; i <= hoehe; i++){
		for(uint j = 1; j <= breite; j++){
			feld[i][j] = rand() % 2;
		}
	}
}

// ===  FUNCTION  ======================================================================
//         Name:  next_generation
//  Description:  Calculates the next generation applying conways rules
//                The source is feld1 and the destination is feld2
//                Hoehe and breite are the boundaries
// =====================================================================================
uint  next_generation( int **feld1, int **feld2, uint breite, uint hoehe ){
	// Declare helper variables
	int livingNeighbour = 0;
	uint population = 0;
	// Inserts 0 into the left and right side
	for(uint i = 0; i <= hoehe+1; i++){
		feld2[i][0] = 0;
		feld2[i][hoehe+1] = 0;
	}
	// Inserts 0 into the upper and lower side
	for(uint i = 0; i <= breite+1; i++){
		feld2[0][i] = 0;
		feld2[breite+1][i] = 0;
	}
	for(uint i = 1; i <= hoehe; i++){
		for(uint j = 1; j <= breite; j++){
			// Calculates the living neighbours
			livingNeighbour = 0;
			for(int k = -1; k <= 1; k++){
				for(int l = -1; l <= 1; l++){
					if( k != 0 && l != 0 ){
						livingNeighbour += feld1[i+k][j+l];
					}
				}
			}
			// Now apply conways rules
			// Defaults to copying
			feld2[i][j] = feld1[i][j];
			// No need for cecking actual state, because there are only 2 states
			// Dead cells with 3 neighbours will be ressurected
			if( livingNeighbour == 3 ){
				feld2[i][j] = 1;
			}
			// A living cell with less than 2 neighbours or with more than 3 dies
			if( livingNeighbour < 2 || livingNeighbour > 3){
				feld2[i][j] = 0;
			}
			// Count living cells
			if( feld2[i][j] == 1 ) population++;
		}
	}
	// Now frees memory and copys field
	for( uint i = 1; i <= hoehe; i++){
		for( uint j = 1; j <= breite; j++){
			feld1[i][j] = feld2[i][j];
		}
	}
	// Now return population
	return population;
}

// ===  FUNCTION  ======================================================================
//         Name:  print_feld
//  Description:  Prints the feld inside the boundaries hoehe and breite
// =====================================================================================
void  print_feld( int **feld, uint hoehe, uint breite ){
	for(uint i = 1; i <= hoehe; i++){
		for(uint j = 1; j <= breite; j++){
			if(feld[i][j] == 1){
				cout << '#';
			}
			else{
				cout << ' ';
			}
		}
		cout << endl;
	}
}

// ===  FUNCTION  ======================================================================
//         Name:  new_int_matrix
//  Description:  Allocate a dynamic int-matrix of size rows*columns; return a pointer.
// =====================================================================================
int** new_int_matrix( int rows, int columns ){
	int   i;
	int **m;
	m     = new int* [rows]; 			// allocate pointer array
	*m    = new int [rows*columns](); 	// allocate data array; initialize!
	for ( i=1; i<rows; i+=1 ) 			// set pointers
		m[i] = m[i-1] + columns;
	return m;
}

// ===  FUNCTION  ======================================================================
//         Name:  delete_int_matrix
//  Description:  Free a dynamic int-matrix.
// =====================================================================================
void  delete_int_matrix( int **m ){
	delete[] *m; 						// delete data array
	delete[]  m; 						// delete pointer array
}