| | |
Game of life i/o problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Just registered but you guys have been a big help for the whole semester. I have what seems to be a simple problem but I'm just stuck. My assignment is a game of life sim that prompts the user for an input file, and then reads integers from the file that represent coordinates of the cells (row/column). After that, it does its thing until the user decides it should stop. I designed it originally to get the values from the keyboard (enter two integers between 1-20 with a space inbetween until negatives are read) because I thought it would be easy to change my cin's to fin's but I'm having some trouble. As of now, the file seems to open but the grid is blank. Here's the section of relevant code:
And here's how the input file is setup:
1 2
3 3
5 5
13 4
17 5
12 12
5 19
-1 -1
Any help would be appreciated. Thanks in advance.
C++ Syntax (Toggle Plain Text)
int main() { ifstream datafile; string filename; cout << "The input values from the file will begin the first generation." << endl << endl; cout << "Follow the prompts to create new generations or terminate the program." << endl << endl; // Initilize the grid to all dead cells bool Grid[Size][Size]; for (int i=0; i < Size; i++) for (int j=0; j < Size; j++) Grid[i][j] = false; int row, col; // Prompt user for filename. cout << "Please enter the file name containing input values. " << endl; cout << endl; cin >> filename; cout << fixed << showpoint; datafile.open(filename.c_str()); if (!datafile) { cout << "File not found" << endl; return 1; } while (datafile) { datafile >> row >> col; } if ( row>=0 && row<Size && col>=0 && col<Size) // if legal row-col Grid[row][col] = true; while ( row != -1); displayGrid(Grid);
And here's how the input file is setup:
1 2
3 3
5 5
13 4
17 5
12 12
5 19
-1 -1
Any help would be appreciated. Thanks in advance.
C++ Syntax (Toggle Plain Text)
while (datafile) { datafile >> row >> col; } if ( row>=0 && row<Size && col>=0 && col<Size) // if legal row-col Grid[row][col] = true;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Thanks a lot. It's reading the file just fine now but I noticed another problem after running through a couple generations. I neglected to include a command to populate a cell with three neighbors. No kids means everyone just slowly dies out. Here's the relevant code (let me know if you need more):
I'm really not sure how to tackle the problem and any ideas would be helpful.
Thanks in advance and thanks again Ancient Dragon.
C++ Syntax (Toggle Plain Text)
void nextGen(bool World[][Size], bool tempWorld[][Size]) { int neighborcount; for (int i=0; i < Size; i++) for (int j=0; j < Size; j++) { neighborcount = getNeighborCount(World,i,j); if (neighborcount == 2) // 2 neighbors tempWorld[i][j] = World[i][j]; // stays alive. else if (neighborcount == 3) tempWorld[i][j] = true; // 3 neighbors: // stays alive else tempWorld[i][j] = false; // 1 and 4+ neighbors: } // dead // copy tempWorld to World for (int i=0; i < Size; i++) for (int j=0; j < Size; j++) World[i][j] = tempWorld[i][j]; } int getNeighborCount(const bool World[][Size], int row, int col) // Counts neighbors in a row/column pair { int count=0; if (row > 0) { if (col > 0 && World[row-1][col-1] == true) count++; if (col < Size-1 && World[row-1][col+1] == true) count++; if (World[row-1][col] == true) count++; } if (row < Size - 1) { if (col > 0 && World[row+1][col-1] == true) count++; if (col < Size - 1 && World[row+1][col+1] == true) count++; if (World[row+1][col] == true) count++; } if (col > 0 && World[row][col-1] == true) count++; if (col < Size - 1 && World[row][col+1] == true) count++; return count; }
I'm really not sure how to tackle the problem and any ideas would be helpful.
Thanks in advance and thanks again Ancient Dragon.
•
•
•
•
Thanks a lot. It's reading the file just fine now but I noticed another problem after running through a couple generations. I neglected to include a command to populate a cell with three neighbors. No kids means everyone just slowly dies out. Here's the relevant code (let me know if you need more):
C++ Syntax (Toggle Plain Text)
if (neighborcount == 2) // 2 neighbors tempWorld[i][j] = World[i][j]; // stays alive. else if (neighborcount == 3) tempWorld[i][j] = true; // 3 neighbors: // stays alive else tempWorld[i][j] = false; // 1 and 4+ neighbors:
And please format your code better -- it's a little hard to follow. You need more {}'s and proper indentation to make it easier to read.
Last edited by WaltP; Mar 29th, 2007 at 3:50 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
I don't think I understand. What does this code do?
calls a getNeighborCount function and loops a local array through to a temporary world. This part decides in the temp world if the cell will live or die. After that decision is made, the temporary world gets copied over to the next generation.
Doesn't it test for 3 neighbors?C++ Syntax (Toggle Plain Text)
if (neighborcount == 2) // 2 neighbors tempWorld[i][j] = World[i][j]; // stays alive. else if (neighborcount == 3) tempWorld[i][j] = true; // 3 neighbors: // stays alive else tempWorld[i][j] = false; // 1 and 4+ neighbors:
Yes it does, but the three cells should populate a fourth. The repopulating thing is bugging me.
And please format your code better -- it's a little hard to follow. You need more {}'s and proper indentation to make it easier to read.
Here's the whole thing. I'm submitting it today so if there's any fine tuning that needs to be done or if you'd just like to yell at me feel free. WaltP- I tried spacing stuff out and lining things up but really didn't add many brackets because they've been a sore spot with me. Any suggestions you (or anyone else) have further regarding the formatting are welcome and appreciated. Thanks again everyone.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; const int Size = 20; void displayGrid(const bool [][Size]); void nextGen(bool [][Size], bool [][Size]); int getNeighborCount(const bool [][Size], int, int); int main() { ifstream datafile; string filename; cout << "The input values from the file will begin the first generation." << endl << endl; cout << "Follow the prompts to create new generations or terminate the program." << endl << endl; // Initilize the grid to all dead cells bool Grid[Size][Size]; for (int i=0; i < Size; i++) for (int j=0; j < Size; j++) Grid[i][j] = false; int row, col; // Prompt user for filename. cout << "Please enter the file name containing input values. " << endl; cout << endl; cin >> filename; cout << fixed << showpoint; datafile.open(filename.c_str()); if (!datafile) { cout << "File not found" << endl; return 1; } while (datafile) { datafile >> row >> col; if ( row>=0 && row<Size && col>=0 && col<Size) // checks for compatability Grid[row][col] = true; } while ( row != -1); displayGrid(Grid); char answer; bool temp[Size][Size]; cout << "Display next generation? Y/N "; cin >> answer; while (answer == 'y' || answer == 'Y') { nextGen(Grid, temp); displayGrid(Grid); cout << "Display next generation? Y/N "; cin >> answer; } return 0; } void displayGrid(const bool World[][Size]) { cout << endl; for (int i=0; i < Size; i++) { for (int j=0; j < Size; j++) if (World[i][j] == true) cout << "X "; else cout << ". "; cout << endl; } } // Makes a temporary generation based on the current grid. Calls the // getNeighborCount function and makes a live/die decision. Then the // temporary world gets copied over to a new generation. void nextGen(bool World[][Size], bool tempWorld[][Size]) { int neighborcount; for (int i=0; i < Size; i++) for (int j=0; j < Size; j++) { neighborcount = getNeighborCount(World,i,j); if (neighborcount == 2) // 2 neighbors stays alive tempWorld[i][j] = World[i][j]; else if (neighborcount == 3) tempWorld[i][j] = true; // 3 neighbors stays alive else tempWorld[i][j] = false; // 1 and 4+ neighbors = dead } // copy tempWorld to World { for (int i=0; i < Size; i++) for (int j=0; j < Size; j++) World[i][j] = tempWorld[i][j]; } } int getNeighborCount(const bool World[][Size], int row, int col) // Counts neighbors in a row/column pair { int count=0; if (row > 0) { if (col > 0 && World[row-1][col-1] == true) count++; if (col < Size-1 && World[row-1][col+1] == true) count++; if (World[row-1][col] == true) count++; } if (row < Size - 1) { if (col > 0 && World[row+1][col-1] == true) count++; if (col < Size - 1 && World[row+1][col+1] == true) count++; if (World[row+1][col] == true) count++; } if (col > 0 && World[row][col-1] == true) count++; if (col < Size - 1 && World[row][col+1] == true) count++; return count; }
Does look better. Read this tutorial for more information.
That's like saying "I want to be a writer, but punctuation confuses me so I won't use much." Simply put brackets around every statement that could be a compound statement. (
Look at good code. Check out the code from poster's here that have a high post count. See what they do and compare it to the tutorial above.
•
•
•
•
... but really didn't add many brackets because they've been a sore spot with me.
if, for, while, etc.)Look at good code. Check out the code from poster's here that have a high post count. See what they do and compare it to the tutorial above.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Looking for help with C++ game of life program. Will pay or make donation for tutor. (C++)
- Game Of Life (C++)
- Game of life (C++)
- conway's game of life (C++)
Other Threads in the C++ Forum
- Previous Thread: help with program please
- Next Thread: The Problem with Pointers
Views: 2346 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding stream string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






