| | |
Game of Life. Please Help!
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
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):
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):
C++ Syntax (Toggle Plain Text)
//////////////////////Header////////////////////// #include <iostream> #ifndef BOOLMATRIX_H #define BOOLMATRIX_H const int rows = 20; const int cols = 20; class boolMatrix { public: // Available for clients use. boolMatrix(); bool get(int row, int col); void set(int row, int col, bool answer); int rowcount(int row); int colcount(int col); int totalcount(); void print(); private: // Can only be used within the class, Client cannot call it. bool container[rows][cols]; }; #endif // BOOLMATRIX_H /////////Class Member Functions//////////// #include <iostream> #include "boolmatrix.h" // class's header file #include <iomanip> using namespace std; // class constructor boolMatrix::boolMatrix() { for (int row = 0; row < rows; row++){ for (int col = 0; col < cols; col++){ container[row][col] = false; } } } bool boolMatrix::get(int row, int col) { return container[row][col]; } void boolMatrix::set(int row, int col, bool answer) { container[row][col] = answer; } int boolMatrix::rowcount(int row) { int rowtotal = 0; for (int count = 0; count < rows; count++){ if ( container[row][count] == true ){ rowtotal++; } } return rowtotal; } int boolMatrix::colcount(int col) { int coltotal = 0; for (int count = 0; count < cols; count++){ if ( container[count][col] == true ){ coltotal++; } } return coltotal; } int boolMatrix::totalcount() { int total = 0; for (int row = 0; row < rows; row++){ for (int col = 0; col < cols; col++){ if ( container[row][col] == true ){ total++; } } } return total; } void boolMatrix::print() { int toptable[9]; int sidetable[rows]; int test = 0; cout << " "; for ( int count = 0; count < 10; count++){ toptable[count] = count; } for ( int row = 0; row < rows; row++){ sidetable[row] = row; } for (int col = 0; col < cols; col++){ cout << toptable[test]; test++; if ( test == 10 ) test = 0; } cout << endl; for (int row = 0; row < rows; row++){ cout << setw(2) << sidetable[row]; for (int col = 0; col < cols; col++){ if ( container[row][col] == true ){ cout << "*"; } else if ( container[row][col] == false ){ cout << " "; } } cout << endl; } }
Last edited by Kevin Baker; May 18th, 2008 at 4:56 am.
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.
Lesson 1 - Choose your forum carefully.
Broadcasting your homework all over the net is NOT choosing carefully, it's claiming Urgency by proxy.
![]() |
Similar Threads
- Game of life (Assembly)
- Game of life i/o problem (C++)
- 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: bubble sort
- Next Thread: Odds calculation program
Views: 416 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






