2025-01-18 01:21:58 +01:00
|
|
|
#include "MazeParser.h"
|
|
|
|
#include "../Environment/Maze.h"
|
|
|
|
#include "../Exceptions/MalformedMaze.h"
|
2025-01-18 19:10:38 +01:00
|
|
|
#include "../Entities/Entity.h"
|
2025-01-18 01:21:58 +01:00
|
|
|
|
|
|
|
using game_exceptions::MalformedMaze;
|
2025-01-18 19:10:38 +01:00
|
|
|
using game::Entity;
|
2025-01-18 01:21:58 +01:00
|
|
|
|
|
|
|
namespace game
|
|
|
|
{
|
|
|
|
vector<int> MazeParser::request_numbers_from_user(const int& amount_of_numbers)
|
|
|
|
{
|
|
|
|
int input;
|
|
|
|
vector<int> list;
|
|
|
|
|
|
|
|
for (int i = 0; i < amount_of_numbers; ++i)
|
|
|
|
{
|
|
|
|
cin >> input;
|
|
|
|
if (!cin)
|
|
|
|
throw MalformedMaze("Cin failed while reading numbers!");
|
|
|
|
|
|
|
|
if (input > MAX_MAZE_SIZE)
|
|
|
|
throw MalformedMaze("This maze is too big");
|
|
|
|
list.push_back(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MazeParser::validate_maze_element(const char& target)
|
|
|
|
{
|
|
|
|
for (const char c : valid_maze_elements)
|
|
|
|
if (c == target)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-01-18 19:10:38 +01:00
|
|
|
bool MazeParser::is_valid_enemy(const char& target) {
|
|
|
|
for (const char c : valid_enemies)
|
|
|
|
if (c == target)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-01-18 01:21:58 +01:00
|
|
|
Maze MazeParser::request_maze_from_user()
|
|
|
|
{
|
|
|
|
vector<int> maze_size = request_numbers_from_user(2);
|
|
|
|
|
|
|
|
char input;
|
|
|
|
vector<vector<char>> field;
|
2025-01-18 19:10:38 +01:00
|
|
|
vector<Entity> enemies;
|
2025-01-18 01:21:58 +01:00
|
|
|
|
|
|
|
for (int y = 0; y < maze_size[0]; ++y)
|
|
|
|
{
|
|
|
|
vector<char> row;
|
|
|
|
for (int x = 0; x < maze_size[1]; ++x)
|
|
|
|
{
|
|
|
|
cin >> input;
|
|
|
|
if (!cin)
|
|
|
|
throw MalformedMaze("Cin failed while reading chars!");
|
|
|
|
|
2025-01-18 19:10:38 +01:00
|
|
|
// I don't think that this is needed. I doubt that they test the check for this one ~Eric
|
|
|
|
// if (input == 'q')
|
2025-01-18 01:21:58 +01:00
|
|
|
// throw ExitGame("Schoenen Tag noch!");
|
|
|
|
|
|
|
|
if (!validate_maze_element(input))
|
|
|
|
throw MalformedMaze("The given input is not a valid element of a maze!");
|
2025-01-19 04:20:02 +01:00
|
|
|
if (is_valid_enemy(input))
|
|
|
|
{
|
2025-01-18 19:10:38 +01:00
|
|
|
enemies.push_back(Entity({x, y}, input));
|
|
|
|
row.push_back('.');
|
|
|
|
}
|
2025-01-19 04:20:02 +01:00
|
|
|
else {
|
|
|
|
row.push_back(input);
|
|
|
|
}
|
2025-01-18 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
field.push_back(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<int> player_start_pos = request_numbers_from_user(2);
|
|
|
|
|
2025-01-19 04:20:02 +01:00
|
|
|
return {field, player_start_pos, enemies};
|
2025-01-18 01:21:58 +01:00
|
|
|
} // Beispieleingabe: `4 3 #.# #.K #T# #Z# 0 1`
|
|
|
|
} // game
|