2025-01-18 01:21:58 +01:00
|
|
|
#include "Maze.h"
|
|
|
|
#include "../Entities/Player.h"
|
|
|
|
#include "../Exceptions/MalformedMaze.h"
|
|
|
|
#include "../Entities/Entity.h"
|
2025-01-19 04:20:02 +01:00
|
|
|
#include "../Util/MathUtil.h"
|
2025-01-18 01:21:58 +01:00
|
|
|
|
|
|
|
using game_exceptions::MalformedMaze;
|
|
|
|
|
|
|
|
namespace game
|
|
|
|
{
|
2025-01-19 04:20:02 +01:00
|
|
|
class MathUtil;
|
|
|
|
|
|
|
|
Maze::Maze(const vector<vector<char>>& play_field, const vector<int>& player_start_position, const vector<Entity>& enemies):
|
2025-01-18 01:21:58 +01:00
|
|
|
field(play_field),
|
2025-01-19 04:20:02 +01:00
|
|
|
player_start_position(Vector2d{player_start_position[1], player_start_position[0]}),
|
|
|
|
enemies(enemies)
|
2025-01-18 01:21:58 +01:00
|
|
|
{
|
|
|
|
if (!this->is_pos_free(this->player_start_position, false))
|
|
|
|
throw MalformedMaze("Player oob");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Maze::was_player_killed_by_ghost(const Player& player) const
|
|
|
|
{
|
|
|
|
return this->field[player.get_pos().y][player.get_pos().x] == 'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Maze::is_player_at_goal(const Player& player) const
|
|
|
|
{
|
|
|
|
return this->field[player.get_pos().y][player.get_pos().x] == 'Z';
|
|
|
|
}
|
|
|
|
|
2025-01-19 04:20:02 +01:00
|
|
|
bool Maze::is_pos_free(const Vector2d& pos, const bool& player_has_key) const
|
2025-01-18 01:21:58 +01:00
|
|
|
{
|
|
|
|
if (pos.x < 0 || pos.y < 0 || pos.y > field.size() - 1 || pos.x > field[pos.y].size() - 1)
|
|
|
|
return false;
|
|
|
|
if (field[pos.y][pos.x] == '#')
|
|
|
|
return false;
|
|
|
|
if (field[pos.y][pos.x] == 'T')
|
|
|
|
return player_has_key;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2025-01-19 04:20:02 +01:00
|
|
|
void Maze::render(const Player& player, const vector<Entity> entities, const bool& infomode_enabled)
|
2025-01-18 01:21:58 +01:00
|
|
|
{
|
|
|
|
for (int y = 0; y < field.size(); ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < field[y].size(); ++x)
|
|
|
|
{
|
|
|
|
if (y == player.get_pos().y && x == player.get_pos().x)
|
|
|
|
cout << "S";
|
|
|
|
else {
|
2025-01-18 19:10:38 +01:00
|
|
|
bool an_enemy_is_at_this_position = false;
|
|
|
|
for (Entity e : entities)
|
|
|
|
if (e.is_at_position({x, y})) {
|
|
|
|
an_enemy_is_at_this_position = true;
|
|
|
|
cout << e.get_display_character();
|
|
|
|
}
|
|
|
|
if (!an_enemy_is_at_this_position)
|
|
|
|
cout << field[y][x];
|
2025-01-18 01:21:58 +01:00
|
|
|
}
|
|
|
|
cout << " ";
|
|
|
|
}
|
2025-01-19 04:20:02 +01:00
|
|
|
if (y == 0 && infomode_enabled)
|
|
|
|
{
|
|
|
|
int steps = this->calculate_steps_until_win(player.get_pos(), 5);
|
|
|
|
if (steps > 999)
|
|
|
|
cout << steps << "Schritte bis zum Ziel";
|
|
|
|
}
|
2025-01-18 01:21:58 +01:00
|
|
|
cout << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-19 04:20:02 +01:00
|
|
|
char Maze::get_field(const Vector2d& pos) const
|
2025-01-18 01:21:58 +01:00
|
|
|
{
|
|
|
|
return this->field[pos.y][pos.x];
|
|
|
|
}
|
|
|
|
|
2025-01-19 04:20:02 +01:00
|
|
|
void Maze::update_field(const Vector2d& pos, const char& target)
|
2025-01-18 01:21:58 +01:00
|
|
|
{
|
|
|
|
this->field[pos.y][pos.x] = target;
|
|
|
|
}
|
|
|
|
|
2025-01-19 04:20:02 +01:00
|
|
|
Vector2d Maze::get_player_start_position() const
|
2025-01-18 01:21:58 +01:00
|
|
|
{
|
|
|
|
return this->player_start_position;
|
|
|
|
}
|
2025-01-18 19:10:38 +01:00
|
|
|
|
2025-01-19 04:20:02 +01:00
|
|
|
Vector2d Maze::get_delta_vector(const Vector2d& pos1, const Vector2d& pos2) const {
|
2025-01-18 19:10:38 +01:00
|
|
|
int x_diff = pos1.x - pos2.x;
|
|
|
|
int y_diff = pos1.y - pos2.y;
|
2025-01-19 04:20:02 +01:00
|
|
|
|
2025-01-18 19:10:38 +01:00
|
|
|
return {x_diff, y_diff};
|
|
|
|
}
|
|
|
|
|
2025-01-19 04:20:02 +01:00
|
|
|
int Maze::calculate_steps_until_win(const Vector2d& position, const int& steps) {
|
|
|
|
if (!this->is_pos_free(position, false))
|
2025-01-18 19:10:38 +01:00
|
|
|
return 999;
|
2025-01-19 04:20:02 +01:00
|
|
|
if (this->get_field(position) == 'Z')
|
2025-01-18 19:10:38 +01:00
|
|
|
return 0;
|
|
|
|
if (steps <= 0)
|
|
|
|
return 999;
|
2025-01-19 04:20:02 +01:00
|
|
|
|
2025-01-18 19:10:38 +01:00
|
|
|
vector<int> i;
|
2025-01-19 04:20:02 +01:00
|
|
|
i.push_back(this->calculate_steps_until_win(position.get_new_updated(0, -1), steps - 1));
|
|
|
|
i.push_back(this->calculate_steps_until_win(position.get_new_updated(0, 1), steps - 1));
|
|
|
|
i.push_back(this->calculate_steps_until_win(position.get_new_updated(1, 0), steps - 1));
|
|
|
|
i.push_back(this->calculate_steps_until_win(position.get_new_updated(-1, 0), steps - 1));
|
|
|
|
|
|
|
|
return MathUtil::get_min(i) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Entity> Maze::get_entities() {
|
|
|
|
return this->enemies;
|
2025-01-18 19:10:38 +01:00
|
|
|
}
|
2025-01-18 01:21:58 +01:00
|
|
|
} // game
|