Advertisement
maxim_shlyahtin

Cell.cpp

Oct 26th, 2023
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #ifndef Cell_cpp
  2. #define Cell_cpp
  3. #include "Cell.h"
  4. #include <algorithm>
  5. #include <map>
  6. #include <random>
  7. #include <vector>
  8. #include <iostream>
  9.  
  10. Cell::Cell(){
  11.     this->event = nullptr;
  12.     this->passability = true;
  13.     this->player_presence = false;
  14.     this->walls = std::map<MOVEMENT, bool>{
  15.         {MOVEMENT::UP, true}, {MOVEMENT::RIGHT, true}, {MOVEMENT::DOWN, true}, {MOVEMENT::LEFT, true}
  16.     };
  17.     this->cell_coords = std::make_pair(0, 0);
  18. }
  19.  
  20. Cell::Cell(Event* event, bool passability, bool player_presence,
  21.     std::map<MOVEMENT, bool> walls, std::pair<int, int> cell_coords){
  22.     this->passability = passability;
  23.     this->player_presence = player_presence;
  24.     this->event = event;
  25.     this->walls = walls;
  26.     this->cell_coords = cell_coords;
  27. }
  28.  
  29. void Cell::set_pass(bool value){
  30.     this->passability = value;
  31. }
  32.  
  33. bool Cell::get_pass() const{
  34.     return this->passability;
  35. }
  36.  
  37. void Cell::set_player_presence(bool value){
  38.     this->player_presence = value;
  39. }
  40.  
  41. bool Cell::get_player_presence() const{
  42.     return this->player_presence;
  43. }
  44.  
  45. Cell::Cell(const Cell& other){
  46.     this->passability = other.passability;
  47.     this->player_presence = other.player_presence;
  48.     this->event = other.event;
  49.     this->walls = other.walls;
  50.     this->cell_coords = other.cell_coords;
  51. }
  52.  
  53. void Cell::cell_swap(Cell& other){
  54.     std::swap(player_presence, other.player_presence);
  55.     std::swap(passability, other.passability);
  56.     std::swap(event, other.event);
  57.     std::swap(walls, other.walls);
  58.     std::swap(cell_coords, other.cell_coords);
  59. }
  60.  
  61. Cell::Cell(Cell&& other){
  62.     this->cell_swap(other);
  63. }
  64.  
  65. Cell &Cell::operator=(Cell &&other){
  66.     if(this != &other)
  67.         this->cell_swap(other);
  68.     return *this;
  69. }
  70.  
  71. Cell &Cell::operator=(const Cell &other) = default;
  72.  
  73. void Cell::set_event(Event* event){
  74.     this->event = event;
  75. }
  76.  
  77. Event* Cell::get_event() const{
  78.     return this->event;
  79. }
  80.  
  81. bool Cell::get_wall(MOVEMENT wall) const{
  82.     return this->walls.at(wall);
  83. }
  84.  
  85. void Cell::set_wall(MOVEMENT wall, bool value){
  86.     this->walls[wall] = value;
  87. }
  88.  
  89. std::pair<int, int> Cell::get_cell_coords() const{
  90.     return this->cell_coords;
  91. }
  92.  
  93. void Cell::set_cell_coords(int x, int y){
  94.     this->cell_coords.first = x;
  95.     this->cell_coords.second = y;
  96. }
  97.  
  98. int Cell::check_cell(int height, int width, int x, int y){
  99.     if (x < 0 || y < 0 || x > width - 1 || y > height - 1)
  100.             return -1;
  101.         else
  102.             return x + y * width; // связь индекса клетки в одномерном массиве и ее координат на поле
  103. }
  104.  
  105. Cell* Cell::check_neighbors(int height, int width, Cell** grid){ // проверяем соседей клетки
  106.     int x = this->cell_coords.first;
  107.     int y = this->cell_coords.second;
  108.     std::vector<Cell*> neighbors;
  109.     int up = this->check_cell(height, width, x, y - 1);
  110.     int right = this->check_cell(height, width, x + 1, y);
  111.     int down = this->check_cell(height, width, x, y + 1);
  112.     int left = this->check_cell(height, width, x - 1, y);
  113.     if(up != -1){ // в каждом if происходит проверка, на то, была ли посещена соответствующая клетка сосед
  114.         if(!grid[up]->get_pass())
  115.             neighbors.push_back(grid[up]);
  116.     }
  117.     if(right != -1){
  118.         if(!grid[right]->get_pass())
  119.             neighbors.push_back(grid[right]);
  120.     }
  121.     if(down != -1){
  122.         if(!grid[down]->get_pass())
  123.             neighbors.push_back(grid[down]);
  124.     }
  125.     if(left != -1){
  126.         if(!grid[left]->get_pass())
  127.             neighbors.push_back(grid[left]);
  128.     }
  129.     if (neighbors.size() > 0){// если нашлись подходящие клетки, то случайным образом выбираем следующую клетку для посещения
  130.         std::random_device dev;
  131.         std::mt19937 rng(dev());
  132.         std::uniform_int_distribution<std::mt19937::result_type> dist(0, neighbors.size() - 1);
  133.         return neighbors[dist(rng)];
  134.     } else{ // если подходящей нет, создаем указатель на клетку сообщающую об этом
  135.         Cell cell;
  136.         Cell* c = &cell;
  137.         c->set_cell_coords(-1, -1);
  138.         return c;
  139.     }
  140. }
  141.  
  142. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement