fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. enum Direction{
  7. UP, DOWN,
  8. LEFT, RIGHT
  9. };
  10.  
  11. class Map{
  12. public:
  13. typedef char field_type;
  14. Map(size_t width, size_t height): _width(width), _height(height){
  15. field_type defaultValue = ' ';
  16. for(size_t x = 0; x < width; ++x){
  17. row_type defaultRow;
  18. for(size_t y = 0; y < height; ++y)
  19. defaultRow.push_back(defaultValue);
  20. _fields.push_back(defaultRow);
  21. }
  22. }
  23.  
  24. field_type &at(size_t x, size_t y){
  25. return _fields[x][y];
  26. }
  27.  
  28. size_t width(){
  29. return _width;
  30. }
  31.  
  32. size_t height(){
  33. return _height;
  34. }
  35. private:
  36. typedef vector<field_type> row_type;
  37. size_t _width, _height;
  38. vector<row_type> _fields;
  39. };
  40.  
  41. class Snake{
  42. friend class Drawer;
  43. public:
  44. struct BodyElement{
  45. char sign;
  46. int x, y;
  47. };
  48.  
  49. Snake(Direction dir, BodyElement el): _dir(dir){
  50. _bodyElements.push_back(el);
  51. }
  52.  
  53. Direction direction(){
  54. return _dir;
  55. }
  56.  
  57. void setDirection(Direction val){
  58. _dir = val;
  59. }
  60.  
  61. BodyElement &head(){
  62. return _bodyElements.back();
  63. }
  64.  
  65. const BodyElement &head() const{
  66. return _bodyElements.back();
  67. }
  68.  
  69. void addBodyElement(char sign){
  70. BodyElement newElement = head();
  71. newElement.sign = sign;
  72. _moveFuncs[_dir](newElement);
  73. _bodyElements.push_back(newElement);
  74. }
  75. private:
  76. Direction _dir;
  77. vector<BodyElement> _bodyElements;
  78. static map<Direction, void(*)(BodyElement &)> _moveFuncs;
  79. };
  80.  
  81. map<Direction, void(*)(Snake::BodyElement &)> Snake::_moveFuncs = {
  82. {{UP}, {[](BodyElement &be){ --be.y; }}},
  83. {{DOWN}, {[](BodyElement &be){ ++be.y; }}},
  84. {{LEFT}, {[](BodyElement &be){ --be.x; }}},
  85. {{RIGHT}, {[](BodyElement &be){ ++be.x; }}}
  86. };
  87.  
  88. class Drawer{
  89. public:
  90. Drawer(Map &map): _map(map){}
  91. void snake(const Snake &snake){
  92. for(auto &el : snake._bodyElements)
  93. _map.at(el.x, el.y) = el.sign;
  94. }
  95. void operator()(){
  96. for(size_t y = 0; y < _map.height(); ++y){
  97. for(size_t x = 0; x < _map.width(); ++x)
  98. cout << _map.at(x, y);
  99. cout << endl;
  100. }
  101. }
  102. private:
  103. Map &_map;
  104. };
  105.  
  106. int main() {
  107. Map myMap(5, 5);
  108. Snake snake(DOWN, Snake::BodyElement{'x', 0, 0});
  109. Drawer draw(myMap);
  110. snake.addBodyElement('a');
  111. snake.addBodyElement('b');
  112. snake.setDirection(RIGHT);
  113. snake.addBodyElement('c');
  114. snake.addBodyElement('d');
  115. snake.setDirection(UP);
  116. snake.addBodyElement('e');
  117. snake.addBodyElement('f');
  118. draw.snake(snake);
  119. draw();
  120. return 0;
  121. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
x f  
a e  
bcd