summaryrefslogtreecommitdiffstats
path: root/2.01.TheGameOfLife.cc
diff options
context:
space:
mode:
Diffstat (limited to '2.01.TheGameOfLife.cc')
-rw-r--r--2.01.TheGameOfLife.cc232
1 files changed, 232 insertions, 0 deletions
diff --git a/2.01.TheGameOfLife.cc b/2.01.TheGameOfLife.cc
new file mode 100644
index 0000000..9e9431f
--- /dev/null
+++ b/2.01.TheGameOfLife.cc
@@ -0,0 +1,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
+}