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.column << "," << c.row << ")";
  6. return os;
  7. }
  8. coordinates cell::getCoordinates() const {
  9. return std::make_pair(column, row);
  10. }
  11. cell cell::getNorth(const std::vector<cell> &grid) const {
  12. auto north = std::find_if(grid.begin(), grid.end(), [this](const cell &c) {
  13. return this->row - 1 == c.row && this->column == c.column;
  14. });
  15. if(north == grid.end()) {
  16. throw std::logic_error{ "Cannot get cell north of (" + std::to_string(row) + "," + std::to_string(column) + ")"};
  17. std::exit(EXIT_FAILURE);
  18. }
  19. return *north;
  20. }
  21. cell cell::getEast(const std::vector<cell> &grid) const {
  22. auto east = std::find_if(grid.begin(), grid.end(), [this](const cell &c) {
  23. return this->row == c.row && this->column + 1 == c.column;
  24. });
  25. if(east == grid.end()) {
  26. throw std::logic_error{ "Cannot get cell east of (" + std::to_string(row) + "," + std::to_string(column) + ")"};
  27. std::exit(EXIT_FAILURE);
  28. }
  29. return *east;
  30. }
  31. cell cell::getSouth(const std::vector<cell> &grid) const {
  32. auto south = std::find_if(grid.begin(), grid.end(), [this](const cell &c) {
  33. return this->row + 1 == c.row && this->column == c.column;
  34. });
  35. if(south == grid.end()) {
  36. throw std::logic_error{ "Cannot get cell south of (" + std::to_string(row) + "," + std::to_string(column) + ")"};
  37. std::exit(EXIT_FAILURE);
  38. }
  39. return *south;
  40. }
  41. cell cell::getWest(const std::vector<cell> &grid) const {
  42. auto west = std::find_if(grid.begin(), grid.end(), [this](const cell &c) {
  43. return this->row == c.row && this->column - 1 == c.column;
  44. });
  45. if(west == grid.end()) {
  46. throw std::logic_error{ "Cannot get cell west of (" + std::to_string(row) + "," + std::to_string(column) + ")"};
  47. std::exit(EXIT_FAILURE);
  48. }
  49. return *west;
  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. }