| | |
Game Of Life help? Ive done most of it
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 7
Reputation:
Solved Threads: 0
Hey can someone help me finish my code? Im in a real stump here.
What Ive done so far is read and display the code from a 2d array, then I counted the neighbours that were dead and then did the process to make a new Generation.
However, I dont think Ive done it completely right. Like, how would I tell the function how many times I need to make the new generation, then display it?
Im so lost :s
What Ive done so far is read and display the code from a 2d array, then I counted the neighbours that were dead and then did the process to make a new Generation.
However, I dont think Ive done it completely right. Like, how would I tell the function how many times I need to make the new generation, then display it?
Im so lost :s
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; const int SIZE = 20; bool readGrid(string fileName, char grid[SIZE][SIZE]); void displayGrid(const char grid[SIZE][SIZE]); int countAlive(char grid[SIZE][SIZE]); void newGen(char grid[SIZE][SIZE]); // declare more functions if necessary int main() { string fileName; // the name of the grid file int nGenerations; // nGenerations is the number of generations char grid[SIZE][SIZE]; // a char array consiting of '*' or '.' // declare more variables if necessary char nuevo[SIZE][SIZE]; cout << "Enter the filename of the grid: " << flush; // don't modify cin >> fileName; // don't modify cout << endl; // don't modify readGrid(fileName, grid); displayGrid(grid); // add processing for reading and displaying the grid cout << endl << "How many generations in total? " << flush; // don't modify cin >> nGenerations; // don't modify // add processing for new grid generations if (nGenerations == 1) // don't modify cout << "This is the grid after " << nGenerations << " generation:" << endl << endl; // don't modify else // don't modify cout << "This is the grid after " << nGenerations << " generations:" << endl << endl; // don't modify // display the grid cout << endl; // don't modify system("pause"); // don't modify return 0; // don't modify } // implement all declared functions bool readGrid(string fileName, char grid[SIZE][SIZE]) { ifstream inData; inData.open(fileName.c_str()); if(inData) { for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { inData >> grid[row][col]; } } return true; } else return false; } void displayGrid(const char grid[SIZE][SIZE]) { for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { cout << grid[row][col] << " "; } cout << endl; } } int countAlive(char grid[SIZE][SIZE]) { int neighbour = 0; for(int row = 1; row < SIZE-1; row++) { for(int col = 1; col < SIZE-1; col++) { if(grid[row+1][col+1] == '*') neighbour++; if(grid[row+1][col] == '*') neighbour++; if(grid[row+1][col-1] == '*') neighbour++; if(grid[row][col-1] == '*') neighbour++; if(grid[row][col+1] == '*') neighbour++; if(grid[row-1][col-1] == '*') neighbour++; if(grid[row-1][col] == '*') neighbour++; if(grid[row-1][col+1] == '*') neighbour++; } } return neighbour; } void newGen(char grid[SIZE][SIZE]) { int neighbour = countAlive(grid); char nuevo[SIZE][SIZE]; grid[SIZE][SIZE] = nuevo[SIZE][SIZE]; for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { grid[row][col] = nuevo[row][col]; if(grid[row][col] == '*' && neighbour == 1) nuevo[row][col] == '*'; if(grid[row][col] == '.' && neighbour == 1) nuevo[row][col] == '.'; if(grid[row][col] == '*' && neighbour == 2) nuevo[row][col] == '*'; if(grid[row][col] == '.' && neighbour == 2) nuevo[row][col] == '*'; if(grid[row][col] == '*' && neighbour > 2) nuevo[row][col] == '.'; if(grid[row][col] == '.' && neighbour > 2) nuevo[row][col] == '.'; } } }
Last edited by Narue; Aug 26th, 2008 at 12:15 pm. Reason: Added code tags
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
What happens when you run it? I'm guessing you are possibly getting a segmentation fault error in line 122 below due to your attempt to access element
grid[SIZE][SIZE] . Arrays in C++ start at 0 and go to SIZE - 1. Or are you trying to do a deep copy of the entire array here? If so, that line isn't going to do it. C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; const int SIZE = 20; bool readGrid(string fileName, char grid[SIZE][SIZE]); void displayGrid(const char grid[SIZE][SIZE]); int countAlive(char grid[SIZE][SIZE]); void newGen(char grid[SIZE][SIZE]); // declare more functions if necessary int main() { string fileName; // the name of the grid file int nGenerations; // nGenerations is the number of generations char grid[SIZE][SIZE]; // a char array consiting of '*' or '.' // declare more variables if necessary char nuevo[SIZE][SIZE]; cout << "Enter the filename of the grid: " << flush; // don't modify cin >> fileName; // don't modify cout << endl; // don't modify readGrid(fileName, grid); displayGrid(grid); // add processing for reading and displaying the grid cout << endl << "How many generations in total? " << flush; // don't modify cin >> nGenerations; // don't modify // add processing for new grid generations if (nGenerations == 1) // don't modify cout << "This is the grid after " << nGenerations << " generation:" << endl << endl; // don't modify else // don't modify cout << "This is the grid after " << nGenerations << " generations:" << endl << endl; // don't modify // display the grid cout << endl; // don't modify system("pause"); // don't modify return 0; // don't modify } // implement all declared functions bool readGrid(string fileName, char grid[SIZE][SIZE]) { ifstream inData; inData.open(fileName.c_str()); if(inData) { for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { inData >> grid[row][col]; } } return true; } else return false; } void displayGrid(const char grid[SIZE][SIZE]) { for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { cout << grid[row][col] << " "; } cout << endl; } } int countAlive(char grid[SIZE][SIZE]) { int neighbour = 0; for(int row = 1; row < SIZE-1; row++) { for(int col = 1; col < SIZE-1; col++) { if(grid[row+1][col+1] == '*') neighbour++; if(grid[row+1][col] == '*') neighbour++; if(grid[row+1][col-1] == '*') neighbour++; if(grid[row][col-1] == '*') neighbour++; if(grid[row][col+1] == '*') neighbour++; if(grid[row-1][col-1] == '*') neighbour++; if(grid[row-1][col] == '*') neighbour++; if(grid[row-1][col+1] == '*') neighbour++; } } return neighbour; } void newGen(char grid[SIZE][SIZE]) { int neighbour = countAlive(grid); char nuevo[SIZE][SIZE]; grid[SIZE][SIZE] = nuevo[SIZE][SIZE]; for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { grid[row][col] = nuevo[row][col]; if(grid[row][col] == '*' && neighbour == 1) nuevo[row][col] == '*'; if(grid[row][col] == '.' && neighbour == 1) nuevo[row][col] == '.'; if(grid[row][col] == '*' && neighbour == 2) nuevo[row][col] == '*'; if(grid[row][col] == '.' && neighbour == 2) nuevo[row][col] == '*'; if(grid[row][col] == '*' && neighbour > 2) nuevo[row][col] == '.'; if(grid[row][col] == '.' && neighbour > 2) nuevo[row][col] == '.'; } } }
•
•
Join Date: Aug 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
What happens when you run it? I'm guessing you are possibly getting a segmentation fault error in line 122 below due to your attempt to access elementgrid[SIZE][SIZE]. Arrays in C++ start at 0 and go to SIZE - 1. Or are you trying to do a deep copy of the entire array here? If so, that line isn't going to do it.
I think my main problem is I dont know how to tell it how many times to make a new generation, like once, twice or up to eight times. And then how to display the new grid. Wah, lol.....
•
•
Join Date: Aug 2008
Posts: 7
Reputation:
Solved Threads: 0
Ok well I added a new function to swap the arrays, I think that is better now :s
C++ Syntax (Toggle Plain Text)
void swapArray(char grid[SIZE][SIZE], char nuevo[SIZE][SIZE]) { for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { grid[row][col] == nuevo[row][col]; } } } void newGen(char grid[SIZE][SIZE]) { int neighbour = countAlive(grid); char nuevo[row][col] = swapArray(grid, nuevo); for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { if(grid[row][col] == '*' && neighbour == 1) nuevo[row][col] == '*'; if(grid[row][col] == '.' && neighbour == 1) nuevo[row][col] == '.'; if(grid[row][col] == '*' && neighbour == 2) nuevo[row][col] == '*'; if(grid[row][col] == '.' && neighbour == 2) nuevo[row][col] == '*'; if(grid[row][col] == '*' && neighbour > 2) nuevo[row][col] == '.'; if(grid[row][col] == '.' && neighbour > 2) nuevo[row][col] == '.'; } }
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
•
•
•
•
Ok well I added a new function to swap the arrays, I think that is better now :s
C++ Syntax (Toggle Plain Text)
void swapArray(char grid[SIZE][SIZE], char nuevo[SIZE][SIZE]) { for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { grid[row][col] == nuevo[row][col]; } } }
== to = . Also, which one is the source array and which one is the destination array? What are you copying into what?•
•
•
•
C++ Syntax (Toggle Plain Text)
void newGen(char grid[SIZE][SIZE]) { int neighbour = countAlive(grid); char nuevo[row][col] = swapArray(grid, nuevo); for(int row = 0; row < SIZE; row++) { for(int col = 0; col < SIZE; col++) { if(grid[row][col] == '*' && neighbour == 1) nuevo[row][col] == '*'; if(grid[row][col] == '.' && neighbour == 1) nuevo[row][col] == '.'; if(grid[row][col] == '*' && neighbour == 2) nuevo[row][col] == '*'; if(grid[row][col] == '.' && neighbour == 2) nuevo[row][col] == '*'; if(grid[row][col] == '*' && neighbour > 2) nuevo[row][col] == '.'; if(grid[row][col] == '.' && neighbour > 2) nuevo[row][col] == '.'; } }
C++ Syntax (Toggle Plain Text)
char nuevo[?][?]; swapArray(grid, nuevo); // make sure this order matches the order // in swapArray and that you are copying // the correct array into the correct array
Edit : To clarify, are you trying to SWAP the arrays or COPY one array into the other?
Last edited by VernonDozier; Aug 26th, 2008 at 9:06 pm.
![]() |
Similar Threads
- great books thread (Geeks' Lounge)
- game problem need help (Game Development)
- Computer In-game freeze (Troubleshooting Dead Machines)
- Radeon 9700 pro only runs halife life 2 in directx 8.1? (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: socket programming
- Next Thread: Memory leaks!
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop developer directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






