Game of life i/o problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

Game of life i/o problem

 
0
  #1
Mar 28th, 2007
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:

  1.  
  2. int main()
  3. {
  4. ifstream datafile;
  5. string filename;
  6.  
  7. cout << "The input values from the file will begin the first generation." << endl << endl;
  8. cout << "Follow the prompts to create new generations or terminate the program." << endl << endl;
  9.  
  10. // Initilize the grid to all dead cells
  11. bool Grid[Size][Size];
  12. for (int i=0; i < Size; i++)
  13. for (int j=0; j < Size; j++)
  14. Grid[i][j] = false;
  15.  
  16. int row, col;
  17.  
  18. // Prompt user for filename.
  19.  
  20. cout << "Please enter the file name containing input values. " << endl;
  21. cout << endl;
  22. cin >> filename;
  23.  
  24. cout << fixed << showpoint;
  25.  
  26. datafile.open(filename.c_str());
  27. if (!datafile)
  28. {
  29. cout << "File not found" << endl;
  30. return 1;
  31. }
  32. while (datafile)
  33. {
  34. datafile >> row >> col;
  35. }
  36. if ( row>=0 && row<Size && col>=0 && col<Size) // if legal row-col
  37. Grid[row][col] = true;
  38. while ( row != -1);
  39.  
  40. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,651
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Game of life i/o problem

 
0
  #2
Mar 28th, 2007
  1. while (datafile)
  2. {
  3. datafile >> row >> col;
  4. }
  5. if ( row>=0 && row<Size && col>=0 && col<Size) // if legal row-col
  6. Grid[row][col] = true;
In the above code you need to move that if statement to be within the while statement. As it is, your program reads the whole file but does nothing with the numbers.
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

Re: Game of life i/o problem

 
0
  #3
Mar 28th, 2007
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):

  1. void nextGen(bool World[][Size], bool tempWorld[][Size])
  2. {
  3. int neighborcount;
  4. for (int i=0; i < Size; i++)
  5. for (int j=0; j < Size; j++) {
  6. neighborcount = getNeighborCount(World,i,j);
  7. if (neighborcount == 2) // 2 neighbors
  8. tempWorld[i][j] = World[i][j]; // stays alive.
  9. else if (neighborcount == 3)
  10. tempWorld[i][j] = true; // 3 neighbors:
  11. // stays alive
  12.  
  13. else tempWorld[i][j] = false; // 1 and 4+ neighbors:
  14. } // dead
  15.  
  16. // copy tempWorld to World
  17. for (int i=0; i < Size; i++)
  18. for (int j=0; j < Size; j++)
  19. World[i][j] = tempWorld[i][j];
  20. }
  21.  
  22. int getNeighborCount(const bool World[][Size], int row, int col)
  23.  
  24. // Counts neighbors in a row/column pair
  25. {
  26.  
  27. int count=0;
  28.  
  29. if (row > 0) {
  30. if (col > 0 && World[row-1][col-1] == true)
  31. count++;
  32. if (col < Size-1 && World[row-1][col+1] == true)
  33. count++;
  34. if (World[row-1][col] == true)
  35. count++;
  36.  
  37. }
  38.  
  39. if (row < Size - 1) {
  40. if (col > 0 && World[row+1][col-1] == true)
  41. count++;
  42. if (col < Size - 1 && World[row+1][col+1] == true)
  43. count++;
  44. if (World[row+1][col] == true)
  45. count++;
  46. }
  47.  
  48. if (col > 0 && World[row][col-1] == true)
  49. count++;
  50. if (col < Size - 1 && World[row][col+1] == true)
  51. count++;
  52.  
  53. return count;
  54. }

I'm really not sure how to tackle the problem and any ideas would be helpful.
Thanks in advance and thanks again Ancient Dragon.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,273
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 543
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Game of life i/o problem

 
0
  #4
Mar 28th, 2007
i have code for basically this exact thing in VB but using arrays if you are interested
Last edited by jbennet; Mar 28th, 2007 at 6:57 pm.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Game of life i/o problem

 
1
  #5
Mar 29th, 2007
Originally Posted by robotnixon View Post
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 don't think I understand. What does this code do?
  1. if (neighborcount == 2) // 2 neighbors
  2. tempWorld[i][j] = World[i][j]; // stays alive.
  3. else if (neighborcount == 3)
  4. tempWorld[i][j] = true; // 3 neighbors:
  5. // stays alive
  6.  
  7. else tempWorld[i][j] = false; // 1 and 4+ neighbors:
Doesn't it test for 3 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

Re: Game of life i/o problem

 
0
  #6
Mar 29th, 2007
Originally Posted by jbennet View Post
i have code for basically this exact thing in VB but using arrays if you are interested
Sure I'll take a look. I'd like to see another way of doing it. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

Re: Game of life i/o problem

 
0
  #7
Mar 29th, 2007
Originally Posted by WaltP View Post
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.

  1. if (neighborcount == 2) // 2 neighbors
  2. tempWorld[i][j] = World[i][j]; // stays alive.
  3. else if (neighborcount == 3)
  4. tempWorld[i][j] = true; // 3 neighbors:
  5. // stays alive
  6.  
  7. else tempWorld[i][j] = false; // 1 and 4+ neighbors:
Doesn't it test for 3 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.
I'm reformatting right now. I agree that it is hard to read and apologize. I'll repost as soon as I'm done. Thanks for telling me. I'm sure that would have cost me points.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

Re: Game of life i/o problem

 
0
  #8
Mar 29th, 2007
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.
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. const int Size = 20;
  7. void displayGrid(const bool [][Size]);
  8. void nextGen(bool [][Size], bool [][Size]);
  9. int getNeighborCount(const bool [][Size], int, int);
  10.  
  11. int main()
  12. {
  13. ifstream datafile;
  14. string filename;
  15.  
  16. cout << "The input values from the file will begin the first generation." << endl << endl;
  17. cout << "Follow the prompts to create new generations or terminate the program." << endl << endl;
  18.  
  19. // Initilize the grid to all dead cells
  20. bool Grid[Size][Size];
  21.  
  22. for (int i=0; i < Size; i++)
  23. for (int j=0; j < Size; j++)
  24.  
  25. Grid[i][j] = false;
  26.  
  27. int row, col;
  28.  
  29. // Prompt user for filename.
  30.  
  31. cout << "Please enter the file name containing input values. " << endl;
  32. cout << endl;
  33. cin >> filename;
  34.  
  35. cout << fixed << showpoint;
  36.  
  37. datafile.open(filename.c_str());
  38. if (!datafile)
  39. {
  40. cout << "File not found" << endl;
  41. return 1;
  42. }
  43. while (datafile)
  44. {
  45. datafile >> row >> col;
  46.  
  47. if ( row>=0 && row<Size && col>=0 && col<Size) // checks for compatability
  48. Grid[row][col] = true;
  49. }
  50. while ( row != -1);
  51.  
  52. displayGrid(Grid);
  53.  
  54. char answer;
  55.  
  56. bool temp[Size][Size];
  57.  
  58. cout << "Display next generation? Y/N ";
  59.  
  60. cin >> answer;
  61.  
  62. while (answer == 'y' || answer == 'Y')
  63. {
  64. nextGen(Grid, temp);
  65. displayGrid(Grid);
  66. cout << "Display next generation? Y/N ";
  67. cin >> answer;
  68. }
  69.  
  70. return 0;
  71. }
  72.  
  73. void displayGrid(const bool World[][Size])
  74. {
  75. cout << endl;
  76.  
  77. for (int i=0; i < Size; i++)
  78. {
  79. for (int j=0; j < Size; j++)
  80.  
  81. if (World[i][j] == true)
  82.  
  83. cout << "X ";
  84.  
  85. else cout << ". ";
  86.  
  87. cout << endl;
  88. }
  89. }
  90.  
  91. // Makes a temporary generation based on the current grid. Calls the
  92. // getNeighborCount function and makes a live/die decision. Then the
  93. // temporary world gets copied over to a new generation.
  94.  
  95. void nextGen(bool World[][Size], bool tempWorld[][Size])
  96. {
  97. int neighborcount;
  98.  
  99. for (int i=0; i < Size; i++)
  100.  
  101. for (int j=0; j < Size; j++)
  102. {
  103. neighborcount = getNeighborCount(World,i,j);
  104.  
  105. if (neighborcount == 2) // 2 neighbors stays alive
  106. tempWorld[i][j] = World[i][j];
  107.  
  108. else if (neighborcount == 3)
  109. tempWorld[i][j] = true; // 3 neighbors stays alive
  110.  
  111.  
  112. else tempWorld[i][j] = false; // 1 and 4+ neighbors = dead
  113. }
  114.  
  115. // copy tempWorld to World
  116. {
  117. for (int i=0; i < Size; i++)
  118.  
  119. for (int j=0; j < Size; j++)
  120.  
  121. World[i][j] = tempWorld[i][j];
  122. }
  123. }
  124.  
  125. int getNeighborCount(const bool World[][Size], int row, int col)
  126.  
  127. // Counts neighbors in a row/column pair
  128. {
  129.  
  130. int count=0;
  131.  
  132. if (row > 0)
  133. {
  134.  
  135. if (col > 0 && World[row-1][col-1] == true)
  136. count++;
  137.  
  138. if (col < Size-1 && World[row-1][col+1] == true)
  139. count++;
  140.  
  141. if (World[row-1][col] == true)
  142. count++;
  143.  
  144. }
  145.  
  146. if (row < Size - 1)
  147. {
  148.  
  149. if (col > 0 && World[row+1][col-1] == true)
  150. count++;
  151.  
  152. if (col < Size - 1 && World[row+1][col+1] == true)
  153. count++;
  154.  
  155. if (World[row+1][col] == true)
  156. count++;
  157. }
  158.  
  159. if (col > 0 && World[row][col-1] == true)
  160. count++;
  161.  
  162. if (col < Size - 1 && World[row][col+1] == true)
  163. count++;
  164.  
  165. return count;
  166. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Game of life i/o problem

 
0
  #9
Mar 29th, 2007
Originally Posted by robotnixon View Post
I tried spacing stuff out and lining things up ...
Does look better. Read this tutorial for more information.

Originally Posted by robotnixon View Post
... but really didn't add many brackets because they've been a sore spot with me.
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. (if, for, while, etc.)
Originally Posted by robotnixon View Post
Any suggestions you (or anyone else) have further regarding the formatting are welcome and appreciated. Thanks again everyone.
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

Re: Game of life i/o problem

 
0
  #10
Mar 29th, 2007
I'll work on it. Thanks for the pointers WaltP. I got bracket-shy because the missplaced bracket in the OP was silly and time-consuming. But you are right. Thanks again.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2346 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC