Need help fixing Game Of Life Problem
Mar 3, 2012 at 7:14pm UTC
Here is my 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
#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 MakeArray(string filename, char board[][COLS])
{
ifstream myfile;
myfile.open (filename.c_str());
if (!myfile)
{
cout<< "File not found\n";
}
else
{
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])
{
//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]==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
{
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];
}
}
}
For some reason it is only passes test cases 1, 5, and 7 (3 out of 7):
hypergrade:
http://hypergrade.com/grader/view_solution.php?task_id=3304
Mar 3, 2012 at 7:39pm UTC
You aren't your neighbour.
1 2 3 4 5 6 7
bool MakeArray(string filename, char board[][COLS])
{
//...
if (!myfile) return true ; //an error
//...
return false ; //everything was OK
}
weird.
Mar 3, 2012 at 8:43pm UTC
You aren't your neighbour.
1 2 3 4 5 6 7
bool MakeArray(string filename, char board[][COLS])
{
//...
if (!myfile) return true ; //an error
//...
return false ; //everything was OK
}
weird.
What's weird?
Here is an example of my problem.
In case case 6 we use life06.txt which looks like this:
http://pastebin.com/fdjWy3yF
In the case case all the LIVE cells (i.e. 'X') die, but in my output they don't
Mar 3, 2012 at 10:43pm UTC
It's weird that you return
true
in case of error.
In the case case all the LIVE cells (i.e. 'X') die, but in my output they don't
That's because you are add yourself in the count
Last edited on Mar 3, 2012 at 10:44pm UTC
Mar 3, 2012 at 11:33pm UTC
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#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.
/*/
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];
}
}
}
output:
http://i39.tinypic.com/35lyut1.png
Mar 4, 2012 at 12:09am UTC
Not at home yet, but two things
1. You are missing a check if (board[r+1][c] ==LIVE)
2. You will be indexing outside the memory of board when r=0 and you substact 1 and the same for c=0 and when you are at the max of both r & c when you add 1. Example if (board[r-1][c-1]==LIVE)
Mar 4, 2012 at 12:42am UTC
2. You will be indexing outside the memory of board when r=0 and you substact 1 and the same for c=0 and when you are at the max of both r & c when you add 1. Example if (board[r-1][c-1]==LIVE)
What do you mean?
Also how to I get my output to match???
Thanks!
Mar 4, 2012 at 3:07am UTC
how to I get my output to match???
Here is what you need to get the first file to work:
1 2 3 4
// Bad
board[r][c]==NewBoard[r][c]; // <---- Bad line 167
// Good
board[r][c]=NewBoard[r][c]; // <---- Good line 167
In doing this you need to add a new line of code
1 2 3
// Add this line right before the first rule check
NewBoard[r][c] = DEAD; // <--------------------- New line of code
if (board[r][c] == LIVE && LiveCnt < 2) //rule 1 <---- Line 134
When you are looping over the rows and columns you start at 0 for both r and c. The statement of
board[r-1][c-1]
is the same as
board[-1][-1]
which is not memory owned by board. Right now your output looks OK.
Mar 4, 2012 at 3:12am UTC
Thanks.
Topic archived. No new replies allowed.