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.

83 lines
2.6 KiB

1 year ago
  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 getColor(Color color) {
  64. switch(color) {
  65. case Color::Red: return "red";
  66. case Color::Green: return "green";
  67. case Color::Blue: return "blue";
  68. case Color::Purple: return "purple";
  69. case Color::Yellow: return "yellow";
  70. case Color::None: return "transparent";
  71. default: return "";
  72. //case Color::Grey = 'G',
  73. }
  74. }