#include "Maze.h" #include "../Entities/Player.h" #include "../Exceptions/MalformedMaze.h" #include "../Entities/Entity.h" #include "../Util/MathUtil.h" using game_exceptions::MalformedMaze; namespace game { class MathUtil; Maze::Maze(const vector>& play_field, const vector& player_start_position, const vector& enemies): field(play_field), player_start_position(Vector2d{player_start_position[1], player_start_position[0]}), enemies(enemies) { 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'; } bool Maze::is_pos_free(const Vector2d& pos, const bool& player_has_key) const { 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; } void Maze::render(const Player& player, const vector entities, const bool& infomode_enabled) { 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 { 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]; } cout << " "; } 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"; } cout << "\n"; } } char Maze::get_field(const Vector2d& pos) const { return this->field[pos.y][pos.x]; } void Maze::update_field(const Vector2d& pos, const char& target) { this->field[pos.y][pos.x] = target; } Vector2d Maze::get_player_start_position() const { return this->player_start_position; } Vector2d Maze::get_delta_vector(const Vector2d& pos1, const Vector2d& pos2) const { int x_diff = pos1.x - pos2.x; int y_diff = pos1.y - pos2.y; return {x_diff, y_diff}; } int Maze::calculate_steps_until_win(const Vector2d& position, const int& steps) { if (!this->is_pos_free(position, false)) return 999; if (this->get_field(position) == 'Z') return 0; if (steps <= 0) return 999; vector i; 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 Maze::get_entities() { return this->enemies; } } // game