Here is how our implementation will work:
(1) The program will ask the user to enter a filename from the console. (Just like the last assignment).
(2) 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.
(3) The game will be played on 10 by 28 two-dimensional array. (Can you guess why?). 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 what I need help on. I need help getting started using the rules to decided what to do with the live cells.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
//Global Variables
constint ROWS = 12;
constint COLS = 30;
constint BOARD_ROWS(10);
constint BOARD_COLS(28);
constchar LIVE = 'X'; //life cells
constchar DEAD = '.'; //dead cells
char NewBoard[ROWS][COLS];
char tempboard[ROWS][COLS];
//functions
void MakeArray(string filename, char board[][COLS]);
void GameBoard(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";
getline(cin,filename);
//call functions
cout<<"Make Array";
NextState(board);
//GameBoard(board);
//stop terminal window from quitting after programs ends
char q;
cin >> q;
return 0;
}
void MakeArray(string filename, char board[][COLS])
{
ifstream myfile;
myfile.open (filename.c_str());
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
myfile>>board[r][c];
}
}
myfile.close();
}
/*/void GameBoard (char board[][COLS])
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
//cout<<board[r][c];
}
//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.
/*/
if (board[r][c] == LIVE && LiveCnt < 2) //rule 1
{
tempboard[r][c]=board[r][c];
NewBoard[r][c]=tempboard[r][c];
NewBoard[r][c]==DEAD;
}
elseif (board[r][c]==LIVE && LiveCnt==2 || LiveCnt==3) //rule 2
{
tempboard[r][c]=board[r][c];
NewBoard[r][c]=tempboard[r][c];
NewBoard[r][c]==LIVE;
}
elseif (board[r][c]==LIVE && LiveCnt>3 ) //rule 3
{
tempboard[r][c]=board[r][c];
NewBoard[r][c]=tempboard[r][c];
NewBoard[r][c]==DEAD;
}
elseif (board[r][c]==DEAD && LiveCnt==3) //rule 4
{
tempboard[r][c]=board[r][c];
NewBoard[r][c]=tempboard[r][c];
NewBoard[r][c]==LIVE;
}
}
}
}
The problem I having is that it is displaying the contents of the file: life01.txt when there is no active cout statements.