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.

104 lines
3.4 KiB

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