aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-12-19 22:03:27 +0100
committerAdrian Kummerlaender2016-12-19 22:03:27 +0100
commit23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb (patch)
tree32cd9dd6231e5fff40d477a23543747c62b65a38
parenta653a3a2823e9bcecc80ec639cd5d2743f3edd04 (diff)
downloadtermlife-23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb.tar
termlife-23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb.tar.gz
termlife-23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb.tar.bz2
termlife-23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb.tar.lz
termlife-23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb.tar.xz
termlife-23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb.tar.zst
termlife-23a3e7a41a05ce3e4c8dcc2bc75ce366d8fd8fbb.zip
Add population display
-rw-r--r--life.cc7
-rw-r--r--src/world.h15
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<std::array<bool, WIDTH>, HEIGHT> matrix_;
};