Game of Life. Please Help!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 1
Reputation: Kevin Baker is an unknown quantity at this point 
Solved Threads: 0
Kevin Baker Kevin Baker is offline Offline
Newbie Poster

Game of Life. Please Help!

 
0
  #1
May 18th, 2008
I have an assignment for school to make a program called the game of life. This program takes values from a data file and places them in a 2d array. All the values taked are set to true in the array and any empty places default to false. Then it displays it on a grid of *'s for true and blank spaces for false. There are 20 rows and 20 colums in the grid. Then the program follows these rules to find the next generation of values from the grid:

If the cell is currently empty:
If the cell has exactly three living neighbors, it will come to life in the next generation.
If the cell has any other number of living neighbors, it will remain empty.
If the cell is currently living:
If the cell has one or zero living neighbors, it will die of loneliness in the next generation.
If the cell has four or more living neighbors, it will die of overcrowding in the next generation.
If the cell has two or three neighbors, it will remain living.
All births and deaths occur simultaneously. This point is critical to the correct result

I have written the code and from everything I see it should be working and compiles fine but for some reason I keep getting the wrong answer, can somone please let me know what I am doing wrong. For now all I want it to do is print the 2nd generation to the screen with the correct results.
The correct output for each grid can be found here: http://www.santarosa.edu/~dharden/10...j5fullout.html

The data values that are read from the data file can be found at: http://www.santarosa.edu/~dharden/10s08/proj5data.html

I have included my main .cpp file for the code as an attachment. I was going to include the header file and the class member functions file as attachments but it wouldn't allow me to so I am posting them right here

Class code(i'm sorry for the length of this code):
  1. //////////////////////Header//////////////////////
  2. #include <iostream>
  3. #ifndef BOOLMATRIX_H
  4. #define BOOLMATRIX_H
  5.  
  6.  
  7. const int rows = 20;
  8. const int cols = 20;
  9.  
  10.  
  11. class boolMatrix
  12. {
  13. public: // Available for clients use.
  14.  
  15. boolMatrix();
  16. bool get(int row, int col);
  17. void set(int row, int col, bool answer);
  18. int rowcount(int row);
  19. int colcount(int col);
  20. int totalcount();
  21. void print();
  22.  
  23.  
  24. private: // Can only be used within the class, Client cannot call it.
  25. bool container[rows][cols];
  26. };
  27.  
  28.  
  29. #endif // BOOLMATRIX_H
  30.  
  31. /////////Class Member Functions////////////
  32. #include <iostream>
  33. #include "boolmatrix.h" // class's header file
  34. #include <iomanip>
  35.  
  36.  
  37. using namespace std;
  38.  
  39.  
  40. // class constructor
  41. boolMatrix::boolMatrix()
  42. {
  43. for (int row = 0; row < rows; row++){
  44. for (int col = 0; col < cols; col++){
  45. container[row][col] = false;
  46. }
  47. }
  48.  
  49. }
  50.  
  51.  
  52. bool boolMatrix::get(int row, int col)
  53. {
  54. return container[row][col];
  55. }
  56.  
  57.  
  58. void boolMatrix::set(int row, int col, bool answer)
  59. {
  60. container[row][col] = answer;
  61. }
  62.  
  63.  
  64. int boolMatrix::rowcount(int row)
  65. {
  66. int rowtotal = 0;
  67.  
  68. for (int count = 0; count < rows; count++){
  69. if ( container[row][count] == true ){
  70. rowtotal++;
  71. }
  72. }
  73. return rowtotal;
  74. }
  75.  
  76.  
  77. int boolMatrix::colcount(int col)
  78. {
  79. int coltotal = 0;
  80.  
  81. for (int count = 0; count < cols; count++){
  82. if ( container[count][col] == true ){
  83. coltotal++;
  84. }
  85. }
  86. return coltotal;
  87. }
  88.  
  89.  
  90. int boolMatrix::totalcount()
  91. {
  92. int total = 0;
  93. for (int row = 0; row < rows; row++){
  94. for (int col = 0; col < cols; col++){
  95. if ( container[row][col] == true ){
  96. total++;
  97. }
  98. }
  99. }
  100. return total;
  101.  
  102. }
  103.  
  104.  
  105. void boolMatrix::print()
  106. {
  107. int toptable[9];
  108. int sidetable[rows];
  109. int test = 0;
  110.  
  111. cout << " ";
  112. for ( int count = 0; count < 10; count++){
  113. toptable[count] = count;
  114. }
  115.  
  116. for ( int row = 0; row < rows; row++){
  117. sidetable[row] = row;
  118. }
  119.  
  120. for (int col = 0; col < cols; col++){
  121. cout << toptable[test];
  122. test++;
  123. if ( test == 10 )
  124. test = 0;
  125. }
  126. cout << endl;
  127.  
  128. for (int row = 0; row < rows; row++){
  129. cout << setw(2) << sidetable[row];
  130. for (int col = 0; col < cols; col++){
  131. if ( container[row][col] == true ){
  132. cout << "*";
  133. } else if ( container[row][col] == false ){
  134. cout << " ";
  135. }
  136. }
  137. cout << endl;
  138. }
  139. }
Last edited by Kevin Baker; May 18th, 2008 at 4:56 am.
Attached Files
File Type: cpp Project5.cpp (3.8 KB, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Game of Life. Please Help!

 
0
  #2
May 18th, 2008
http://cboard.cprogramming.com/showthread.php?t=103202

Lesson 1 - Choose your forum carefully.
Broadcasting your homework all over the net is NOT choosing carefully, it's claiming Urgency by proxy.
Reply With Quote Quick reply to this message  
Reply

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




Views: 416 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC