aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-14 16:20:52 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-14 16:20:52 +0200
commit33bed565c7dc5e419995702b854f618eb13847e5 (patch)
treea3855ec638b7bff585fa0f2f2a4576d2e24a0c56
parent212734b31102b11f4819c6676270baa1c99ea27b (diff)
small refactoring
-rw-r--r--BlueTileBehavior.cpp12
-rw-r--r--BlueTileBehavior.h2
-rw-r--r--GrayTileBehavior.cpp8
-rw-r--r--GrayTileBehavior.h2
-rw-r--r--NullTileBehavior.cpp2
-rw-r--r--NullTileBehavior.h2
-rw-r--r--RedTileBehavior.cpp8
-rw-r--r--RedTileBehavior.h2
-rw-r--r--Tile.cpp9
-rw-r--r--Tile.h3
-rw-r--r--YellowTileBehavior.cpp7
-rw-r--r--YellowTileBehavior.h2
12 files changed, 34 insertions, 25 deletions
diff --git a/BlueTileBehavior.cpp b/BlueTileBehavior.cpp
index fbca527..487398a 100644
--- a/BlueTileBehavior.cpp
+++ b/BlueTileBehavior.cpp
@@ -1,13 +1,13 @@
#include <memory>
#include "BlueTileBehavior.h"
+#include "YellowTileBehavior.h"
#include "Artist.h"
#include "Tile.h"
-#include "TileData.h"
using namespace std;
-BlueTileBehavior BlueTileBehavior::instance {"B"};
+BlueTileBehavior BlueTileBehavior::instance { BlueTileBehavior::type };
void BlueTileBehavior::step(Artist & artist) {
this->interactions++;
@@ -20,9 +20,7 @@ void BlueTileBehavior::step(Artist & artist) {
void BlueTileBehavior::update_neighbor(Tile * tile) {
if (tile == nullptr) return;
- TileData new_data = tile->data;
- new_data.type = "B";
- tile->set_data(new_data);
+ tile->set_type(BlueTileBehavior::type);
}
void BlueTileBehavior::update(Tile & tile) {
@@ -31,9 +29,7 @@ void BlueTileBehavior::update(Tile & tile) {
this->update_neighbor(tile.get_neighbor(this->dx, this->dy));
this->update_neighbor(tile.get_neighbor(-this->dx, -this->dy));
- TileData new_data = tile.data;
- new_data.type = "Y";
- tile.set_data(new_data);
+ tile.set_type(YellowTileBehavior::type);
}
unique_ptr<TileBehaviorStrategy> BlueTileBehavior::clone(Museum & museum) {
diff --git a/BlueTileBehavior.h b/BlueTileBehavior.h
index 16566c1..a01b50d 100644
--- a/BlueTileBehavior.h
+++ b/BlueTileBehavior.h
@@ -8,6 +8,8 @@ public:
virtual void update(Tile &);
virtual std::unique_ptr<TileBehaviorStrategy> clone(Museum &);
+ static constexpr const char * type = "B";
+
private:
using TileBehaviorStrategy::TileBehaviorStrategy;
static BlueTileBehavior instance;
diff --git a/GrayTileBehavior.cpp b/GrayTileBehavior.cpp
index 31acb33..257c32d 100644
--- a/GrayTileBehavior.cpp
+++ b/GrayTileBehavior.cpp
@@ -1,18 +1,16 @@
#include <memory>
#include "GrayTileBehavior.h"
+#include "RedTileBehavior.h"
#include "Tile.h"
-#include "TileData.h"
using namespace std;
-GrayTileBehavior GrayTileBehavior::instance {"G"};
+GrayTileBehavior GrayTileBehavior::instance { GrayTileBehavior::type };
void GrayTileBehavior::update(Tile & tile) {
if (this->interactions < 3) return;
- TileData new_data = tile.data;
- new_data.type = "R";
- tile.set_data(new_data);
+ tile.set_type(RedTileBehavior::type);
}
unique_ptr<TileBehaviorStrategy> GrayTileBehavior::clone(Museum & museum) {
diff --git a/GrayTileBehavior.h b/GrayTileBehavior.h
index ceba350..72c3fd8 100644
--- a/GrayTileBehavior.h
+++ b/GrayTileBehavior.h
@@ -7,6 +7,8 @@ public:
virtual void update(Tile &);
virtual std::unique_ptr<TileBehaviorStrategy> clone(Museum &);
+ static constexpr const char * type = "G";
+
private:
using TileBehaviorStrategy::TileBehaviorStrategy;
static GrayTileBehavior instance;
diff --git a/NullTileBehavior.cpp b/NullTileBehavior.cpp
index 64bf622..3e671bc 100644
--- a/NullTileBehavior.cpp
+++ b/NullTileBehavior.cpp
@@ -4,7 +4,7 @@
using namespace std;
-NullTileBehavior NullTileBehavior::instance {""};
+NullTileBehavior NullTileBehavior::instance { NullTileBehavior::type };
void NullTileBehavior::update(Tile &) { }
diff --git a/NullTileBehavior.h b/NullTileBehavior.h
index f47770e..9b52c48 100644
--- a/NullTileBehavior.h
+++ b/NullTileBehavior.h
@@ -7,6 +7,8 @@ public:
virtual void update(Tile &);
virtual std::unique_ptr<TileBehaviorStrategy> clone(Museum &);
+ static constexpr const char * type = "";
+
private:
using TileBehaviorStrategy::TileBehaviorStrategy;
static NullTileBehavior instance;
diff --git a/RedTileBehavior.cpp b/RedTileBehavior.cpp
index 245b886..0c0dff4 100644
--- a/RedTileBehavior.cpp
+++ b/RedTileBehavior.cpp
@@ -1,13 +1,13 @@
#include <memory>
#include "RedTileBehavior.h"
+#include "BlueTileBehavior.h"
#include "Tile.h"
-#include "TileData.h"
#include "Museum.h"
using namespace std;
-RedTileBehavior RedTileBehavior::instance {"R"};
+RedTileBehavior RedTileBehavior::instance { RedTileBehavior::type };
void RedTileBehavior::step(Artist & artist) {
this->interactions++;
@@ -16,9 +16,7 @@ void RedTileBehavior::step(Artist & artist) {
void RedTileBehavior::update(Tile & tile) {
if (this->interactions == 0) return;
- TileData new_data = tile.data;
- new_data.type = "B";
- tile.set_data(new_data);
+ tile.set_type(BlueTileBehavior::type);
}
unique_ptr<TileBehaviorStrategy> RedTileBehavior::clone(Museum & museum) {
diff --git a/RedTileBehavior.h b/RedTileBehavior.h
index a2c530e..1ac3e64 100644
--- a/RedTileBehavior.h
+++ b/RedTileBehavior.h
@@ -8,6 +8,8 @@ public:
virtual void update(Tile &);
virtual std::unique_ptr<TileBehaviorStrategy> clone(Museum &);
+ static constexpr const char * type = "R";
+
private:
using TileBehaviorStrategy::TileBehaviorStrategy;
static RedTileBehavior instance;
diff --git a/Tile.cpp b/Tile.cpp
index 1f49d96..7c235f5 100644
--- a/Tile.cpp
+++ b/Tile.cpp
@@ -5,10 +5,17 @@
#include "TileBehavior.h"
#include "Museum.h"
-Tile::Tile(Museum & museum, TileData data) : museum(museum) {
+using namespace std;
+
+Tile::Tile(Museum & museum, TileData & data) : museum(museum) {
this->set_data(data);
}
+void Tile::set_type(const string & type) {
+ this->data.type = type;
+ this->set_data(this->data);
+}
+
void Tile::set_data(TileData & data) {
this->data = data;
this->color = TileAppearance::get_color(this->data.type);
diff --git a/Tile.h b/Tile.h
index 9575c99..d2a83a8 100644
--- a/Tile.h
+++ b/Tile.h
@@ -10,7 +10,7 @@ class Museum;
class Tile {
public:
- Tile(Museum & museum, TileData data);
+ Tile(Museum & museum, TileData & data);
public:
TileData data;
@@ -22,6 +22,7 @@ public:
public:
void set_data(TileData & data);
+ void set_type(const std::string & type);
void update();
void step(Artist &);
Tile * get_neighbor(int dx, int dy);
diff --git a/YellowTileBehavior.cpp b/YellowTileBehavior.cpp
index 5697aab..0c2ee35 100644
--- a/YellowTileBehavior.cpp
+++ b/YellowTileBehavior.cpp
@@ -2,6 +2,7 @@
#include <random>
#include "YellowTileBehavior.h"
+#include "GrayTileBehavior.h"
#include "Tile.h"
#include "Museum.h"
@@ -11,7 +12,7 @@ using std::random_device;
using std::mt19937;
using namespace std;
-YellowTileBehavior YellowTileBehavior::instance {"Y"};
+YellowTileBehavior YellowTileBehavior::instance { YellowTileBehavior::type };
random_device dev{};
mt19937 rng(dev());
@@ -34,9 +35,7 @@ void YellowTileBehavior::update(Tile & tile) {
}
if (this->interactions < 2) return;
- TileData new_data = tile.data;
- new_data.type = "G";
- tile.set_data(new_data);
+ tile.set_type(GrayTileBehavior::type);
}
unique_ptr<TileBehaviorStrategy> YellowTileBehavior::clone(Museum & museum) {
diff --git a/YellowTileBehavior.h b/YellowTileBehavior.h
index 1b5b2ef..8de0b75 100644
--- a/YellowTileBehavior.h
+++ b/YellowTileBehavior.h
@@ -7,6 +7,8 @@ public:
virtual void update(Tile &);
virtual std::unique_ptr<TileBehaviorStrategy> clone(Museum &);
+ static constexpr const char * type = "Y";
+
private:
using TileBehaviorStrategy::TileBehaviorStrategy;
static YellowTileBehavior instance;