#include #include "BoardT.h" using namespace std; class CellT { public: CellT()=default; void Check(size_t row, size_t col) { BoardT & board = BoardT::GetInstance(); count = 0; for(int r = row-1; r <= static_cast(row)+1; ++r) { for(int c = col-1; c <= static_cast(col)+1; ++c) { int rReal{r}; int cReal{c}; if (rReal < 0) { rReal = board.Rows() -1; } if (rReal == static_cast(board.Rows())) { rReal = 0; } if (cReal < 0) { cReal = board.Cols() -1; } if (cReal == static_cast(board.Cols())) { cReal = 0; } if (board[rReal,cReal].alive) { ++count; } } } if (board[row,col].alive) { count --; if (count < 2 or count > 3) { nextState = false; } else { nextState = true; } } else { if (count == 3) { nextState = true; } else { nextState = false; } } } void Update() { alive = nextState; nextState = false; } void Live() { alive = true; } void Die() { alive = false; } bool Alive(void) const { return alive;} size_t Count(void) const {return count;} private: bool alive{false}; size_t count; bool nextState{false}; }; using MyBoardT = BoardT; void MakeBoard(); void MakeDemoBoard(); void DisplayBoard(); void UpdateBoard(); int main() { srand(time(nullptr)); MyBoardT::SetSize(20,20); MakeDemoBoard(); string junk; DisplayBoard(); cout << endl; while(junk != "q") { UpdateBoard(); DisplayBoard(); cout << "Continue "; getline(cin, junk); } return 0; } void UpdateBoard() { MyBoardT & board = MyBoardT::GetInstance(); size_t r, c; for(r =0; r < board.Rows(); ++r) { for(c = 0; c < board.Cols(); ++c) { board[r,c].Check(r,c); } } for(r =0; r < board.Rows(); ++r) { for(c = 0; c < board.Cols(); ++c) { board[r,c].Update(); } } } void DisplayBoard() { MyBoardT & board = MyBoardT::GetInstance(); size_t r, c; for(r =0; r < board.Rows(); ++r) { for(c = 0; c < board.Cols(); ++c) { if (board[r,c].Alive()) { //cout << board[r,c].Count(); cout << "X"; } else { cout << "."; } } cout << endl; } cout << endl; return; } void MakeDemoBoard() { MyBoardT & board = MyBoardT::GetInstance(); // square board[1,1].Live(); board[1,2].Live(); board[2,1].Live(); board[2,2].Live(); // blink board[1,10].Live(); board[2,10].Live(); board[3,10].Live(); // diamond board[1,6].Live(); board[2,5].Live(); board[2,7].Live(); board[3,6].Live(); // exploder board[10,5].Live(); board[11,4].Live(); board[11,5].Live(); board[11,6].Live(); // glider board[15,10].Live(); board[15,11].Live(); board[15,12].Live(); board[16,12].Live(); board[17,11].Live(); } void MakeBoard() { MyBoardT & board = MyBoardT::GetInstance(); size_t r, c; size_t alive{0}; size_t totalSize; totalSize = board.Rows() * board.Cols(); while (alive < .3*totalSize) { r = rand() % board.Rows(); c = rand() % board.Cols(); if (!board[r,c].Alive()) { board[r,c].Live(); ++alive; } } return; }