Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef Cell_cpp
- #define Cell_cpp
- #include "Cell.h"
- #include <algorithm>
- #include <map>
- #include <random>
- #include <vector>
- #include <iostream>
- Cell::Cell(){
- this->event = nullptr;
- this->passability = true;
- this->player_presence = false;
- this->walls = std::map<MOVEMENT, bool>{
- {MOVEMENT::UP, true}, {MOVEMENT::RIGHT, true}, {MOVEMENT::DOWN, true}, {MOVEMENT::LEFT, true}
- };
- this->cell_coords = std::make_pair(0, 0);
- }
- Cell::Cell(Event* event, bool passability, bool player_presence,
- std::map<MOVEMENT, bool> walls, std::pair<int, int> cell_coords){
- this->passability = passability;
- this->player_presence = player_presence;
- this->event = event;
- this->walls = walls;
- this->cell_coords = cell_coords;
- }
- void Cell::set_pass(bool value){
- this->passability = value;
- }
- bool Cell::get_pass() const{
- return this->passability;
- }
- void Cell::set_player_presence(bool value){
- this->player_presence = value;
- }
- bool Cell::get_player_presence() const{
- return this->player_presence;
- }
- Cell::Cell(const Cell& other){
- this->passability = other.passability;
- this->player_presence = other.player_presence;
- this->event = other.event;
- this->walls = other.walls;
- this->cell_coords = other.cell_coords;
- }
- void Cell::cell_swap(Cell& other){
- std::swap(player_presence, other.player_presence);
- std::swap(passability, other.passability);
- std::swap(event, other.event);
- std::swap(walls, other.walls);
- std::swap(cell_coords, other.cell_coords);
- }
- Cell::Cell(Cell&& other){
- this->cell_swap(other);
- }
- Cell &Cell::operator=(Cell &&other){
- if(this != &other)
- this->cell_swap(other);
- return *this;
- }
- Cell &Cell::operator=(const Cell &other) = default;
- void Cell::set_event(Event* event){
- this->event = event;
- }
- Event* Cell::get_event() const{
- return this->event;
- }
- bool Cell::get_wall(MOVEMENT wall) const{
- return this->walls.at(wall);
- }
- void Cell::set_wall(MOVEMENT wall, bool value){
- this->walls[wall] = value;
- }
- std::pair<int, int> Cell::get_cell_coords() const{
- return this->cell_coords;
- }
- void Cell::set_cell_coords(int x, int y){
- this->cell_coords.first = x;
- this->cell_coords.second = y;
- }
- int Cell::check_cell(int height, int width, int x, int y){
- if (x < 0 || y < 0 || x > width - 1 || y > height - 1)
- return -1;
- else
- return x + y * width; // связь индекса клетки в одномерном массиве и ее координат на поле
- }
- Cell* Cell::check_neighbors(int height, int width, Cell** grid){ // проверяем соседей клетки
- int x = this->cell_coords.first;
- int y = this->cell_coords.second;
- std::vector<Cell*> neighbors;
- int up = this->check_cell(height, width, x, y - 1);
- int right = this->check_cell(height, width, x + 1, y);
- int down = this->check_cell(height, width, x, y + 1);
- int left = this->check_cell(height, width, x - 1, y);
- if(up != -1){ // в каждом if происходит проверка, на то, была ли посещена соответствующая клетка сосед
- if(!grid[up]->get_pass())
- neighbors.push_back(grid[up]);
- }
- if(right != -1){
- if(!grid[right]->get_pass())
- neighbors.push_back(grid[right]);
- }
- if(down != -1){
- if(!grid[down]->get_pass())
- neighbors.push_back(grid[down]);
- }
- if(left != -1){
- if(!grid[left]->get_pass())
- neighbors.push_back(grid[left]);
- }
- if (neighbors.size() > 0){// если нашлись подходящие клетки, то случайным образом выбираем следующую клетку для посещения
- std::random_device dev;
- std::mt19937 rng(dev());
- std::uniform_int_distribution<std::mt19937::result_type> dist(0, neighbors.size() - 1);
- return neighbors[dist(rng)];
- } else{ // если подходящей нет, создаем указатель на клетку сообщающую об этом
- Cell cell;
- Cell* c = &cell;
- c->set_cell_coords(-1, -1);
- return c;
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement