До сих пор я сделал все, что касается создания класса, поиска характеристик живых клеток и генерации поколений. Я просто застрял в поиске статистики для данных мертвых ячеек в определенной строке c, а столбец - мой клиент. cpp. Я хотел бы получить некоторые рекомендации, чтобы закончить sh этот последний кусок в моем коде, я могу предоставить больше кода при необходимости.
main. cpp
#include <iostream>
#include <iomanip>
#include <cassert>
#include <fstream>
#include "boolmatrix.h"
using namespace std;
void readGrid(/*out*/ boolMatrix& life);
void getGenerations(/*out*/ int& numGenerations);
void determineNextGeneration(/*inout*/ boolMatrix& life);
//void determineFateOfSingleCell(/*in*/ const boolMatrix& life,/*out*/ boolMatrix& life2,/*in*/int row,/*in*/ int col);
void printResults(/*in*/ const boolMatrix& life);
int main()
{
boolMatrix life; //Used for file reading
int numGenerations;
/* life.print(); //Normal ADT Grid Display */
readGrid(life); //ADT / File Read Display
getGenerations(numGenerations);
for (int count = 0; count < numGenerations; count++)
{
printResults(life);
determineNextGeneration(life);
printResults(life);
}
//*NOTE: MULTI LINE COMMENTS ARE CLASS TESTING FUNCTIONS*
/* //populate a bool array using rand values for cell coordinates or positions
for (int i = 0; i < 50; i++) {
life.set(rand() % 20, rand() % 20, true);
} */
// cout << life.get(0,0) << endl;
/* for (int row = 0; row < boolMatrix::NUM_ROWS; row++) {
for (int col = 0; col < boolMatrix::NUM_COLS; col++) {
cout << life.neighborCount(row, col); //calling an important function to look around a cell
}
cout << endl;
} */
}
void readGrid(boolMatrix& life)
{
int row, col;
//readGrid - HINT source code below should be a read or get_input function
ifstream infile("lifedata.txt");
infile >> row >> col;
while (infile)
{
//matrix[row][col] = true;
life.set(row,col,true); //a setter class member function is needed here
infile >> row >> col;
}
infile.close();
}
void getGenerations(/*out*/ int& numGenerations)
{
cout << "Enter number of generations: ";
cin >> numGenerations;
cout << endl;
}
void printResults(/*in*/ const boolMatrix& life)
{
life.print(); //calling the class print member function
//calling three more class member functions
cout<<"Total alive in row 10: " << life.rowCount(10) << endl; //Determines total alive in specific row
cout<<"Total alive in column 10: " << life.colCount(10) << endl; //Determines total alive in specific column
//cout << "Total dead in row 16: " <<
//Total dead in col 1 = 16
cout<<"Total alive in matrix: " << life.totalCount()<< endl; //Determines total alive
cout << endl;
}
void determineNextGeneration(/*inout*/ boolMatrix& life)
{
boolMatrix b;
for(int row=0;row<boolMatrix::NUM_ROWS;row++)
{
for (int col=0;col<boolMatrix::NUM_COLS;col++)
{
int count = life.neighborCount(row,col);
if(life.get(row,col))
{
if(count > 1 && count < 4)
b.set(row,col,true);
}
else
{
if(count == 3)
b.set(row,col,true);
}
}
}
life = b;
}
boolMatrix.h
#ifndef LIFE_H
#define LIFE_H
#include <iostream>
#include <iomanip>
#include <cassert>
#include <fstream>
using namespace std;
class boolMatrix
{
public:
static const int NUM_ROWS = 20; //Static variable declaring row size
static const int NUM_COLS = 20; //Static variable declaring column size
boolMatrix(); //A default constructor
void set(int row,int col, bool val);
void print() const; //observers
bool get(int row,int col) const;
int totalCount()const;
int rowCount(int row)const;
int colCount(int col)const;
int neighborCount(int row, int col)const;
private:
bool matrix[NUM_ROWS][NUM_COLS];
};
#endif
boolMatrix. cpp
#include <iostream>
#include <iomanip>
#include <cassert>
#include <fstream>
#include "boolmatrix.h"
using namespace std;
boolMatrix::boolMatrix()
{
//initialize array - HINT source code below should be an initialize function
for (int row = 0; row < NUM_ROWS; row++)
{
for (int col = 0; col < NUM_COLS; col++)
{
matrix[row][col] = false;
}
}
}
void boolMatrix::set(int row,int col, bool val)
{
assert(row >= 0 && row < NUM_ROWS && col >= 0 && col < NUM_COLS);
matrix[row][col] = val;
}
void boolMatrix::print() const
{
//print contents of array after initialization - HINT source code below should be a print function
// cout << " 01234" << endl;
cout << " ";
for(int col = 0; col < NUM_COLS; col++)
{
cout << col % 10;
}
cout << endl;
for (int row = 0; row < NUM_ROWS; row++)
{
cout << setw(2) << row % 25;
for (int col = 0; col < NUM_COLS; col++)
{
if (matrix[row][col])
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
cout << endl;
}
bool boolMatrix::get(int row, int col) const
{
assert(row>= 0 && row < NUM_ROWS && col >= 0 && col < NUM_COLS);
return matrix[row][col];
}
int boolMatrix::totalCount()const
{
int count = 0;
for(int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
if(matrix[row][col])
count++;
}
}
return count;
}
int boolMatrix::rowCount(int row)const
{
//assert
assert(row >= 0 && row < NUM_ROWS);
int count = 0;
for(int col = 0; col < NUM_COLS; col++)
{
if(matrix[row][col])
count++;
}
return count;
}
int boolMatrix::colCount(int col)const
{
//assert
assert(col >= 0 && col < NUM_COLS);
int count = 0;
for(int row = 0; row < NUM_ROWS; row++)
{
if(matrix[row][col])
count++;
}
return count;
}
int boolMatrix::neighborCount(int row, int col)const
{
assert(row>= 0 && row < NUM_ROWS && col >= 0 && col < NUM_COLS);
int count = 0;
if(row > 0)
count += get(row - 1, col);
if(row < NUM_ROWS - 1)
count += get(row + 1, col);
if(col > 0)
count += get(row, col - 1);
if(col < NUM_COLS - 1)
count += get(row, col + 1);
if(row > 0 && col > 0)
count += get(row - 1, col - 1);
if(row > 0 && col < NUM_COLS - 1)
count += get(row - 1, col + 1);
if(row < NUM_ROWS - 1 && col > 0)
count += get(row + 1, col - 1);
if(row < NUM_ROWS - 1 && col < NUM_COLS - 1)
count += get(row + 1, col + 1);
return count;
}
ВЫБОР ОБРАЗЦА:
(ОРИГИНАЛЬНАЯ СЕТКА) ИГРА ЖИЗНИ НАЧИНАЕТ СТАТИСТИКУ
СЕТКА НЕ ОТОБРАЖАЕТСЯ ЗДЕСЬ, ПОТОМУ ЧТО СЛИШКОМ МНОГО СИМВОЛОВ
Total alive in row 10 = 3
Total alive in col 10 = 4
Total dead in row 16 = 15
Total dead in col 1 = 16
Total alive = 101
Total dead = 299