Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <cstdio>
  2. #include <thread>
  3. #include <cstdlib>
  4. #include <chrono>
  5. #include <ncurses.h>
  6.  
  7. using namespace std::chrono_literals;
  8.  
  9. const int fieldW = 8;
  10. const int fieldH = 12;
  11. const int pieceSize = 4;
  12. const int pieceCnt = 7;
  13. const char empty = ' ';
  14.  
  15. const char pieces[pieceCnt][pieceSize * pieceSize + 1] = {
  16.     "    "
  17.     "AAAA"
  18.     "    "
  19.     "    ",
  20.  
  21.     "    "
  22.     "B   "
  23.     "BBB "
  24.     "    ",
  25.  
  26.     "    "
  27.     "  C "
  28.     "CCC "
  29.     "    ",
  30.  
  31.     "    "
  32.     " DD "
  33.     " DD "
  34.     "    ",
  35.  
  36.     "    "
  37.     " EE "
  38.     "EE  "
  39.     "    ",
  40.  
  41.     "    "
  42.     " F  "
  43.     "FFF "
  44.     "    ",
  45.  
  46.     "    "
  47.     "GG  "
  48.     " GG "
  49.     "    "
  50. };
  51.  
  52. char field[fieldH + 3][fieldW + 4] = {};
  53.  
  54. void initializeField()
  55. {
  56.     for (int i = 0; i < fieldH + 2; ++i)
  57.         for (int j = 0; j < fieldW + 3; ++j)
  58.             field[i][j] = empty;
  59. }
  60.  
  61. inline char &fieldReal(int y, int x)
  62. {
  63.     return field[y + 3][x + 2];
  64. }
  65.  
  66. void printField(WINDOW *w)
  67. {
  68.     char t;
  69.     for (int i = 0; i < fieldH; ++i) {
  70.         wmove(w, 2 * i + 1, 1);
  71.         for (int j = 0; j < fieldW; ++j) {
  72.             t = fieldReal(i, j);
  73.             wprintw(w, "%c%c", t, t);
  74.         }
  75.     }
  76.     for (int i = 0; i < fieldH; ++i) {
  77.         wmove(w, 2 * i + 2, 1);
  78.         for (int j = 0; j < fieldW; ++j) {
  79.             t = fieldReal(i, j);
  80.             wprintw(w, "%c%c", t, t);
  81.         }
  82.     }
  83. }
  84.  
  85. void printFieldLn(WINDOW *w, int y)
  86. {
  87.     wmove(w, 2 * y + 1, 1);
  88.     char t;
  89.     for (int i = 0; i < fieldW; ++i) {
  90.         t = fieldReal(y, i);
  91.         wprintw(w, "%c%c", t, t);
  92.     }
  93.     wmove(w, 2 * y + 2, 1);
  94.     for (int i = 0; i < fieldW; ++i) {
  95.         t = fieldReal(y, i);
  96.         wprintw(w, "%c%c", t, t);
  97.     }
  98. }
  99.  
  100. void fieldMv(int y1, int x1, int y2, int x2)
  101. {
  102.     if (x1 == x2 && y1 == y2)
  103.         return;
  104.     fieldReal(y2, x2) = fieldReal(y1, x1);
  105.     fieldReal(y1, x1) = empty;
  106. }
  107.  
  108.  
  109. int main()
  110. {
  111.     initscr();
  112.     cbreak();
  113.     keypad(stdscr, true);
  114.     noecho();
  115.     refresh();
  116.     timeout(0);
  117.     curs_set(0);
  118.  
  119.     WINDOW *fieldwin = newwin(2 * fieldH + 2, 2 * fieldW + 2, 1, 1);
  120.     box(fieldwin, 0, 0);
  121.     wrefresh(fieldwin);
  122.  
  123.     initializeField();
  124.     char c;
  125.     int y = 3, x = 5, ly = y, lx = x;
  126.  
  127.     fieldReal(y, x) = 'X';
  128.     while (1) {
  129.         c = getch();
  130.         if (c == 'q')
  131.             break;
  132.         switch (c) {
  133.         case 'a':
  134.             if (x > 0)
  135.                 --x;
  136.             break;
  137.         case 'd':
  138.             if (x < fieldW - 1)
  139.                 ++x;
  140.             break;
  141.         case 'w':
  142.             if (y > 0)
  143.                 --y;
  144.             break;
  145.         case 's':
  146.             if (y < fieldH - 1)
  147.                 ++y;
  148.         }
  149.  
  150.         fieldMv(ly, lx, y, x);
  151.         printField(fieldwin);
  152.         //printFieldLn(fieldwin, y);
  153.         //if (y != ly)
  154.         //    printFieldLn(fieldwin, ly);
  155.         wrefresh(fieldwin);
  156.         lx = x, ly = y;
  157.  
  158.         std::this_thread::sleep_for(50ms);
  159.     }
  160.  
  161.     endwin();
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement