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
down the middle and on the second reiteration where it should be
horizontally,
mine outputs
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.