From 23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 19 Dec 2016 22:03:27 +0100 Subject: Add population display --- life.cc | 7 +++++-- src/world.h | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/life.cc b/life.cc index ee522ec..3af4e69 100644 --- a/life.cc +++ b/life.cc @@ -31,14 +31,17 @@ void draw( }); util::print_tb(1, 1, TB_WHITE, TB_DEFAULT, "Age:"); - util::print_tb(6, 1, TB_WHITE, TB_DEFAULT, std::to_string(world.getAge())); + util::print_tb(13, 1, TB_WHITE, TB_DEFAULT, std::to_string(world.getAge())); + + util::print_tb(1, 2, TB_WHITE, TB_DEFAULT, "Population:"); + util::print_tb(13, 2, TB_WHITE, TB_DEFAULT, std::to_string(world.getPopulation())); tb_present(); } int main(int, char*[]) { util::TermGuard guard; - life::World<40,20> world; + life::World<60,40> world; std::size_t worldOffsetX = tb_width() / 2 - world.width / 2; std::size_t worldOffsetY = tb_height() / 2 - world.height / 2; diff --git a/src/world.h b/src/world.h index 61423c4..e70e20e 100644 --- a/src/world.h +++ b/src/world.h @@ -53,15 +53,25 @@ class World { return this->age_; } + std::size_t getPopulation() const { + return this->population_; + } + void summonLifeAt(std::size_t x, std::size_t y) { if ( this->area_(x, y) ) { - this->matrix_[y][x] = true; + if ( !this->matrix_[y][x] ) { + this->matrix_[y][x] = true; + this->population_ += 1; + } } } void extinguishLifeAt(std::size_t x, std::size_t y) { if ( this->area_(x, y) ) { - this->matrix_[y][x] = false; + if ( this->matrix_[y][x] ) { + this->matrix_[y][x] = false; + this->population_ -= 1; + } } } @@ -111,6 +121,7 @@ class World { const util::BoxTraverser area_; std::size_t age_{}; + std::size_t population_{}; std::array, HEIGHT> matrix_; }; -- cgit v1.2.3