From 56726a6c0538f82d786e6795ee2b72ed40f23906 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sat, 26 Nov 2011 20:40:05 +0100 Subject: [PATCH] Get rid of class position --- agent.cc | 5 ++--- agent.h | 12 +++++++----- main.cc | 2 +- map.cc | 21 ++++++++++----------- map.h | 43 ++++++++++++++++++++----------------------- 5 files changed, 40 insertions(+), 43 deletions(-) diff --git a/agent.cc b/agent.cc index f0a944e..de7227f 100644 --- a/agent.cc +++ b/agent.cc @@ -6,10 +6,9 @@ #include "map.h" void -agent::put_at(struct position *pos) +agent::put_at(class tile &t) { - class tile *t = &pos->map->tile_at(*pos); - if (!t->on_agent_enter(this)) { + if (!t.on_agent_enter(*this)) { std::cerr << "Collision."; exit(EXIT_FAILURE); } diff --git a/agent.h b/agent.h index 4e5b8fc..aaf48ca 100644 --- a/agent.h +++ b/agent.h @@ -1,19 +1,21 @@ #ifndef BRMLIFE__AGENT_H #define BRMLIFE__AGENT_H -class position; +#include "map.h" class agent { public: int id; - class position *pos; + class tile *tile; - agent(int id_, class position &pos_) : id (id_), pos (&pos_) + agent(int id_, class tile &tile_) : id (id_), tile (&tile_) { - put_at(pos); + put_at(tile_); }; - void put_at(struct position *pos); +private: + /* Just for initial placement. */ + void put_at(class tile &tile); }; #endif diff --git a/main.cc b/main.cc index 5dc4b32..e9f14fd 100644 --- a/main.cc +++ b/main.cc @@ -8,7 +8,7 @@ main(int argc, char *argv[]) { class map map(10, 10); - class position agentpos(4, 4, map); + class tile &agentpos = map.tile_at(4, 4); class agent agent(0, agentpos); map.print_map(); diff --git a/map.cc b/map.cc index a4fc0c5..8dbc7e6 100644 --- a/map.cc +++ b/map.cc @@ -5,17 +5,17 @@ #include "map.h" bool -tile::on_agent_enter(class agent *a) +tile::on_agent_enter(class agent &a) { if (agent) return false; - agent = a; + agent = &a; return true; } void -tile::on_agent_leave(class agent *a) +tile::on_agent_leave(class agent &a) { - assert(a == agent); + assert(&a == agent); agent = NULL; } @@ -38,12 +38,12 @@ tile_ground::type_symbol(void) return '.'; } -class position * -position::next_in(int dir_x, int dir_y) +class tile & +tile::tile_in_dir(int dir_x, int dir_y) { - int x2 = (map->w + x + dir_x) % map->w; - int y2 = (map->h + y + dir_y) % map->h; - return new position(x2, y2, *map); + int x2 = (map.w + x + dir_x) % map.w; + int y2 = (map.h + y + dir_y) % map.h; + return map.tile_at(x2, y2); } @@ -52,8 +52,7 @@ map::print_map(void) { for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { - class position p = position(x, y, *this); - std::cout << tile_at(p).symbol(); + std::cout << tile_at(x, y).symbol(); } std::cout << '\n'; } diff --git a/map.h b/map.h index e9fc62e..9dddd0b 100644 --- a/map.h +++ b/map.h @@ -6,34 +6,29 @@ class map; class tile { public: + int x, y; + class map ↦ + class agent *agent; - virtual bool on_agent_enter(class agent *); - virtual void on_agent_leave(class agent *); - - virtual void on_tick(void); + tile(int x_, int y_, class map &map_) : x(x_), y(y_), map(map_), agent(NULL) {}; char symbol(void); virtual char type_symbol(void) = 0; + + class tile &tile_in_dir(int dir_x, int dir_y); + + virtual bool on_agent_enter(class agent &); + virtual void on_agent_leave(class agent &); + + virtual void on_tick(void); }; class tile_ground : public tile { - char type_symbol(void); -}; - -class position { public: - int x, y; - class map *map; - - position(int x_, int y_, class map &map_) - { - x = x_; - y = y_; - map = &map_; - }; - - class position *next_in(int dir_x, int dir_y); + tile_ground(int x_, int y_, struct map &map_) : tile(x_, y_, map_) {}; +private: + char type_symbol(void); }; class map { @@ -47,14 +42,16 @@ public: h = h_; tiles = new class tile * [w * h]; - for (int i = 0; i < w * h; i++) { - tiles[i] = new tile_ground; + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + tiles[y * h + x] = new tile_ground(x, y, *this); + } } }; - class tile &tile_at(class position &pos) + class tile &tile_at(int x, int y) { - return *tiles[pos.y * h + pos.x]; + return *tiles[y * h + x]; }; void print_map(void);