Game Of Life help? Ive done most of it

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 7
Reputation: starletcharmed is an unknown quantity at this point 
Solved Threads: 0
starletcharmed starletcharmed is offline Offline
Newbie Poster

Game Of Life help? Ive done most of it

 
0
  #1
Aug 26th, 2008
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

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const int SIZE = 20;
  8. bool readGrid(string fileName, char grid[SIZE][SIZE]);
  9. void displayGrid(const char grid[SIZE][SIZE]);
  10.  
  11.  
  12.  
  13. int countAlive(char grid[SIZE][SIZE]);
  14.  
  15. void newGen(char grid[SIZE][SIZE]);
  16.  
  17.  
  18.  
  19.  
  20. // declare more functions if necessary
  21.  
  22. int main()
  23. {
  24. string fileName; // the name of the grid file
  25. int nGenerations; // nGenerations is the number of generations
  26. char grid[SIZE][SIZE]; // a char array consiting of '*' or '.'
  27. // declare more variables if necessary
  28.  
  29. char nuevo[SIZE][SIZE];
  30.  
  31. cout << "Enter the filename of the grid: " << flush; // don't modify
  32. cin >> fileName; // don't modify
  33. cout << endl; // don't modify
  34.  
  35. readGrid(fileName, grid);
  36. displayGrid(grid);
  37.  
  38. // add processing for reading and displaying the grid
  39.  
  40. cout << endl << "How many generations in total? " << flush; // don't modify
  41. cin >> nGenerations; // don't modify
  42.  
  43.  
  44. // add processing for new grid generations
  45.  
  46. if (nGenerations == 1) // don't modify
  47. cout << "This is the grid after " << nGenerations << " generation:" << endl << endl; // don't modify
  48. else // don't modify
  49. cout << "This is the grid after " << nGenerations << " generations:" << endl << endl; // don't modify
  50.  
  51.  
  52.  
  53. // display the grid
  54.  
  55. cout << endl; // don't modify
  56. system("pause"); // don't modify
  57. return 0; // don't modify
  58. }
  59.  
  60. // implement all declared functions
  61.  
  62.  
  63.  
  64.  
  65.  
  66. bool readGrid(string fileName, char grid[SIZE][SIZE])
  67. {
  68. ifstream inData;
  69.  
  70. inData.open(fileName.c_str());
  71.  
  72. if(inData)
  73. {
  74. for(int row = 0; row < SIZE; row++)
  75. {
  76. for(int col = 0; col < SIZE; col++)
  77. {
  78. inData >> grid[row][col];
  79. }
  80. }
  81. return true; }
  82. else
  83. return false;
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. void displayGrid(const char grid[SIZE][SIZE])
  92. {
  93. for(int row = 0; row < SIZE; row++)
  94. {
  95. for(int col = 0; col < SIZE; col++)
  96. {
  97. cout << grid[row][col] << " ";
  98. }
  99. cout << endl;
  100. }
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. int countAlive(char grid[SIZE][SIZE])
  110. {
  111. int neighbour = 0;
  112.  
  113. for(int row = 1; row < SIZE-1; row++)
  114. {
  115. for(int col = 1; col < SIZE-1; col++)
  116. {
  117. if(grid[row+1][col+1] == '*') neighbour++;
  118. if(grid[row+1][col] == '*') neighbour++;
  119. if(grid[row+1][col-1] == '*') neighbour++;
  120. if(grid[row][col-1] == '*') neighbour++;
  121. if(grid[row][col+1] == '*') neighbour++;
  122. if(grid[row-1][col-1] == '*') neighbour++;
  123. if(grid[row-1][col] == '*') neighbour++;
  124. if(grid[row-1][col+1] == '*') neighbour++;
  125. }
  126. }
  127.  
  128. return neighbour;
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. void newGen(char grid[SIZE][SIZE])
  138. {
  139. int neighbour = countAlive(grid);
  140. char nuevo[SIZE][SIZE];
  141.  
  142. grid[SIZE][SIZE] = nuevo[SIZE][SIZE];
  143.  
  144.  
  145. for(int row = 0; row < SIZE; row++)
  146. {
  147. for(int col = 0; col < SIZE; col++)
  148. {
  149. grid[row][col] = nuevo[row][col];
  150.  
  151. if(grid[row][col] == '*' && neighbour == 1)
  152. nuevo[row][col] == '*';
  153. if(grid[row][col] == '.' && neighbour == 1)
  154. nuevo[row][col] == '.';
  155. if(grid[row][col] == '*' && neighbour == 2)
  156. nuevo[row][col] == '*';
  157. if(grid[row][col] == '.' && neighbour == 2)
  158. nuevo[row][col] == '*';
  159. if(grid[row][col] == '*' && neighbour > 2)
  160. nuevo[row][col] == '.';
  161. if(grid[row][col] == '.' && neighbour > 2)
  162. nuevo[row][col] == '.';
  163. }
  164. }
  165. }
Last edited by Narue; Aug 26th, 2008 at 12:15 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Game Of Life help? Ive done most of it

 
0
  #2
Aug 26th, 2008
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.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const int SIZE = 20;
  8. bool readGrid(string fileName, char grid[SIZE][SIZE]);
  9. void displayGrid(const char grid[SIZE][SIZE]);
  10. int countAlive(char grid[SIZE][SIZE]);
  11. void newGen(char grid[SIZE][SIZE]);
  12.  
  13.  
  14.  
  15.  
  16. // declare more functions if necessary
  17.  
  18. int main()
  19. {
  20. string fileName; // the name of the grid file
  21. int nGenerations; // nGenerations is the number of generations
  22. char grid[SIZE][SIZE]; // a char array consiting of '*' or '.'
  23. // declare more variables if necessary
  24.  
  25. char nuevo[SIZE][SIZE];
  26.  
  27. cout << "Enter the filename of the grid: " << flush; // don't modify
  28. cin >> fileName; // don't modify
  29. cout << endl; // don't modify
  30.  
  31. readGrid(fileName, grid);
  32. displayGrid(grid);
  33.  
  34. // add processing for reading and displaying the grid
  35.  
  36. cout << endl << "How many generations in total? " << flush; // don't modify
  37. cin >> nGenerations; // don't modify
  38.  
  39.  
  40. // add processing for new grid generations
  41.  
  42. if (nGenerations == 1) // don't modify
  43. cout << "This is the grid after " << nGenerations << " generation:" << endl << endl; // don't modify
  44. else // don't modify
  45. cout << "This is the grid after " << nGenerations << " generations:" << endl << endl; // don't modify
  46.  
  47.  
  48.  
  49. // display the grid
  50.  
  51. cout << endl; // don't modify
  52. system("pause"); // don't modify
  53. return 0; // don't modify
  54. }
  55.  
  56. // implement all declared functions
  57.  
  58.  
  59. bool readGrid(string fileName, char grid[SIZE][SIZE])
  60. {
  61. ifstream inData;
  62.  
  63. inData.open(fileName.c_str());
  64.  
  65. if(inData)
  66. {
  67. for(int row = 0; row < SIZE; row++)
  68. {
  69. for(int col = 0; col < SIZE; col++)
  70. {
  71. inData >> grid[row][col];
  72. }
  73. }
  74. return true;
  75. }
  76. else
  77. return false;
  78. }
  79.  
  80.  
  81. void displayGrid(const char grid[SIZE][SIZE])
  82. {
  83. for(int row = 0; row < SIZE; row++)
  84. {
  85. for(int col = 0; col < SIZE; col++)
  86. {
  87. cout << grid[row][col] << " ";
  88. }
  89. cout << endl;
  90. }
  91. }
  92.  
  93.  
  94. int countAlive(char grid[SIZE][SIZE])
  95. {
  96. int neighbour = 0;
  97.  
  98. for(int row = 1; row < SIZE-1; row++)
  99. {
  100. for(int col = 1; col < SIZE-1; col++)
  101. {
  102. if(grid[row+1][col+1] == '*') neighbour++;
  103. if(grid[row+1][col] == '*') neighbour++;
  104. if(grid[row+1][col-1] == '*') neighbour++;
  105. if(grid[row][col-1] == '*') neighbour++;
  106. if(grid[row][col+1] == '*') neighbour++;
  107. if(grid[row-1][col-1] == '*') neighbour++;
  108. if(grid[row-1][col] == '*') neighbour++;
  109. if(grid[row-1][col+1] == '*') neighbour++;
  110. }
  111. }
  112.  
  113. return neighbour;
  114. }
  115.  
  116.  
  117. void newGen(char grid[SIZE][SIZE])
  118. {
  119. int neighbour = countAlive(grid);
  120. char nuevo[SIZE][SIZE];
  121.  
  122. grid[SIZE][SIZE] = nuevo[SIZE][SIZE];
  123.  
  124.  
  125. for(int row = 0; row < SIZE; row++)
  126. {
  127. for(int col = 0; col < SIZE; col++)
  128. {
  129. grid[row][col] = nuevo[row][col];
  130.  
  131. if(grid[row][col] == '*' && neighbour == 1)
  132. nuevo[row][col] == '*';
  133. if(grid[row][col] == '.' && neighbour == 1)
  134. nuevo[row][col] == '.';
  135. if(grid[row][col] == '*' && neighbour == 2)
  136. nuevo[row][col] == '*';
  137. if(grid[row][col] == '.' && neighbour == 2)
  138. nuevo[row][col] == '*';
  139. if(grid[row][col] == '*' && neighbour > 2)
  140. nuevo[row][col] == '.';
  141. if(grid[row][col] == '.' && neighbour > 2)
  142. nuevo[row][col] == '.';
  143. }
  144. }
  145. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 7
Reputation: starletcharmed is an unknown quantity at this point 
Solved Threads: 0
starletcharmed starletcharmed is offline Offline
Newbie Poster

Re: Game Of Life help? Ive done most of it

 
0
  #3
Aug 26th, 2008
Originally Posted by VernonDozier View Post
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.
Well in that function im making a new generation, so I tried to copy it to another array inside the function. I guess Ill have to try something else then.

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.....
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 7
Reputation: starletcharmed is an unknown quantity at this point 
Solved Threads: 0
starletcharmed starletcharmed is offline Offline
Newbie Poster

Re: Game Of Life help? Ive done most of it

 
0
  #4
Aug 26th, 2008
Ok well I added a new function to swap the arrays, I think that is better now :s



  1. void swapArray(char grid[SIZE][SIZE], char nuevo[SIZE][SIZE])
  2. {
  3. for(int row = 0; row < SIZE; row++)
  4. {
  5. for(int col = 0; col < SIZE; col++)
  6. {
  7. grid[row][col] == nuevo[row][col];
  8. }
  9. }
  10. }
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. void newGen(char grid[SIZE][SIZE])
  19. {
  20. int neighbour = countAlive(grid);
  21. char nuevo[row][col] = swapArray(grid, nuevo);
  22.  
  23. for(int row = 0; row < SIZE; row++)
  24. {
  25. for(int col = 0; col < SIZE; col++)
  26. {
  27. if(grid[row][col] == '*' && neighbour == 1)
  28. nuevo[row][col] == '*';
  29. if(grid[row][col] == '.' && neighbour == 1)
  30. nuevo[row][col] == '.';
  31. if(grid[row][col] == '*' && neighbour == 2)
  32. nuevo[row][col] == '*';
  33. if(grid[row][col] == '.' && neighbour == 2)
  34. nuevo[row][col] == '*';
  35. if(grid[row][col] == '*' && neighbour > 2)
  36. nuevo[row][col] == '.';
  37. if(grid[row][col] == '.' && neighbour > 2)
  38. nuevo[row][col] == '.';
  39.  
  40. }
  41.  
  42. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Game Of Life help? Ive done most of it

 
0
  #5
Aug 26th, 2008
Originally Posted by starletcharmed View Post
Ok well I added a new function to swap the arrays, I think that is better now :s



  1. void swapArray(char grid[SIZE][SIZE], char nuevo[SIZE][SIZE])
  2. {
  3. for(int row = 0; row < SIZE; row++)
  4. {
  5. for(int col = 0; col < SIZE; col++)
  6. {
  7. grid[row][col] == nuevo[row][col];
  8. }
  9. }
  10. }
Close. Change the == to = . Also, which one is the source array and which one is the destination array? What are you copying into what?





Originally Posted by starletcharmed View Post
  1. void newGen(char grid[SIZE][SIZE])
  2. {
  3. int neighbour = countAlive(grid);
  4. char nuevo[row][col] = swapArray(grid, nuevo);
  5.  
  6. for(int row = 0; row < SIZE; row++)
  7. {
  8. for(int col = 0; col < SIZE; col++)
  9. {
  10. if(grid[row][col] == '*' && neighbour == 1)
  11. nuevo[row][col] == '*';
  12. if(grid[row][col] == '.' && neighbour == 1)
  13. nuevo[row][col] == '.';
  14. if(grid[row][col] == '*' && neighbour == 2)
  15. nuevo[row][col] == '*';
  16. if(grid[row][col] == '.' && neighbour == 2)
  17. nuevo[row][col] == '*';
  18. if(grid[row][col] == '*' && neighbour > 2)
  19. nuevo[row][col] == '.';
  20. if(grid[row][col] == '.' && neighbour > 2)
  21. nuevo[row][col] == '.';
  22.  
  23. }
  24.  
  25. }
Line 4 above. Be careful. Are you declaring a new array called nuevo? if so, what is the size? Is it supposed to be SIZE x SIZE or row x col? Also, do it in two separate lines:

  1. char nuevo[?][?];
  2. swapArray(grid, nuevo); // make sure this order matches the order
  3. // in swapArray and that you are copying
  4. // 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC