From 12b74f12d426773bddb0276d8490e5d198d6cb21 Mon Sep 17 00:00:00 2001 From: Peisong Xiao Date: Sun, 30 Jun 2024 03:24:19 -0400 Subject: [PATCH] first working demo --- src/display.cc | 2 +- src/main.cc | 3 +++ src/map.cc | 13 +++++++++---- src/map.h | 31 +++---------------------------- src/position.cc | 19 ++++++------------- 5 files changed, 22 insertions(+), 46 deletions(-) diff --git a/src/display.cc b/src/display.cc index cc1d450..439ba35 100644 --- a/src/display.cc +++ b/src/display.cc @@ -9,7 +9,7 @@ void display::clear() { void display::print(std::ostream &out) const { for (auto line : contents) - out << line << std::endl; + out << line; } void display::print_line(const std::string &line, const int linenum) { diff --git a/src/main.cc b/src/main.cc index cd78a22..205aefe 100644 --- a/src/main.cc +++ b/src/main.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include "map.h" #include "races.h" #include "display.h" @@ -23,6 +24,8 @@ int main() { display.clear(); mmap->print(display); display.print_position(player->get_position(), '@'); + display.print(cout); + cout << "Command: " << ch << endl; auto available_positions = mmap->get_available_positions(); diff --git a/src/map.cc b/src/map.cc index 437202d..777c5e3 100644 --- a/src/map.cc +++ b/src/map.cc @@ -1,23 +1,26 @@ #include "map.h" #include +#include #include #include game_map::game_map(int lvl): - layer{layer_num::map}, map{MAP_HEIGHT}, level{lvl} { + layer{layer_num::map} { + level = lvl; // TODO: randomly generate a map } game_map::game_map(const std::string &map_data, int lvl): - layer{layer_num::map}, map{MAP_HEIGHT}, level{lvl} { + layer{layer_num::map} { + level = lvl; std::istringstream iss{map_data}; std::string line; for (int i = 0; i < MAP_HEIGHT; ++i) { getline(iss, line); - map.push_back(line); + map[i] = line; } } @@ -32,7 +35,9 @@ std::vector game_map::get_available_positions() const { for (int line = 0; line < MAP_HEIGHT; ++line) for (int x = 0; x < MAP_WIDTH; ++x) - if (map[line][x] == '.') + if (map[line][x] == '.' || + map[line][x] == '#' || + map[line][x] == '+') result.push_back(position{x, line}); return result; } diff --git a/src/map.h b/src/map.h index 492a4b0..de0477d 100644 --- a/src/map.h +++ b/src/map.h @@ -42,14 +42,14 @@ public: // just in case you want to retain the potion effects void enter_level(character *who); void leave_level(character *who); -private: +public: struct effect { character *who; stat_name statn; int amount; }; - std::vector map; + std::string map[MAP_HEIGHT]; int level; // use this to remember every applied potion @@ -57,31 +57,6 @@ private: }; const std::string default_map = - "|-----------------------------------------------------------------------------|\ -| |\ -| |--------------------------| |-----------------------| |\ -| |..........................| |.......................| |\ -| |..........................+########+.......................|-------| |\ -| |..........................| # |...............................|--| |\ -| |..........................| # |..................................|--| |\ -| |----------+---------------| # |----+----------------|...............| |\ -| # ############# |...............| |\ -| # # |-----+------| |...............| |\ -| # # |............| |...............| |\ -| ################### |............| ######+...............| |\ -| # # |............| # |...............| |\ -| # # |-----+------| # |--------+------| |\ -| |---------+-----------| # # # # |\ -| |.....................| # # # |----+------| |\ -| |.....................| ######################## |...........| |\ -| |.....................| # # |...........| |\ -| |.....................| # |------+--------------------|...........| |\ -| |.....................| # |.......................................| |\ -| |.....................+##########+.......................................| |\ -| |.....................| |.......................................| |\ -| |---------------------| |---------------------------------------| |\ -| |\ -|-----------------------------------------------------------------------------|\ -"; + "|-----------------------------------------------------------------------------|\n| |\n| |--------------------------| |-----------------------| |\n| |..........................| |.......................| |\n| |..........................+########+.......................|-------| |\n| |..........................| # |...............................|--| |\n| |..........................| # |..................................|--| |\n| |----------+---------------| # |----+----------------|...............| |\n| # ############# |...............| |\n| # # |-----+------| |...............| |\n| # # |............| |...............| |\n| ################### |............| ######+...............| |\n| # # |............| # |...............| |\n| # # |-----+------| # |--------+------| |\n| |---------+-----------| # # # # |\n| |.....................| # # # |----+------| |\n| |.....................| ######################## |...........| |\n| |.....................| # # |...........| |\n| |.....................| # |------+--------------------|...........| |\n| |.....................| # |.......................................| |\n| |.....................+##########+.......................................| |\n| |.....................| |.......................................| |\n| |---------------------| |---------------------------------------| |\n| |\n|-----------------------------------------------------------------------------|"; #endif diff --git a/src/position.cc b/src/position.cc index b4f2cdc..63609e5 100644 --- a/src/position.cc +++ b/src/position.cc @@ -31,22 +31,15 @@ bool position::operator<(const position &other) const { return y < other.y || x < other.x; } +#include + size_t find(const std::vector &sorted_list, const position &target) { - int l = 0; - int r = sorted_list.size() - 1; + // TODO: implement binary searching - while (l <= r) { - int mid = l + (l + r) / 2; - - if (target == sorted_list[mid]) - return mid; - - if (target < sorted_list[mid]) - l = mid + 1; - else - r = mid - 1; - } + for (size_t i = 0; i < sorted_list.size(); ++i) + if (sorted_list[i] == target) + return i; return sorted_list.size(); }