Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. #include <windows.h>
  5. #include <deque>
  6. #include <ctime>
  7.  
  8. #define y first
  9. #define x second
  10.  
  11. #define For(i,z) for(int32_t i=0;i<(z);i++)
  12.  
  13. using namespace std;
  14.  
  15. const int N = 20;
  16.  
  17. /*
  18.     0 - empty
  19.     1 - snake
  20.     2 - fruit
  21. */
  22. int state[N][N];
  23.  
  24. int delay;
  25. string conv = "DASW";
  26. vector <pair<int, int> > addpos = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  27. deque<pair<int, int> > snake;
  28.  
  29. inline void waitForKey() {
  30.     fflush(stdin);
  31.     while (fgetc(stdin) == 0x0A);
  32.     return;
  33. }
  34.  
  35. inline bool correct(int y, int x) {
  36.     if (y < 0 || y >= N) return false;
  37.     if (x < 0 || x >= N) return false;
  38.     return (state[y][x] != 1);
  39. }
  40.  
  41. inline void clearall() {
  42.     delay = 500;
  43.     snake.clear();
  44.     For (i, N)
  45.         For (j, N)
  46.             state[i][j] = 0;
  47. }
  48.  
  49.  
  50. string fieldchars = ".XO";
  51. void printfield() {
  52.     system("cls");
  53.     For (i, N) {
  54.         For (j, N)
  55.             cout << fieldchars[state[i][j]];
  56.         cout << '\n';
  57.     }
  58.     cout << endl;
  59. }
  60.  
  61. int genapple() {
  62.     int nxt;
  63.     while (true) {
  64.         nxt = rand() % (N * N);
  65.         if (state[nxt/N][nxt%N] == 0)
  66.             break;
  67.     }
  68.     state[nxt/N][nxt%N] = 2;
  69. }
  70.  
  71. void play() {
  72.     clearall();
  73.  
  74.     snake.emplace_front(0, 0);
  75.     state[0][0] = 1;
  76.  
  77.     genapple();
  78.     printfield();
  79.  
  80.     pair<int, int> curAdd = {0, 1};
  81.     while (true) {
  82.         int tt = GetTickCount();
  83.         //int adidx = -1;
  84.         auto wasAdd = curAdd;
  85.         bool space = GetKeyState(VK_SPACE) & 0x8000;
  86.  
  87.         while (GetTickCount() - tt < delay / (space ? 3 : 1)) {
  88.             For (i, 4)
  89.                 if (GetKeyState(conv[i]) & 0x8000)
  90.                     if (wasAdd != make_pair(-addpos[i].y, -addpos[i].x))
  91.                         curAdd = addpos[i];
  92.         }
  93.  
  94.  
  95.         int ny = snake.front().y + curAdd.y,
  96.             nx = snake.front().x + curAdd.x;
  97.  
  98.         if (correct(ny, nx) == false) {
  99.             cout << "DEAD!!! Press any key + Enter to continue" << endl;
  100.             waitForKey();
  101.             return;
  102.         }
  103.         if (state[ny][nx] == 2) {
  104.             state[ny][nx] = 1;
  105.             genapple();
  106.             delay = max(100, delay * 96 / 100);
  107.         } else {
  108.             state[ny][nx] = 1;
  109.             state[snake.back().y][snake.back().x] = 0;
  110.             snake.pop_back();
  111.         }
  112.         snake.emplace_front(ny, nx);
  113.         printfield();
  114.         //Sleep();
  115.     }
  116. }
  117.  
  118. int32_t main()
  119. {
  120.     ios_base::sync_with_stdio(false);
  121.     cin.tie(0); cout.tie(0);
  122.     srand(time(NULL));
  123.  
  124.     cout << "Input on WASD" << endl;
  125.     cout << "Space for triple speed" << endl;
  126.  
  127.     cout << "Press any button + Enter to continue" << endl;
  128.     waitForKey();
  129.     while (true) {
  130.         play();
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement