You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.4 KiB

2 months ago
  1. #include "cell.h"
  2. #include <stdexcept>
  3. #include <algorithm>
  4. std::ostream &operator<<(std::ostream &os, const cell &c) {
  5. os << static_cast<char>(c.type) << static_cast<char>(c.color);
  6. os << " at (" << c.column << "," << c.row << ")";
  7. return os;
  8. }
  9. coordinates cell::getCoordinates() const {
  10. return std::make_pair(column, row);
  11. }
  12. cell cell::getNorth(const std::vector<cell> &grid) const {
  13. auto north = std::find_if(grid.begin(), grid.end(), [this](const cell &c) {
  14. return this->row - 1 == c.row && this->column == c.column;
  15. });
  16. if(north == grid.end()) {
  17. throw std::logic_error{ "Cannot get cell north of (" + std::to_string(row) + "," + std::to_string(column) + ")"};
  18. std::exit(EXIT_FAILURE);
  19. }
  20. return *north;
  21. }
  22. cell cell::getEast(const std::vector<cell> &grid) const {
  23. auto east = std::find_if(grid.begin(), grid.end(), [this](const cell &c) {
  24. return this->row == c.row && this->column + 1 == c.column;
  25. });
  26. if(east == grid.end()) {
  27. throw std::logic_error{ "Cannot get cell east of (" + std::to_string(row) + "," + std::to_string(column) + ")"};
  28. std::exit(EXIT_FAILURE);
  29. }
  30. return *east;
  31. }
  32. cell cell::getSouth(const std::vector<cell> &grid) const {
  33. auto south = std::find_if(grid.begin(), grid.end(), [this](const cell &c) {
  34. return this->row + 1 == c.row && this->column == c.column;
  35. });
  36. if(south == grid.end()) {
  37. throw std::logic_error{ "Cannot get cell south of (" + std::to_string(row) + "," + std::to_string(column) + ")"};
  38. std::exit(EXIT_FAILURE);
  39. }
  40. return *south;
  41. }
  42. cell cell::getWest(const std::vector<cell> &grid) const {
  43. auto west = std::find_if(grid.begin(), grid.end(), [this](const cell &c) {
  44. return this->row == c.row && this->column - 1 == c.column;
  45. });
  46. if(west == grid.end()) {
  47. throw std::logic_error{ "Cannot get cell west of (" + std::to_string(row) + "," + std::to_string(column) + ")"};
  48. std::exit(EXIT_FAILURE);
  49. }
  50. return *west;
  51. }
  52. std::string cell::getColor() const {
  53. switch(color) {
  54. case Color::Red: return "red";
  55. case Color::Green: return "green";
  56. case Color::Blue: return "blue";
  57. case Color::Purple: return "purple";
  58. case Color::Yellow: return "yellow";
  59. case Color::None: return "transparent";
  60. default: return "";
  61. //case Color::Grey = 'G',
  62. }
  63. }
  64. std::string cell::getType() const {
  65. switch(type) {
  66. case Type::Wall: return "Wall";
  67. case Type::Floor: return "Floor";
  68. case Type::Door: return "Door";
  69. case Type::LockedDoor: return "LockedDoor";
  70. case Type::Key: return "Key";
  71. case Type::Ball: return "Ball";
  72. case Type::Box: return "Box";
  73. case Type::Goal: return "Goal";
  74. case Type::Lava: return "Lava";
  75. case Type::Agent: return "Agent";
  76. case Type::Adversary: return "Adversary";
  77. case Type::SlipperyNorth: return "SlipperyNorth";
  78. case Type::SlipperySouth: return "SlipperySouth";
  79. case Type::SlipperyEast: return "SlipperyEast";
  80. case Type::SlipperyWest: return "SlipperyWest";
  81. default: return "";
  82. }
  83. }
  84. std::string getColor(Color color) {
  85. switch(color) {
  86. case Color::Red: return "red";
  87. case Color::Green: return "green";
  88. case Color::Blue: return "blue";
  89. case Color::Purple: return "purple";
  90. case Color::Yellow: return "yellow";
  91. case Color::None: return "transparent";
  92. default: return "";
  93. //case Color::Grey = 'G',
  94. }
  95. }