2024-12-14 17:32:06 +01:00
|
|
|
#include "Maze.h"
|
2024-12-16 23:20:04 +01:00
|
|
|
#include "Player.h"
|
2024-12-17 15:09:50 +01:00
|
|
|
#include "Exceptions/MalformedMaze.h"
|
|
|
|
|
|
|
|
using game_exceptions::MalformedMaze;
|
2024-12-14 17:32:06 +01:00
|
|
|
|
|
|
|
namespace game
|
|
|
|
{
|
2024-12-16 23:20:04 +01:00
|
|
|
Maze::Maze(const vector<vector<char>> play_field, const vector<int> player_start_position):
|
|
|
|
field(play_field),
|
2024-12-17 15:09:50 +01:00
|
|
|
player_start_position(PositionVector{player_start_position[1], player_start_position[0]})
|
2024-12-14 17:32:06 +01:00
|
|
|
{
|
2024-12-17 15:09:50 +01:00
|
|
|
if (!this->is_pos_free(this->player_start_position, false))
|
|
|
|
throw MalformedMaze("Player oob");
|
2024-12-16 23:20:04 +01:00
|
|
|
}
|
2024-12-14 17:32:06 +01:00
|
|
|
|
2024-12-18 09:12:00 +01:00
|
|
|
bool Maze::was_player_killed_by_ghost(const Player& player) const
|
2024-12-16 23:20:04 +01:00
|
|
|
{
|
|
|
|
return this->field[player.get_pos().y][player.get_pos().x] == 'A';
|
2024-12-14 17:32:06 +01:00
|
|
|
}
|
|
|
|
|
2024-12-18 09:12:00 +01:00
|
|
|
bool Maze::is_player_at_goal(const Player& player) const
|
2024-12-14 17:32:06 +01:00
|
|
|
{
|
2024-12-16 23:20:04 +01:00
|
|
|
return this->field[player.get_pos().y][player.get_pos().x] == 'Z';
|
2024-12-14 17:32:06 +01:00
|
|
|
}
|
|
|
|
|
2024-12-18 09:12:00 +01:00
|
|
|
bool Maze::is_pos_free(const PositionVector& pos, const bool& player_has_key) const
|
2024-12-14 17:32:06 +01:00
|
|
|
{
|
|
|
|
if (pos.x < 0 || pos.y < 0 || pos.y > field.size() - 1 || pos.x > field[pos.y].size() - 1)
|
2024-12-18 09:12:00 +01:00
|
|
|
return false;
|
2024-12-14 17:32:06 +01:00
|
|
|
if (field[pos.y][pos.x] == '#')
|
2024-12-18 09:12:00 +01:00
|
|
|
return false;
|
2024-12-16 23:20:04 +01:00
|
|
|
if (field[pos.y][pos.x] == 'T')
|
2024-12-18 09:12:00 +01:00
|
|
|
return player_has_key;
|
|
|
|
return true;
|
2024-12-14 17:32:06 +01:00
|
|
|
}
|
|
|
|
|
2024-12-18 09:12:00 +01:00
|
|
|
void Maze::render(const Player& player) const
|
2024-12-14 17:32:06 +01:00
|
|
|
{
|
|
|
|
for (int y = 0; y < field.size(); ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < field[y].size(); ++x)
|
|
|
|
{
|
2024-12-16 23:20:04 +01:00
|
|
|
if (y == player.get_pos().y && x == player.get_pos().x)
|
2024-12-18 09:12:00 +01:00
|
|
|
cout << "S";
|
|
|
|
else
|
|
|
|
cout << field[y][x];
|
|
|
|
cout << " ";
|
2024-12-14 17:32:06 +01:00
|
|
|
}
|
2024-12-18 09:12:00 +01:00
|
|
|
cout << "\n";
|
2024-12-14 17:32:06 +01:00
|
|
|
}
|
|
|
|
}
|
2024-12-16 23:20:04 +01:00
|
|
|
|
2024-12-18 09:12:00 +01:00
|
|
|
char Maze::get_field(const PositionVector& pos) const
|
2024-12-16 23:20:04 +01:00
|
|
|
{
|
|
|
|
return this->field[pos.y][pos.x];
|
|
|
|
}
|
|
|
|
|
2024-12-18 09:12:00 +01:00
|
|
|
void Maze::update_field(const PositionVector& pos, const char& target)
|
2024-12-16 23:20:04 +01:00
|
|
|
{
|
|
|
|
this->field[pos.y][pos.x] = target;
|
|
|
|
}
|
|
|
|
|
2024-12-17 15:09:50 +01:00
|
|
|
PositionVector Maze::get_player_start_position() const
|
2024-12-16 23:20:04 +01:00
|
|
|
{
|
|
|
|
return this->player_start_position;
|
|
|
|
}
|
2024-12-14 17:32:06 +01:00
|
|
|
} // game
|