Conway's Game of Life

This is a discussion on Conway's Game of Life within the C++ Programming forums, part of the General Programming Boards category; I am fully aware that this is a fairly common assignment and that I'm sure it's been posted many time, ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    1

    Conway's Game of Life

    I am fully aware that this is a fairly common assignment and that I'm sure it's been posted many time, but I'm having trouble getting the generations to function properly. I've got the entire program coded and it runs, but it doesn't quite run right.

    Code:
      6 #include <iostream>
      7 #include <iomanip>
      8 #include <fstream>
      9 #include <stdlib.h>
     10 #include <unistd.h>
     11 #include <time.h>
     12 #include "ansi.h"
     13
     14 using namespace std;
     15
     16 #define GRID_WIDTH 80
     17 #define GRID_HEIGHT 24
     18 #define DELAY 500000
     19
     20 void initGrid(char grid[][GRID_WIDTH]);
     21 void initGrid(const char* fileName, char [][GRID_WIDTH]);
     22 void renderGrid(char [][GRID_WIDTH]);
     23 void nextGeneration(char cur[][GRID_WIDTH], char next[][GRID_WIDTH]);
     24
     25 int main(int argc, char **argv)
     26 {
     27   char grid1[GRID_HEIGHT][GRID_WIDTH];
     28   char grid2[GRID_HEIGHT][GRID_WIDTH];
     29   int generation = 1;
     30
     31   //initialize the grid
     32   if(argc==2)
     33   {
     34     initGrid(argv[1], grid1);
     35   }
     36   else
     37   {
     38     initGrid(grid1);
     39   }
     40
     41   //run the generations, until the user presses ctrl+c
     42   while(true)
     43   {
     44     //show the appropriate grid, and generate the next population
     45     if(generation++ % 2)
     46     {
     47       //odd numbered generation
     48       renderGrid(grid1);
     49       nextGeneration(grid1, grid2);
     50     }
     51     else
     52     {
     53       //even numbered generation
     54       renderGrid(grid2);
     55       nextGeneration(grid2, grid1);
     56     }
     57
     58     //wait awhile before doing it again
     59     usleep(DELAY);
     60   }
     61
     62   return 0;
     63 }
     64
     65 //initialize the grid with random cells
     66 void initGrid(char grid[][GRID_WIDTH])
     67 {
     68   //get the random number seeded with our present time
     69   srand(time(0));
     70
     71   //loop through each row
     72   for(int y=0; y < GRID_HEIGHT; y++)
     73   {
     74     //loop through each column
     75     for(int x=0; x < GRID_WIDTH; x++)
     76     {
     77       //1/3 of the cells are alive
     78       grid[y][x] = (rand() % 3) == 1;
     79     }
     80   }
     81 }
     82
     83 //initialize the grid from a file
     84 void initGrid(const char* fileName, char grid[][GRID_WIDTH])
     85 {
     86   ifstream file;
     87   char c;
     88
     89   file.open(fileName);
     90
     91   //we are just going to assume that the file is valid
     92   //this is bad but in the interest of time, its okay
     93   for(int y=0; y < GRID_HEIGHT; y++)
     94   {
     95     for(int x=0; x < GRID_WIDTH; x++)
     96     {
     97       c=file.get();
     98
     99       if(c=='\n')
    100       {
    101         c=file.get();
    102       }
    103
    104       grid[y][x]= c =='*';
    105     }
    106   }
    107
    108   file.close();
    109 }
    110
    111 //render the grid
    112 void renderGrid(char grid[][GRID_WIDTH])
    113 {
    114   //loop through the grid, rendering as we go
    115   for(int y=0; y < GRID_HEIGHT; y++)
    116   {
    117     for(int x=0; x < GRID_WIDTH; x++)
    118     {
    119       //go to the position
    120       cout << cursorPosition(x+1, y+1);
    121
    122       //print the ' ' or '*'
    123       cout << (grid[y][x] ? '*' : ' ');
    124     }
    125   }
    126 }
    127
    128 //create the next generation. The cur grid generates the next grid
    129 void nextGeneration(char cur[][GRID_WIDTH], char next[][GRID_WIDTH])
    130 {
    131
    132   int count;
    133
    134
    135   for(int y=0; y < GRID_HEIGHT; y++)
    136   {
    137     for(int x=0; x < GRID_WIDTH; x++)
    138     {
    139       count = 0;
    140
    141       //checks grid for asteriks
    142       if(cur[y - 1][x] == '*')
    143       {
    144         count += 1;
    145       }
    146       else if(cur[y][x - 1])
    147       {
    148         count += 1;
    149       }
    150       else if(cur[y + 1][x])
    151       {
    152         count += 1;
    153       }
    154       else if(cur[y][x + 1])
    155       {
    156         count += 1;
    157       }
    158       else if(cur[y - 1][x - 1])
    159       {
    160         count += 1;
    161       }
    162       else if(cur[y + 1][x + 1])
    163       {
    164         count += 1;
    165       }
    166       else if(cur[y - 1][x + 1])
    167       {
    168         count += 1;
    169       }
    170       else if(cur[y + 1][x - 1])
    171       {
    172         count += 1;
    173       }
    174
    175
    176       //less than 2 living neighbors - cell dies: starvation
    177       if((cur[y][x] == '*') && (count < 2))
    178       {
    179         next[y][x] = ' ';
    180         cout << "h" << "\n";
    181       }
    182       //more than 3 living neighbors - cell dies: overcrowding
    183       else if((cur[y][x] == '*') && (count > 3))
    184       {
    185         next[y][x] = ' ';
    186         cout << "i" << "\n";
    187       }
    188       //2 or 3 living neighbors - cell stays alive: stability
    189       else if((cur[y][x] == '*') && count == 2 || count == 3)
    190       {
    191         next[y][x] = '*';
    192         cout << "j" << "\n";
    193       }
    194       //dead cell with exactly 3 neighbors - cell is reborn: reproduction
    195       else if((cur[y][x] == ' ') && count == 3)
    196       {
    197         next[y][x] = '*';
    198         cout << "j" << "\n";
    199       }
    200
    201     }
    202   }
    203 }
    Also, for anyone wondering what exactly my code is doing and why I feel as though it's not running correctly, it's doing this:
    http://i.imgur.com/O6B0Kfc.gif

    I'm not asking anyone to do this assignment for me, I'm just wondering if anyone can point me in the direction that I need to go to in order to fix it. Any help would be greatly appreciated!
    Last edited by trdains; 01-25-2014 at 01:07 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Conway's Game of Life - Wikipedia, the free encyclopedia
    Rather than a whole mess of random data, start with a single glider and see if it moves like it's supposed to.

    Also, you should try and edit your post to remove the double line numbers. You don't need line numbers when you post, the board will add them for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conway's game of life in parallel
    By std10093 in forum C Programming
    Replies: 15
    Last Post: 07-25-2013, 01:01 PM
  2. Conway's Game of Life using pointers!
    By john_member in forum C Programming
    Replies: 2
    Last Post: 03-10-2013, 11:21 PM
  3. Conway's Game of Life
    By Anhur in forum C Programming
    Replies: 37
    Last Post: 09-27-2012, 10:54 PM
  4. Text Based Conway's game of Life in C
    By spideysagnik in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2011, 07:10 AM
  5. John Conway's Game Of Life
    By O Mighty Nips in forum C Programming
    Replies: 3
    Last Post: 05-14-2011, 11:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21