#include "Maze.h" #include "../Entities/Player.h" #include "../Exceptions/MalformedMaze.h" #include "../Entities/Entity.h" using game_exceptions::MalformedMaze; namespace game { Maze::Maze(const vector> play_field, const vector player_start_position, const vector enemies): field(play_field), player_start_position(PositionVector{player_start_position[1], player_start_position[0]}) { 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 PositionVector& 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 { 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 << " "; } cout << "\n"; } } char Maze::get_field(const PositionVector& pos) const { return this->field[pos.y][pos.x]; } void Maze::update_field(const PositionVector& pos, const char& target) { this->field[pos.y][pos.x] = target; } PositionVector Maze::get_player_start_position() const { return this->player_start_position; } PositionVector Maze::get_distance_pos1_pos2(const PositionVector& pos1, const PositionVector& pos2) { int x_diff = pos1.x - pos2.x; int y_diff = pos1.y - pos2.y; return {x_diff, y_diff}; } int Maze::calculate_steps(const PositionVector& target_position, PositionVector position, int steps) { if (this->is_pos_free(position, false)) return 999; if (target_position.eq(position)) return 0; if (steps <= 0) return 999; vector i; } } // game