Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1. // THIS IS DUNGEON CRAWLER
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <chrono>
  6. #include <thread>
  7. #include <stdio.h>
  8. #include <termios.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11.  
  12. struct enemy
  13. {
  14.     int position;
  15.     enemy();
  16.     void move();
  17. };
  18.  
  19. void drawBoard(int board[64]);
  20. void movement(int board[], int &userPosition, enemy enemies[]);
  21. void hasWon(int board[], int &userPosition);
  22. void onEnemy(int board[], int &userPosition, enemy enemies[]);
  23.  
  24. int kbhit();
  25.  
  26. // global variable
  27. bool lost = false;
  28. bool won = false;
  29.  
  30. enemy::enemy()
  31. {
  32.     // 0 is player start position
  33.     position = rand() % 63 + 1;
  34. }
  35. void enemy::move()
  36. {
  37.     int r = rand() % 3;
  38.     int newPosition = position;
  39.     switch (r)
  40.     {
  41.     case 0:
  42.         newPosition -= 8;
  43.         break;
  44.     case 1:
  45.         newPosition += 8;
  46.         break;
  47.     case 2:
  48.         newPosition -= 1;
  49.         break;
  50.     case 3:
  51.         newPosition += 1;
  52.         break;
  53.     default:
  54.         return;
  55.     }
  56.     if (newPosition < 0 || newPosition > 62)
  57.         return; // 62 because 63 is the players objective
  58.  
  59.     position = newPosition;
  60.     return;
  61. }
  62.  
  63. ///////////////////////////////////
  64. //////////////////////////////////
  65. int main()
  66. {
  67.     int board[64];
  68.     int userPosition = 0;
  69. #pragma region Intializing board
  70.  
  71.     for (int i = 0; i < 64; i++)
  72.     {
  73.         board[i] = 0;
  74.     }
  75.     board[userPosition] = 5;
  76.  
  77.     // spawning enemies
  78.     enemy enemies[4];
  79.  
  80.     for (int i = 0; i < 4; i++)
  81.     {
  82.         board[enemies[i].position] = 7;
  83.     }
  84.  
  85.     board[63] = 1;
  86. #pragma endregion
  87.  
  88.     drawBoard(board);
  89.  
  90.     while (won != true && lost != true)
  91.     {
  92.         std::cout << won << std::endl;
  93.         movement(board, userPosition, enemies);
  94.         drawBoard(board);
  95.         hasWon(board, userPosition);
  96.         onEnemy(board, userPosition, enemies);
  97.         std::this_thread::sleep_for(std::chrono::milliseconds(100));
  98.         std::system("clear");
  99.     }
  100.     std::cout << "Congratulation, you've finished the game\n\n";
  101.     if (won == true)
  102.         std::cout << "You've WOOOOON !!! \n\n";
  103.     else
  104.         std::cout << "You're noob...\n\n";
  105. }
  106. ///////////////////////////////////////////////
  107. /////////////////////////////////////////////
  108.  
  109. void movement(int board[], int &userPosition, enemy enemies[])
  110. {
  111.     if (kbhit())
  112.     {
  113.         char input = getchar();
  114.         int userPositionNew = userPosition;
  115.         switch (input)
  116.         {
  117.         case 'w':
  118.             userPositionNew -= 8;
  119.             break;
  120.         case 's':
  121.             userPositionNew += 8;
  122.             break;
  123.         case 'a':
  124.             userPositionNew -= 1;
  125.             break;
  126.         case 'd':
  127.             userPositionNew += 1;
  128.             break;
  129.         default:
  130.             return;
  131.         }
  132.         if (userPositionNew < 0 || userPositionNew > 63)
  133.             return;
  134.         board[userPosition] = 0;
  135.         userPosition = userPositionNew;
  136.         board[userPosition] = 5;
  137.  
  138.         // I wasnt sure how to do this, but I wanted to make enemy move every time player moves...
  139.         for (int i = 0; i < 4; i++)
  140.         {
  141.             board[enemies[i].position] = 0;
  142.         }
  143.         for (int i = 0; i < 4; i++)
  144.         {
  145.             enemies[i].move();
  146.         }
  147.         for (int i = 0; i < 4; i++)
  148.         {
  149.             board[enemies[i].position] = 7;
  150.         }
  151.     }
  152.     return;
  153. }
  154. void onEnemy(int board[], int &userPosition, enemy enemies[])
  155. {
  156.     for (int i = 0; i < 4; i++)
  157.     {
  158.         if (userPosition == enemies[i].position)
  159.             lost = true;
  160.     }
  161.     return;
  162. }
  163. void hasWon(int board[], int &userPosition)
  164. {
  165.     if (userPosition == 63)
  166.     {
  167.         won = true;
  168.         std::cout << "You've won" << std::endl;
  169.     }
  170.     return;
  171. }
  172. void drawBoard(int board[64])
  173. {
  174.     // int i = 1 and no 0 because of the division, division of 0 by 8 is 0
  175.     // and so it writes newline that we do not want
  176.     for (int i = 1; i <= 64; i++)
  177.     {
  178.         std::cout << board[i - 1] << "  ";
  179.         if (i % 8 == 0)
  180.             std::cout << '\n';
  181.     }
  182. }
  183. int kbhit()
  184. {
  185.     struct termios oldt, newt;
  186.     int ch;
  187.     int oldf;
  188.  
  189.     tcgetattr(STDIN_FILENO, &oldt);
  190.     newt = oldt;
  191.     newt.c_lflag &= ~(ICANON | ECHO);
  192.     tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  193.     oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  194.     fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  195.  
  196.     ch = getchar();
  197.  
  198.     tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  199.     fcntl(STDIN_FILENO, F_SETFL, oldf);
  200.  
  201.     if (ch != EOF)
  202.     {
  203.         ungetc(ch, stdin);
  204.         return 1;
  205.     }
  206.  
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement