Advertisement
Trapov

Untitled

Mar 21st, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.97 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. #include <list>
  3. #include <iostream>
  4. #include <random>
  5. #include <ctime>
  6. #include <windows.h>
  7. #include <conio.h>
  8. #include <chrono>
  9. /////////////////////////////////////////////////////////////////////////////////////////
  10. const int   WIDTH       =   80;
  11. const int   HEIGHT      =   20;
  12. /////////////////////////////////////////////////////////////////////////////////////////
  13. template<typename T>
  14. using   T_list_ =   std::list<T>;
  15. /////////////////////////////////////////////////////////////////////////////////////////
  16. struct T_point
  17. {
  18.     //-----------------------------------------------------------------------------------
  19.     int  x;
  20.     int  y;
  21.     //-----------------------------------------------------------------------------------
  22.     T_point
  23.           (
  24.               int x = 0,
  25.               int y = 0
  26.           )
  27.           :
  28.           x (x),
  29.           y (y)
  30.     {}
  31.     //-----------------------------------------------------------------------------------
  32.     bool    operator<   (const T_point& point)   const
  33.     {
  34.         return  std::make_pair(x,y) < std::make_pair(point.x,point.y);
  35.     }
  36.     //-----------------------------------------------------------------------------------
  37.     T_point operator+=  (const T_point& point)
  38.     {
  39.         x   +=  point.x;
  40.         y   +=  point.y;
  41.         return *this;
  42.     }
  43.     //-----------------------------------------------------------------------------------
  44.     T_point operator+   (const T_point& point)   const
  45.     {
  46.         T_point res = *this;
  47.         return  res += point;
  48.     }
  49.     //-----------------------------------------------------------------------------------
  50.     bool    operator==  (const T_point& point)   const
  51.     {
  52.         return  x == point.x
  53.              && y == point.y;
  54.     }
  55.     //-----------------------------------------------------------------------------------
  56.     void    print_position_()                    const
  57.     {
  58.         std::cout
  59.             << x << ' '
  60.             << y << ' '
  61.             << std::endl;
  62.     }
  63.     //-----------------------------------------------------------------------------------
  64. };
  65. /////////////////////////////////////////////////////////////////////////////////////////
  66. class T_snake
  67. {
  68.     //-----------------------------------------------------------------------------------
  69.     T_list_<T_point>  snake;
  70.     //-----------------------------------------------------------------------------------
  71. public:
  72.     //-----------------------------------------------------------------------------------
  73.     T_snake
  74.         (
  75.             const T_point& start_position
  76.         )
  77.     {
  78.         snake.push_back(start_position);
  79.     }
  80.     //-----------------------------------------------------------------------------------
  81.     T_point             get_head_position_()        const
  82.     {
  83.         return  *snake.begin();
  84.     }
  85.     //-----------------------------------------------------------------------------------
  86.     T_point             get_end_tail_position_()    const
  87.     {
  88.         return *(std::prev(snake.end()));
  89.     }
  90.     //-----------------------------------------------------------------------------------
  91.     T_list_<T_point>    get_all_snake_()            const
  92.     {
  93.         return  snake;
  94.     }
  95.     //-----------------------------------------------------------------------------------
  96.     void                add_tail_snake_(const T_point& add_point)
  97.     {
  98.         snake.push_back(add_point);
  99.     }
  100.     //-----------------------------------------------------------------------------------
  101.     void                move_new_snake_(T_list_<T_point>&& new_list)
  102.     {
  103.         snake   =    std::move(new_list);
  104.     }
  105.     //-----------------------------------------------------------------------------------
  106. };
  107. /////////////////////////////////////////////////////////////////////////////////////////
  108. class T_food
  109. {
  110.     //-----------------------------------------------------------------------------------
  111.     T_point position_food;
  112.     //-----------------------------------------------------------------------------------
  113. public:
  114.     //-----------------------------------------------------------------------------------
  115.     T_food
  116.         (
  117.             const T_point& position_food = {2,2}
  118.         )
  119.         :
  120.         position_food(position_food)
  121.     {}
  122.     //-----------------------------------------------------------------------------------
  123.     void         generate_new_position_food_()
  124.     {
  125.         static  std::mt19937    gen(std::time(nullptr));
  126.         static  std::uniform_int_distribution<> distance_width(1,WIDTH-2);
  127.         static  std::uniform_int_distribution<> distance_height(1,HEIGHT-2);
  128.         position_food.x     =   distance_width(gen);
  129.         position_food.y     =   distance_height(gen);
  130.     }
  131.     //-----------------------------------------------------------------------------------
  132.     T_point      get_food_position_()            const
  133.     {
  134.         return  position_food;
  135.     }
  136.     //-----------------------------------------------------------------------------------
  137. };
  138. /////////////////////////////////////////////////////////////////////////////////////////
  139. class T_draw_base
  140. {
  141.     //-----------------------------------------------------------------------------------
  142.     COORD        windows_coord_ ;
  143.     T_point      coord_;
  144.     //-----------------------------------------------------------------------------------
  145. public:
  146.     //-----------------------------------------------------------------------------------
  147.     const static HANDLE handle_ ;
  148.     //-----------------------------------------------------------------------------------
  149.     T_draw_base
  150.              (
  151.                  const T_point& point = {1,1}
  152.              )
  153.              :
  154.              coord_(point)
  155.     {}
  156.     //-----------------------------------------------------------------------------------
  157.     virtual     ~T_draw_base()
  158.     {}
  159.     //-----------------------------------------------------------------------------------
  160.     void        clear_windows_coord_()
  161.     {
  162.         windows_coord_.X    =   0;
  163.         windows_coord_.Y    =   0;
  164.     }
  165.     //-----------------------------------------------------------------------------------
  166.     void        set_windows_coord_()
  167.     {
  168.         windows_coord_.X    =   coord_.x;
  169.         windows_coord_.Y    =   coord_.y;
  170.     }
  171.     //-----------------------------------------------------------------------------------
  172.     void        draw_char_in_coord_(const T_point& point,const char& c)
  173.     {
  174.         coord_  =   point;
  175.         set_windows_coord_();
  176.         SetConsoleCursorPosition(handle_,windows_coord_);
  177.         std::cout.put(c);
  178.         clear_windows_coord_();
  179.     }
  180.     //-----------------------------------------------------------------------------------
  181. };
  182. /////////////////////////////////////////////////////////////////////////////////////////
  183. const HANDLE T_draw_base::handle_  =  GetStdHandle( STD_OUTPUT_HANDLE );
  184. /////////////////////////////////////////////////////////////////////////////////////////
  185. class T_draw_field : public T_draw_base
  186. {
  187.     //-----------------------------------------------------------------------------------
  188. public:
  189.     //-----------------------------------------------------------------------------------
  190.     T_draw_field()
  191.     {}
  192.     //-----------------------------------------------------------------------------------
  193.     void        draw_field_()
  194.     {
  195.         for(int x = 0; x < WIDTH; ++x)
  196.         {
  197.             for(int y = 0; y < HEIGHT; ++y)
  198.             {
  199.                 if
  200.                 (
  201.                     x == 0
  202.                  || x == WIDTH - 1
  203.                  || y == 0
  204.                  || y == HEIGHT - 1
  205.                 )
  206.                 {
  207.                     draw_char_in_coord_({x,y},'#');
  208.                 } // if
  209.             } // for
  210.         } // for
  211.     }
  212.     //-----------------------------------------------------------------------------------
  213. };
  214. /////////////////////////////////////////////////////////////////////////////////////////
  215. class T_draw_snake : public T_draw_base
  216. {
  217.     //-----------------------------------------------------------------------------------
  218. public:
  219.     //-----------------------------------------------------------------------------------
  220.     T_draw_snake()
  221.     {}
  222.     //-----------------------------------------------------------------------------------
  223.     void        draw_snake_(const T_snake& snake)
  224.     {
  225.         auto snake_tail  =   snake.get_all_snake_();
  226.         for(auto i = snake_tail.cbegin(); i != snake_tail.cend(); ++i)
  227.         {
  228.             if(*i == snake.get_head_position_())
  229.             {
  230.                 draw_char_in_coord_(*i,'O');
  231.             }
  232.             else
  233.             {
  234.                 draw_char_in_coord_(*i,'o');
  235.             }
  236.         }
  237.     }
  238.     //-----------------------------------------------------------------------------------
  239. };
  240. /////////////////////////////////////////////////////////////////////////////////////////
  241. class T_snake_game
  242. {
  243.     //-----------------------------------------------------------------------------------
  244.     T_snake         snake_;
  245.     T_draw_base     drawer_base_;
  246.     T_draw_field    drawer_field_;
  247.     T_draw_snake    drawer_snake_;
  248.     T_food          food_;
  249.     enum
  250.     {
  251.         STOP,
  252.  
  253.         UP,
  254.         DOWN,
  255.         LEFT,
  256.         RIGHT
  257.     } direction;
  258.     //-----------------------------------------------------------------------------------
  259. public:
  260.     //-----------------------------------------------------------------------------------
  261.     T_snake_game
  262.                 (
  263.                 )
  264.                 :
  265.     snake_({40,10})
  266.     {}
  267.     //-----------------------------------------------------------------------------------
  268.     void        initial_varialbes_and_draw_()
  269.     {
  270.         snake_.add_tail_snake_({41,10});
  271.         snake_.add_tail_snake_({42,10});
  272.         drawer_field_.draw_field_();
  273.         food_.generate_new_position_food_();
  274.         drawer_base_.draw_char_in_coord_(food_.get_food_position_(),'@');
  275.         drawer_snake_.draw_snake_(snake_);
  276.     }
  277.     //-----------------------------------------------------------------------------------
  278.     void        calculate_direction_()
  279.     {
  280.         if(_kbhit())
  281.         {
  282.             switch(_getch())
  283.             {
  284.             case 'w':
  285.                 direction   =   UP;
  286.                 break;
  287.             case 'a':
  288.                 direction   =   LEFT;
  289.                 break;
  290.             case 's':
  291.                 direction   =   DOWN;
  292.                 break;
  293.             case 'd':
  294.                 direction   =   RIGHT;
  295.                 break;
  296.             default:
  297.                 break;
  298.             } // switch
  299.         } // if
  300.     }
  301.     //-----------------------------------------------------------------------------------
  302.     bool        check_conflict_with_wall_() const
  303.     {
  304.         return      snake_.get_head_position_().x == 0
  305.                  || snake_.get_head_position_().x == WIDTH - 1
  306.                  || snake_.get_head_position_().y == 0
  307.                  || snake_.get_head_position_().y == HEIGHT - 1;
  308.     }
  309.     //-----------------------------------------------------------------------------------
  310.     bool        check_conflict_with_food_() const
  311.     {
  312.         return      snake_.get_head_position_() == food_.get_food_position_();
  313.     }
  314.     //-----------------------------------------------------------------------------------
  315.     bool        check_conflict_with_tail_() const
  316.     {
  317.         for
  318.           (
  319.             auto i = std::next(snake_.get_all_snake_().begin());
  320.             i != snake_.get_all_snake_().end();
  321.             ++i
  322.           )
  323.           {
  324.               if(*i == snake_.get_head_position_())
  325.               {
  326.                   return true;
  327.               }
  328.           }
  329.           return false;
  330.     }
  331.     //-----------------------------------------------------------------------------------
  332.     void        draw_direction_()
  333.     {
  334.         T_point             new_head    =   *snake_.get_all_snake_().begin();
  335.         T_list_<T_point>&&  new_snake(snake_.get_all_snake_());
  336.         switch(direction)
  337.         {
  338.         case UP:
  339.             new_head.y--;
  340.             break;
  341.         case LEFT:
  342.             new_head.x--;
  343.             break;
  344.         case DOWN:
  345.             new_head.y++;
  346.             break;
  347.         case RIGHT:
  348.             new_head.x++;
  349.             break;
  350.         default:
  351.             break;
  352.         }
  353.         new_snake.push_front(new_head);
  354.         T_point             erase_elem  =   *std::prev(new_snake.end());
  355.         new_snake.erase(std::prev(new_snake.end()));
  356.         snake_.move_new_snake_(static_cast<T_list_<T_point>&&>(new_snake));
  357.         drawer_base_.draw_char_in_coord_(erase_elem,' ');
  358.         drawer_snake_.draw_snake_(snake_);
  359.     }
  360.     //-----------------------------------------------------------------------------------
  361.     void        generate_new_food()
  362.     {
  363.         food_.generate_new_position_food_();
  364.         drawer_base_.draw_char_in_coord_(food_.get_food_position_(),'@');
  365.     }
  366.     //-----------------------------------------------------------------------------------
  367.     void        add_new_pos_tail()
  368.     {
  369.         snake_.add_tail_snake_(*std::prev(snake_.get_all_snake_().end()));
  370.     }
  371.     //-----------------------------------------------------------------------------------
  372. };
  373. /////////////////////////////////////////////////////////////////////////////////////////
  374. int main()
  375. {
  376.     T_snake_game game;
  377.     game.initial_varialbes_and_draw_();
  378.     for(;;)
  379.     {
  380.         game.calculate_direction_();
  381.         if
  382.          (
  383.              game.check_conflict_with_wall_()
  384.          )
  385.         {
  386.             break;
  387.         }
  388.         if(game.check_conflict_with_food_())
  389.         {
  390.             game.generate_new_food();
  391.             game.add_new_pos_tail();
  392.         }
  393.         game.draw_direction_();
  394.         Sleep(100);
  395.     }
  396.     std::cin.get();
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement