944,124 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3051
  • C++ RSS
Mar 28th, 2007
0

Game of life i/o problem

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
robotnixon is offline Offline
37 posts
since Mar 2007
Mar 28th, 2007
0

Re: Game of life i/o problem

C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Mar 28th, 2007
0

Re: Game of life i/o problem

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)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
robotnixon is offline Offline
37 posts
since Mar 2007
Mar 28th, 2007
0

Re: Game of life i/o problem

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.
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005
Mar 29th, 2007
1

Re: Game of life i/o problem

Click to Expand / Collapse  Quote originally posted by robotnixon ...
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?
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,748 posts
since May 2006
Mar 29th, 2007
0

Re: Game of life i/o problem

Click to Expand / Collapse  Quote originally posted by jbennet ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
robotnixon is offline Offline
37 posts
since Mar 2007
Mar 29th, 2007
0

Re: Game of life i/o problem

Click to Expand / Collapse  Quote originally posted by WaltP ...
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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
robotnixon is offline Offline
37 posts
since Mar 2007
Mar 29th, 2007
0

Re: Game of life i/o problem

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)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
robotnixon is offline Offline
37 posts
since Mar 2007
Mar 29th, 2007
0

Re: Game of life i/o problem

Click to Expand / Collapse  Quote originally posted by robotnixon ...
I tried spacing stuff out and lining things up ...
Does look better. Read this tutorial for more information.

Click to Expand / Collapse  Quote originally posted by robotnixon ...
... 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.)
Click to Expand / Collapse  Quote originally posted by robotnixon ...
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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,748 posts
since May 2006
Mar 29th, 2007
0

Re: Game of life i/o problem

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
robotnixon is offline Offline
37 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with program please
Next Thread in C++ Forum Timeline: The Problem with Pointers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC