diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 14:48:02 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 14:48:02 +0100 |
commit | 6dfa3fb34fb0a2ea028fd46e77296e26b092fb99 (patch) | |
tree | 51257e54561550c91dbec262fe143d4dbe41f059 | |
parent | 862186ae7cbbd922057fa5f6b49509c36f9ade36 (diff) |
use string instead of argument vector for commands
-rw-r--r-- | backend/LocationFactory.h | 3 | ||||
-rw-r--r-- | frontend/Player.cpp | 10 | ||||
-rw-r--r-- | frontend/Player.h | 16 | ||||
-rw-r--r-- | frontend/cmd/cheat.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/equip.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/get.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/go.cpp | 8 | ||||
-rw-r--r-- | frontend/cmd/help.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/hit.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/put.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/query.cpp | 2 | ||||
-rw-r--r-- | frontend/cmd/quit.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/restart.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/search.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/use.cpp | 4 | ||||
-rw-r--r-- | frontend/cmd/view.cpp | 8 | ||||
-rw-r--r-- | frontend/cmd/wait.cpp | 4 | ||||
-rw-r--r-- | frontend/main.cpp | 3 | ||||
-rw-r--r-- | frontend/strings.cpp | 13 | ||||
-rw-r--r-- | frontend/strings.h | 1 |
20 files changed, 73 insertions, 35 deletions
diff --git a/backend/LocationFactory.h b/backend/LocationFactory.h index ba34ff3..2da590e 100644 --- a/backend/LocationFactory.h +++ b/backend/LocationFactory.h @@ -5,5 +5,8 @@ class LocationFactory { public: static Location * create_location(const char * name, const char * description); + +private: + LocationFactory() = delete; }; diff --git a/frontend/Player.cpp b/frontend/Player.cpp index d9974f7..c579f83 100644 --- a/frontend/Player.cpp +++ b/frontend/Player.cpp @@ -6,12 +6,6 @@ using namespace std; -static string argv_pop(vector<string> & argv) { - string el = argv.front(); - argv.erase(argv.begin()); - return el; -} - Player::Player(Dungeon & dungeon) : dungeon(dungeon), location(*dungeon.get_start_location()) @@ -41,9 +35,9 @@ void Player::cmdset_death() { this->cmds["Opnieuw"] = &Player::cmd_restart; } -FollowupAction Player::cmd(Argv argv) { +FollowupAction Player::cmd(string & argv) { if (argv.size() == 0) return FollowupAction::NONE; - string cmd = str_title(argv_pop(argv)); + string cmd = str_title(str_consume_arg(argv)); if (this->cmds.contains(cmd)) return (this->*cmds.at(cmd))(argv); diff --git a/frontend/Player.h b/frontend/Player.h index 94ec6a3..09e3a73 100644 --- a/frontend/Player.h +++ b/frontend/Player.h @@ -1,8 +1,11 @@ #pragma once -#include <vector> #include <unordered_map> #include <string> +#include <memory> + +#include "backend/WeaponObject.h" +#include "backend/ArmorObject.h" class Dungeon; class Location; @@ -15,16 +18,15 @@ enum FollowupAction { }; class Player { - typedef std::vector<std::string> & Argv; - typedef FollowupAction Cmd(Argv); + typedef FollowupAction Cmd(std::string &); private: std::string name; unsigned int health_points = 20; float attack_chance = 0.4; unsigned int gold = 0; - // TODO: WeaponObject[] - // TODO: ArmorObject[] + std::unique_ptr<WeaponObject> weapon = nullptr; + std::unique_ptr<ArmorObject> armor = nullptr; Location & location; bool cheating = false; @@ -33,7 +35,7 @@ public: virtual ~Player() = default; public: - FollowupAction cmd(Argv argv); + FollowupAction cmd(std::string &); public: void take_damage(unsigned int dmg); @@ -44,7 +46,7 @@ private: void cmdset_death(); private: - std::unordered_map<std::string, FollowupAction(Player::*)(Argv)> cmds; + std::unordered_map<std::string, FollowupAction(Player::*)(std::string &)> cmds; Cmd cmd_query; Cmd cmd_search; Cmd cmd_go; diff --git a/frontend/cmd/cheat.cpp b/frontend/cmd/cheat.cpp index 6f1c163..aeed5f4 100644 --- a/frontend/cmd/cheat.cpp +++ b/frontend/cmd/cheat.cpp @@ -1,7 +1,9 @@ #include "../Player.h" #include "../print.h" -FollowupAction Player::cmd_cheat(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_cheat(string &) { this->cheating = !this->cheating; lprtf("Cheats staan nu %s.\n", this->cheating ? "aan" : "uit"); return FollowupAction::NONE; diff --git a/frontend/cmd/equip.cpp b/frontend/cmd/equip.cpp index bcb261f..79ab8d1 100644 --- a/frontend/cmd/equip.cpp +++ b/frontend/cmd/equip.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_equip(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_equip(string & argv) { // TODO return FollowupAction::UPDATE; } diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp index b703a0e..77ad727 100644 --- a/frontend/cmd/get.cpp +++ b/frontend/cmd/get.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_get(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_get(string & argv) { // TODO return FollowupAction::NONE; } diff --git a/frontend/cmd/go.cpp b/frontend/cmd/go.cpp index cbe8e7c..624bf6c 100644 --- a/frontend/cmd/go.cpp +++ b/frontend/cmd/go.cpp @@ -2,6 +2,7 @@ #include "../Player.h" #include "../print.h" +#include "../strings.h" using namespace std; @@ -12,13 +13,14 @@ static const unordered_map<string, Direction> direction_map = { { "west", Direction::WEST }, }; -FollowupAction Player::cmd_go(Argv argv) { - if (argv.size() == 0 || !direction_map.contains(argv[0])) { +FollowupAction Player::cmd_go(string & argv) { + string direction_str = str_consume_arg(argv); + if (direction_str.size() == 0 || !direction_map.contains(direction_str)) { lprtf("Fout, gebruik: Ga <noord|zuid|oost|west>\n"); return FollowupAction::NONE; } - Direction direction = direction_map.at(argv[0]); + Direction direction = direction_map.at(direction_str); Location * next_location = this->location.get_exit(direction); if (next_location == nullptr) { lprtf("Er is geen uitgang in deze richting!\n"); diff --git a/frontend/cmd/help.cpp b/frontend/cmd/help.cpp index 1ae1fde..e614f28 100644 --- a/frontend/cmd/help.cpp +++ b/frontend/cmd/help.cpp @@ -1,7 +1,9 @@ #include "../print.h" #include "../Player.h" -FollowupAction Player::cmd_help(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_help(string &) { lprtf("De beschikbare commando's zijn: "); bool first = true; for (auto & [ key, _ ] : this->cmds) { diff --git a/frontend/cmd/hit.cpp b/frontend/cmd/hit.cpp index 17f9099..86d596a 100644 --- a/frontend/cmd/hit.cpp +++ b/frontend/cmd/hit.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_hit(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_hit(string & argv) { // TODO return FollowupAction::UPDATE; } diff --git a/frontend/cmd/put.cpp b/frontend/cmd/put.cpp index 8a6c828..6e7002b 100644 --- a/frontend/cmd/put.cpp +++ b/frontend/cmd/put.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_put(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_put(string & argv) { // TODO return FollowupAction::NONE; } diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp index 3a26e9c..e1e45a1 100644 --- a/frontend/cmd/query.cpp +++ b/frontend/cmd/query.cpp @@ -13,7 +13,7 @@ static const unordered_map<Direction, string> direction_map = { { Direction::WEST, "West" }, }; -FollowupAction Player::cmd_query(Argv argv) { +FollowupAction Player::cmd_query(string &) { lprtf("Je staat bij de locatie %s.\n", this->location.get_name()); lprtf("%s\n", this->location.get_description()); diff --git a/frontend/cmd/quit.cpp b/frontend/cmd/quit.cpp index 24aa7c9..8f97562 100644 --- a/frontend/cmd/quit.cpp +++ b/frontend/cmd/quit.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_quit(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_quit(string &) { return FollowupAction::EXIT; } diff --git a/frontend/cmd/restart.cpp b/frontend/cmd/restart.cpp index aac5833..d9d68e0 100644 --- a/frontend/cmd/restart.cpp +++ b/frontend/cmd/restart.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_restart(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_restart(string &) { return FollowupAction::RESTART; } diff --git a/frontend/cmd/search.cpp b/frontend/cmd/search.cpp index af22ca2..4646ddc 100644 --- a/frontend/cmd/search.cpp +++ b/frontend/cmd/search.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_search(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_search(string & argv) { // TODO return FollowupAction::UPDATE; } diff --git a/frontend/cmd/use.cpp b/frontend/cmd/use.cpp index a7955eb..a6e876a 100644 --- a/frontend/cmd/use.cpp +++ b/frontend/cmd/use.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_use(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_use(string & argv) { // TODO return FollowupAction::NONE; } diff --git a/frontend/cmd/view.cpp b/frontend/cmd/view.cpp index 0636cac..b9d179b 100644 --- a/frontend/cmd/view.cpp +++ b/frontend/cmd/view.cpp @@ -2,13 +2,15 @@ #include "../Player.h" #include "../strings.h" -FollowupAction Player::cmd_view(Argv argv) { - if (argv.size() == 0) { +using namespace std; + +FollowupAction Player::cmd_view(string & target) { + if (target.size() == 0) { lprtf("Fout, gebruik: Bekijk <Zelf|VIJHAND|OBJECT>\n"); return FollowupAction::NONE; } - if (str_lower(argv[0]) == "zelf") { + if (str_lower(target) == "zelf") { lprtf("Je hebt %d levenspunten.\n", this->health_points); lprtf("Je hebt een aanvalskans van %.0f%%.\n", this->get_attack() * 100); // TODO: weapon diff --git a/frontend/cmd/wait.cpp b/frontend/cmd/wait.cpp index d2bc908..49cb7db 100644 --- a/frontend/cmd/wait.cpp +++ b/frontend/cmd/wait.cpp @@ -1,6 +1,8 @@ #include "../Player.h" -FollowupAction Player::cmd_wait(Argv argv) { +using namespace std; + +FollowupAction Player::cmd_wait(string &) { return FollowupAction::NONE; } diff --git a/frontend/main.cpp b/frontend/main.cpp index deacdc5..366473d 100644 --- a/frontend/main.cpp +++ b/frontend/main.cpp @@ -37,8 +37,7 @@ FollowupAction game_main() { while (1) { string line = rl(); if (line.length() == 0) continue; - vector<string> argv = str_split(line, " "); - FollowupAction action = player.cmd(argv); + FollowupAction action = player.cmd(line); switch (action) { case NONE: break; diff --git a/frontend/strings.cpp b/frontend/strings.cpp index 70fed85..687309d 100644 --- a/frontend/strings.cpp +++ b/frontend/strings.cpp @@ -58,3 +58,16 @@ string str_title(const string & input) { return out; } +string str_consume_arg(string & argv) { + const char * delim = " \t"; + size_t start = argv.find_first_not_of(delim); + if (start == string::npos) start = 0; + size_t end = argv.find_first_of(delim, start); + if (end == string::npos) end = argv.size(); + string out = argv.substr(start, end - start); + size_t nextarg = argv.find_first_not_of(delim, end); + if (nextarg == string::npos) nextarg = end; + argv = argv.substr(nextarg); + return out; +} + diff --git a/frontend/strings.h b/frontend/strings.h index affc835..61236a6 100644 --- a/frontend/strings.h +++ b/frontend/strings.h @@ -21,4 +21,5 @@ std::string str_wrap(const char *); std::vector<std::string> str_split(const std::string & src, const std::string & delim); std::string str_lower(const std::string & input); std::string str_title(const std::string & input); +std::string str_consume_arg(std::string & argv); |