Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4.  
  5. #include "../Graph_lib/Simple_window.h"
  6. #include "../Graph_lib/Graph.h"
  7.  
  8. using namespace Graph_lib;
  9. using namespace std;
  10. //window2048 win{ Point{ 100, 100 }, 700, 600, "2048" };
  11. struct window2048 : Simple_window
  12. {
  13.  //window2048 (Point xy, int wm, int h, const string& name);
  14.   using Simple_window::Simple_window;
  15. private:
  16.   Button top;
  17.   Button down;
  18.   Button right;
  19.   Button left;
  20.   bool butt_pushed;
  21.   static void cb_top(Address, Address);
  22.   static void cb_down(Address, Address);
  23.   static void cb_left(Address, Address);
  24.   static void cb_right(Address, Address);
  25. };
  26.  
  27. class Cell
  28. {
  29. public:
  30.   Cell(int bin);
  31.   void plus_pow()
  32.   {
  33.     b_pow++;
  34.   }
  35.  
  36.   int get_bin(){return b_pow;}
  37.   int get_color(){return Set_color();}
  38.  
  39. private:
  40.   int Set_color();
  41.   int b_pow;
  42. };
  43.  
  44.  
  45. int Cell::Set_color()
  46. {
  47.   switch(b_pow)
  48.   {
  49.   case -1:
  50.     return 0; //error check
  51.     break;
  52.   case 0:
  53.     return 17;
  54.     break;
  55.   case 1:
  56.     return 214;
  57.     break;
  58.   case 2:
  59.     return 211;
  60.     break;
  61.   case 3:
  62.     return 132;
  63.     break;
  64.   case 4:
  65.     return 129;
  66.     break;
  67.   default:
  68.     return 2;
  69.   }
  70. }
  71.  
  72.  
  73. Cell::Cell(int bin)
  74. {
  75.   b_pow=bin;
  76. }
  77.  
  78.  
  79. class Board : public Shape
  80. {
  81. public:
  82.   Board();
  83.   void set_table(char move);
  84. protected:
  85.   void draw_lines() const;
  86. private:
  87.   void right_set(int i);
  88.   void left_set(int i); //с этого момента не написанно
  89.   void up_set(int i);
  90.   void down_set(int i);
  91.  
  92.   void start_game();
  93.   void random_cell();
  94.  
  95.   vector<Vector_ref<Cell>> table;
  96. };
  97.  
  98.  
  99. Board::Board()
  100.   :Shape ()
  101. {
  102.   start_game();
  103. }
  104.  
  105. int end_of_game()
  106. {
  107.   Text end_of_game (Point (200, 200), "End of game");
  108.   end_of_game.set_color(Color::blue);
  109.   win.attach(end_of_game);
  110.   return 0;
  111. }
  112.  
  113. void Board::random_cell()
  114. {
  115.   vector<vector<int>> free_cell;
  116.  
  117.   for(int i {0} ; i<4 ; ++i)
  118.   {
  119.     for(int j {0} ; j<4 ; ++j)
  120.     {
  121.       if (table[i][j].get_bin()==0)
  122.         free_cell.push_back(vector<int>{i,j});
  123.     }
  124.   }
  125.  
  126.   if (free_cell.size()==0)
  127.   {
  128.     throw ("Game Over"); //if no moves on next turn
  129.   }
  130.  
  131.   int a = free_cell.size();
  132.  
  133.   if (a == 1)
  134.   {
  135.     table[free_cell[0][0]][free_cell[0][1]]=Cell{1};
  136.     return;
  137.   }
  138.  
  139.   srand((unsigned int)time(NULL));
  140.   a = abs(rand() % a);
  141.   table[free_cell[a][0]][free_cell[a][1]]=Cell{1};
  142. }
  143.  
  144.  
  145. void Board::start_game()
  146. {
  147.   for (int i{0} ; i<4 ; ++i)
  148.   {
  149.     Vector_ref<Cell> set;
  150.     table.push_back(set);
  151.     for (int j{0} ; j<4 ; ++j)
  152.     {
  153.       table[i].push_back(new Cell(0));
  154.     }
  155.   }
  156.   random_cell();
  157.   random_cell();
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164. void Board::right_set(int i)
  165. {
  166.  
  167.   while (true)
  168.   {
  169.   for(int l{3}; l >= 2; --l) //хз l>=2 или l>=1
  170.   {
  171.     for (int j{l} ; j >= 1 ; --j)//сдвиг не 0 вправо до конца
  172.     {
  173.       if(table[i][j].get_bin()==0)
  174.         for (int h{0} ; h < 4 ; ++h)
  175.         {
  176.           for(int J{j} ; J>=1 ; --J)
  177.             if(table[i][J].get_bin()==0 && J>=1)
  178.               swap(table[i][J-1],table[i][J]);
  179.         }
  180.     }
  181.  
  182.     for (int j{l} ; j>=1 ; --j)//слияние вправо
  183.     {
  184.       if (table[i][j].get_bin()==table[i][j-1].get_bin() && table[i][j].get_bin()!=0)
  185.       {
  186.         table[i][j].plus_pow();
  187.         table[i][j-1]=Cell(0);
  188.       }
  189.     }
  190.    }
  191.   }
  192.   end_of_game();
  193. }
  194.  
  195. //void Board::up_set(int i)
  196. //{
  197. //  for (int l{3})
  198. //}
  199.  
  200.  
  201. void Board::set_table(char move)
  202. {
  203.   switch (move)
  204.   {
  205.   case 'r':
  206.   {
  207.     for (int i{0} ; i<4 ; i++)
  208.       right_set(i);
  209.     random_cell();
  210.     break;
  211.   }
  212.  
  213. //  case 'l':
  214. //    for (int i{0} ; i<4 ; i++)
  215. //    left_set(i);
  216. //    random_cell();
  217. //    break;
  218.  
  219.   }
  220. }
  221.  
  222.  
  223. void Board::draw_lines() const
  224. {
  225.   Shape::draw_lines();
  226.  
  227.   Vector_ref<Graph_lib::Rectangle> rect;
  228.   Vector_ref<Text> box;
  229.   vector<Vector_ref<Cell>> prin = table;
  230.  
  231.   for (int i{0} ; i<4 ; i++)
  232.   {
  233.     for (int j{0} ; j<4 ; j++)
  234.     {
  235.       rect.push_back(new Graph_lib::Rectangle{Point{j*150,i*150},150,150});
  236.       rect[rect.size()-1].set_fill_color(prin[i][j].get_color());
  237.       rect[rect.size()-1].draw();
  238.  
  239.       if (prin[i][j].get_bin()>0)
  240.       {
  241.         string s = to_string((int)pow(2,prin[i][j].get_bin()));
  242.         box.push_back(new Graph_lib::Text{Point{j*150+60, i*150+90} , s });
  243.         box[box.size()-1].set_font_size(60);
  244.         box[box.size()-1].draw();
  245.       }
  246.     }
  247.   }
  248. }
  249.  
  250.  
  251.  
  252.  
  253. int main()
  254. {
  255.  
  256.  
  257.   Board game;
  258.  
  259.   win.attach(game);
  260.   win.wait_for_button();
  261.  
  262.   while(true)
  263.   {
  264.     win.wait_for_button();
  265.  
  266.     game.set_table('r');
  267.   }
  268.  
  269.  
  270.  
  271.   return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement