Need help c++ assignment: Game of Life

He I am really stuck on this assignment. Here is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#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
//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";
	cin>>filename;

	//call functions
	MakeArray(filename, board);
//	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])
{

}


Here is the assignment:
Conway's Game of Life

For this assignment your 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 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.

Here is what file "life01.txt" looks like: http://pastebin.com/0ZNMbmzH
OOOOOOOOOOOOOOOOOOOOOOOOOOO000
0............................O
0............................O
0............................O
0..........XX................O
0..........XX................O
0............................O
0............................O
0............................O
0............................O
0............................O
0OOOOOOOOOOOOOOOOOOOOOOOOOOOOO



Here is what the teacher's output looks like:http://hypergrade.com/grader/view_solution.php?task_id=3304
First, find a live cell.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

#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
//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";
	cin>>filename;

	//call functions
	MakeArray(filename, board);
	GameBoard(board);
        NextState(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]){
    char board2[ROWS][COLS];
    for (int r=1; r<ROWS-1; r++)
    {
        for (int c=1; c<COLS-1; 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]==LIVE)
            {
                LiveCnt++;
            }
            if (board[r+1][c+1]==LIVE)
            {
                LiveCnt++;  
            }
 
        
        }
    }
   
}


How do I do that if statement (s) that apply the rules???
@saurabhgupta0527
I am not asking someone to do this for me, but to show me how to do it.

The last part is confusing for me.

I know I need an if statement that looks something like this:
"if the cell is live and does not have either 2 or 3 neighbors then set it to dead.


How do you put that in this form: if (----)?
Here my new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

#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 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;
			}
			else if (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;
			}
			else if (board[r][c]==LIVE && LiveCnt>3 ) //rule 3
			{
				tempboard[r][c]=board[r][c];
				NewBoard[r][c]=tempboard[r][c];
				NewBoard[r][c]==DEAD;
			}
			else if (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.

I am using dev c++ 5.1.0.0
Topic archived. No new replies allowed.