epr24pr42-ojanssen2/src/Util/MazeParser.cpp

70 lines
1.9 KiB
C++
Raw Normal View History

#include "MazeParser.h"
#include "../Environment/Maze.h"
#include "../Exceptions/MalformedMaze.h"
using game_exceptions::MalformedMaze;
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;
}
Maze MazeParser::request_maze_from_user()
{
vector<int> maze_size = request_numbers_from_user(2);
char input;
vector<vector<char>> field;
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!");
// I don't think that this is needed. I doubt that the test check for this one ~Eric
//if (input == 'q')
// throw ExitGame("Schoenen Tag noch!");
if (!validate_maze_element(input))
throw MalformedMaze("The given input is not a valid element of a maze!");
row.push_back(input);
}
field.push_back(row);
}
vector<int> player_start_pos = request_numbers_from_user(2);
return Maze(field, player_start_pos);
} // Beispieleingabe: `4 3 #.# #.K #T# #Z# 0 1`
} // game