From 273e1357621198fa6d9301b5a3b17eec92483c9b Mon Sep 17 00:00:00 2001 From: moonleay Date: Tue, 17 Dec 2024 14:31:47 +0100 Subject: [PATCH] feat: added game exit --- CMakeLists.txt | 1 + Exceptions/ExitGame.h | 26 ++++++++++++++++++++++++++ Game.cpp | 8 +++++++- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Exceptions/ExitGame.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 156e8a6..f632763 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,4 +21,5 @@ add_executable(epr24pr3_ojanssen2 Exceptions/MovementNotPossible.h Exceptions/MalformedMaze.h Exceptions/UnkownAction.h + Exceptions/ExitGame.h ) diff --git a/Exceptions/ExitGame.h b/Exceptions/ExitGame.h new file mode 100644 index 0000000..29cabc3 --- /dev/null +++ b/Exceptions/ExitGame.h @@ -0,0 +1,26 @@ +// +// Created by moonleay on 12/17/24. +// + +#ifndef EXITGAME_H +#define EXITGAME_H + +namespace game_exceptions +{ + class ExitGame + { + string why; + + public: + ExitGame(string why): why(why) + { + } + + string what() + { + return this->why; + } + }; +} // game_exceptions + +#endif //EXITGAME_H diff --git a/Game.cpp b/Game.cpp index 7164826..fd012d1 100644 --- a/Game.cpp +++ b/Game.cpp @@ -6,6 +6,7 @@ #include "MazeParser.h" #include "Player.h" #include "Maze.h" +#include "Exceptions/ExitGame.h" #include "Exceptions/MovementNotPossible.h" #include "Exceptions/UnkownAction.h" @@ -13,6 +14,7 @@ using game::Player; using game::Maze; using game_exceptions::UnkownAction; using game_exceptions::MovementNotPossible; +using game_exceptions::ExitGame; namespace game // Namespace = Scope with name and no features { @@ -50,6 +52,8 @@ namespace game // Namespace = Scope with name and no features case 'd': movement_vector.update(1, 0); break; + case 'q': + throw ExitGame("Schoenen Tag noch!"); case 'h': // Schreibe hilfsreiche Tipps in die Konsole cout << @@ -102,11 +106,13 @@ namespace game // Namespace = Scope with name and no features { movement_vector = handle_user_input(game_input); } catch (UnkownAction& err) + { + cout << err.what() << "\n"; + } catch (ExitGame& err) { cout << err.what() << "\n"; } - // Kontrolliere gewollte Bewegung und setze sie um. try {