Conway's Game of Life Help

Hey everyone,

I am making Conway's Game of Life. Here is what the assignment says:

For this assignment you are to write a program, that plays Conway's game of Life.

See the Wikipedia definition, if you have never played the game:

http://en.wikipedia.org/wiki/Conway's_Game_of_Life.

Here is how our implementation will work:

1. The initial configuration will be read in from a file, which will be a 12 by 30 two-dimensional array of characters. The game board will be surrounded by all O's.

2. The game will be played on 10 by 28 two-dimensional array. (Can you guess why?).

3. A period ('.') will represent a dead cell and an 'X' will represent a live cell.

You will be severely penalized if your program does not have at least three functions.

Here is my code:


#include "stdafx.h"


#include <iostream>
#include <string>
#include <fstream>


using namespace std;

//Global Variables
const int ROWS = 12;
const int COLS = 30;
const int BOARD_ROWS(10);
const int BOARD_COLS(28);
const char LIVE = 'X'; //life cells
const char DEAD = '.'; //dead cells
char NewBoard[ROWS][COLS];
char quit;

//functions
bool MakeArray(string filename, char board[][COLS]);
void GameBoard(char board[][COLS]);
void SwitchBoard (char board[][COLS]);
void NextState(char board[][COLS]);

int main()
{
	char board [ROWS][COLS];
	string filename; //Name of the file
	cout<<"Enter the filename: \n";
	cin>>filename;
	//cout<<endl;

	//call functions
	if (MakeArray(filename, board))
{
	cout << "File not found.\n\n";
	cin>>quit; //stops from quiting
	return 1;
}
	//start loop
	cout<<endl;
	for (int l = 0; l<10; l++)
	{
		NextState(board);
		GameBoard(board);
		SwitchBoard(board);
	}
	//end loop

	//stop terminal window from quitting after programs ends
	char q;
	cin >> q;

	return 0;
}


bool MakeArray(string filename, char board[][COLS])
{
	ifstream myfile;
	myfile.open (filename.c_str());
	if (!myfile) return true;

    for (int r=0; r<ROWS; r++)
	{
		for (int c=0; c<COLS; c++)
		{
			myfile>>board[r][c];
		}
	}
	myfile.close();

	return false;
}


void GameBoard (char board[][COLS])
{
//cout<<endl;
	for (int r=1; r<=ROWS-2; r++)
	{
		for (int c=1; c<=COLS-2; c++)
		{

			cout<<board[r][c];
		}
		cout<<endl;
	}
	cout<<endl;
}
void NextState (char board[][COLS])
{

	for (int r=0; r<ROWS; r++)
	{
		for (int c=0; c<COLS; c++)
		{

			int LiveCnt=0;
			if (board[r-1][c-1]==LIVE)
			{
				LiveCnt++;
			}
			if (board[r-1][c]==LIVE)
			{
				LiveCnt++;
			}
			if (board[r-1][c+1]==LIVE)
			{
				LiveCnt++;
			}
			if (board[r][c-1]==LIVE)
			{
				LiveCnt++;
			}

			if (board[r][c+1]==LIVE)
			{
				LiveCnt++;
			}
			if (board[r+1][c-1]==LIVE)
			{
				LiveCnt++;
			}
			if (board[r+1][c+1]==LIVE)
			{
				LiveCnt++;
			}
/*/
Rules:
1.	Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2.	Any live cell with two or three live neighbours lives on to the next generation.
3.	Any live cell with more than three live neighbours dies, as if by overcrowding.
4.	Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
/*/

			NewBoard[r][c] = DEAD;

			if (board[r][c] == LIVE && LiveCnt < 2) //rule 1
			{

				NewBoard[r][c]=DEAD;
			}
			else if (board[r][c]==LIVE && (LiveCnt==2 || LiveCnt==3))//rule 2
			{

				NewBoard[r][c]=LIVE;
			}
			else if (board[r][c]==LIVE && LiveCnt>3 ) //rule 3
			{

				NewBoard[r][c]=DEAD;
			}
			else if (board[r][c]==DEAD && LiveCnt==3) //rule 4
			{

				NewBoard[r][c]=LIVE;
			}
		}
	}



}
void SwitchBoard(char board[][COLS])
{

	for (int r=0; r<ROWS; r++)
	{
		for (int c=0; c<COLS; c++)
		{
			board[r][c]=NewBoard[r][c];
		}
	}

}


Here is the contains of the plain text file its supposed to read from, titled life01, a .txt file.


OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
O............................O
O............................O
O............................O
O..........XX................O
O..........XX................O
O............................O
O............................O
O............................O
O............................O
O............................O
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO


life02.txt

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
O............................O
O............................O
O.........X..................O
O.........X..................O
O.........X..................O
O............................O
O............................O
O............................O
O............................O
O............................O
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO


life03.txt

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
O............................O
O............................O
O............................O
O.........XX.................O
O........XX..................O
O.........X..................O
O............................O
O............................O
O............................O
O............................O
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO



life04.txt

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
O............................O
O............................O
O............................O
O.........X..................O
O........XXX.................O
O........X.X.................O
O.........X..................O
O............................O
O............................O
O............................O
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO



life05.txt

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
O............................O
O............................O
O............................O
O........XX..................O
O.......X..X.................O
O........XX..................O
O............................O
O............................O
O............................O
O............................O
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO



life06.txt

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
O............................O
O........X...................O
O.........X..................O
O..........X.................O
O...........X................O
O............X...............O
O.............X..............O
O............................O
O............................O
O............................O
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO



The problem I'm having is, the first life01.txt file runs fine, however, the other files such as 2,3,4,5, and 6 seem to have problems. Our professor did not post the solutions of what the output should look like for the reiterations. However, I did find solutions from online and that's how I know something is wrong with my code. Like, for example in life02.txt it shows

x
x
x

down the middle and on the second reiteration where it should be

xxx

horizontally,

mine outputs

x.x


horizontally,

and from there everything is periods. Can anyone help me to find out what is wrong with my code regarding to the rules of the game of life which can be found in the wiki link above? Also, I would appreciate it if someone could also run my code to see what the problem is.

Also, how do I make my .txt files be able to be read from the desktop of my computer as well? The program only is able to find and read it when they are placed in my Visual 2012 folder which is the compiler I'm using. If I move the .txt files to the desktop, how do I make the program find it and read it from there as well? Because right now, if I move the .txt files to the desktop, the program says it cannot find and read the file unless the .txt files are in my Visual 2012 folder.

Thank you very much.

Here is the solutions that I found online:

http://hypergrade.com/grader/view_solution.php?task_id=3304

Thanks a lot for any help, I greatly appreciate it.
Last edited on
Any help is appreciated.
Each cell has 8 neighbors. You're only checking 7 neighbors.
@cire

Oh I see, thank you. I checked my combinations and when I look back at the void NextState function, I can see that there are in fact 7 instead of 8. However, I'm not sure which combination I am missing between [r] and [c], when I look back at it, it seems like I have every combination. What would the 8th combination be to make the program check 8 neighbors?

Thanks again.
Last edited on
This kind of problem has an easy fix. Draw a box like this:
1
2
3
4
5
6
7
8
9

|           -1      0       1     
|   -1:   -1,-1 / -1,0   / -1, 1 
|  
|    0:   0,-1  / 0, 0   /  0, 1
|   
|    1:   1,-1  /  1,0   /  1, 1 
|
increasing y 


So to access top middle is [x][y - 1] for example. Anyway, by drawing it out like this, you can see what is missing. I'm finding myself often drawing out complex situations in game programming and then it can easily be seen what must be missing or what must be done.
Last edited on
@Mats

Thanks so much for your diagram, it really helped me! I was missing [r+1] [c], now everything works great. I just have one more question about opening the file from anywhere on the computer, such as the desktop, etc.

When the program starts, it asks the user to input the file name of the .txt file, however, how do I make my .txt files be able to be read from the desktop of my computer as well? The program only is able to find and read it when they are placed in my Visual 2012 folder which is the compiler I'm using. If I move the .txt files to the desktop, how do I make the program find it and read it from there as well? Because right now, if I move the .txt files to the desktop, the program says it cannot find and read the file unless the .txt files are in my Visual 2012 folder.
Anyone can feel free to help as well, thanks.
Last edited on
filepath

If you do type "input.txt", the ifstream::open looks from the current working directory (the "VS2012 folder").
If you would type "..\input.txt", the file should be in the parent folder of current working directory.
If you would type "C:\Users\omiexstrike\input.txt", the file should be in your "home".

Read about absolute and relative paths.
If you distribute a game, you include resources with it. For example, if you get some commercial game and then download a map for it, you must put it in folder C:\Program Files\GameFolder\Maps. So for your game, tell users that new start positions go in folder C:\yourgamepath\startpositions and then tell your code to look in that path for start positions.
Topic archived. No new replies allowed.