Advertisement
Guest User

Darkroom

a guest
May 17th, 2021
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #define CLRSCR system("cls"); // windows
  5. //#define CLRSCR system("clear"); // linux
  6.  
  7. /*
  8. . . . . . . . . . .
  9. . . . . . . . . . .
  10. . .       | . . . .
  11. . . .     |____ . .
  12. . . . .       . . .
  13. . . . . .   . . . .
  14. . . . . . o . . . .
  15. . . . . . . . . . .
  16. */
  17.  
  18. std::vector<std::vector<char>> createMap(const int& mapSize, char symbol) {
  19.     std::vector<std::vector<char>> map(mapSize);
  20.     for (int x = 0; x < mapSize; ++x){
  21.         for (int y = 0; y < mapSize; ++y) {
  22.             map[x].push_back(symbol);
  23.         }
  24.     }
  25.     return map;
  26. }
  27.  
  28. void drawMap(const std::vector<std::vector<char>>& map, const int& mapSize) {
  29.     for (int x = 0; x < mapSize; ++x) {
  30.         std::cout << ' ';
  31.         for (int y = 0; y < mapSize; ++y) {
  32.             std::cout << map[x][y] << ' ';
  33.         }
  34.         std::cout << std::endl;
  35.     }
  36. }
  37.  
  38. std::vector<std::vector<int>> getEndPosOfVecsOfLigth(const int& mapSize, const int& posX, const int& posY, const int& dir, const int& power) {
  39.     std::vector<std::vector<int>> vecsLight;
  40.     int x = 0;
  41.     int y = 0;
  42.     int endPos = power - 1;
  43.     std::vector<int> vecX;
  44.     std::vector<int> vecY;
  45.     for (int i = 0 - power + 2; i < endPos; ++i) {
  46.         if (dir % 2 != 0) {
  47.             x = posX + endPos * dir;
  48.             y = posY + i;
  49.         } else {
  50.             x = posY + i;
  51.             y = posX + endPos * (dir / 2);
  52.         }
  53.         vecX.push_back(x);
  54.         vecY.push_back(y);
  55.     }
  56.     vecsLight.push_back(vecX);
  57.     vecsLight.push_back(vecY);
  58.     return vecsLight;
  59. }
  60.  
  61.  
  62. int moveOverVec(const int& endPos, int& currPos) {
  63.     if (currPos < endPos) {
  64.         currPos++;
  65.     }
  66.     else if (currPos > endPos) {
  67.         currPos--;
  68.     }
  69.     return currPos;
  70. }
  71.  
  72. void revealMap(const int posX, const int posY, const int dir,
  73.                const std::vector<std::vector<char>>& map,
  74.                const std::vector<std::vector<int>>& vecsLight,
  75.                std::vector<std::vector<char>>& darknessMask) {
  76.     size_t numOfLigthsVecs = vecsLight[0].size();
  77.     int beginX = 0;
  78.     int beginY = 0;
  79.     if (dir % 2 != 0) {
  80.         beginX = posX + dir;
  81.         beginY = posY;
  82.     } else {
  83.         beginX = posX;
  84.         beginY = posY + (dir / 2);
  85.     }
  86.     darknessMask[beginX][beginY] = map[beginX][beginY];
  87.     if (map[beginX][beginY] != ' ') {
  88.         return;
  89.     }
  90.     int endX = 0;
  91.     int endY = 0;
  92.     int x = 0;
  93.     int y = 0;
  94.     for (int i = 0; i < numOfLigthsVecs; ++i) {
  95.         x = beginX;
  96.         y = beginY;
  97.         endX = vecsLight[0][i];
  98.         endY = vecsLight[1][i];
  99.         while (x != endX || y != endY) {
  100.             x = moveOverVec(endX, x);
  101.             y = moveOverVec(endY, y);
  102.             // trim light on the edge of the map
  103.             if (x > 15 || x < 0) { // to change 15 with mapSize -1
  104.                 x = endX - 1;
  105.                 break;
  106.             }
  107.             if (y > 15 || y < 0) { // to change 15 with mapSize -1
  108.                 y = endY - 1;
  109.                 break;
  110.             }
  111.             //
  112.             if (map[x][y] != ' ') {
  113.                 darknessMask[x][y] = map[x][y];
  114.                 x = endX;
  115.                 y = endY;
  116.                 continue;
  117.             }
  118.             darknessMask[x][y] = map[x][y];
  119.         }
  120.     }
  121. }
  122.  
  123. int getDirection(int direction) {
  124.     int dir = 0;
  125.     // 1 - north, 2 - east, 3 - south, 4 - west
  126.     switch (direction) { // +- shows the direction, 1 - vertical, 2 - horizontal
  127.         case (1): dir = -1; break;
  128.         case (2): dir = +2; break;
  129.         case (3): dir = +1; break;
  130.         case (4): dir = -2; break;
  131.     }
  132.     return dir;
  133. }
  134.  
  135. int controlPlayer(int& playerPosX, int& playerPosY) {
  136.     std::cout << " Make a move (w/d/s/a): ";
  137.     char pressedKey;
  138.     int direction = 0;
  139.     std::cin >> pressedKey;
  140.     switch (pressedKey) { // 1 - north, 2 - east, 3 - south, 4 - west
  141.         case ('w'): direction = 1; playerPosX--; break;
  142.         case ('d'): direction = 2; playerPosY++; break;
  143.         case ('s'): direction = 3; playerPosX++; break;
  144.         case ('a'): direction = 4; playerPosY--; break;
  145.         default:
  146.             std::cout << "Error key!";
  147.             direction = controlPlayer(playerPosX, playerPosY);
  148.             break;
  149.     }
  150.     return direction;
  151. }
  152.  
  153. int main()
  154. {
  155.     const int mapSize = 16;
  156.     int playerPosX = mapSize / 2;
  157.     int playerPosY = mapSize / 2;
  158.     int playerOrientation = 1;
  159.     const int flashLightPower = 6;
  160.     std::pair<std::vector<int>, std::vector<int>> visibleObjects;
  161.     std::vector<std::vector<char>> map = createMap(mapSize, ' ');
  162.     std::vector<std::vector<char>> darknessMask = createMap(mapSize,'.');
  163.  
  164.     //objects
  165.     map[6][8] = 'L';
  166.     map[5][6] = 'L';
  167.     map[8][5] = 'L';
  168.     map[9][5] = 'L';
  169.     map[10][5] = 'L';
  170.  
  171.     while (1) {
  172.         int dirOfLight = getDirection(playerOrientation);
  173.         darknessMask = createMap(mapSize, '.');
  174.         darknessMask[playerPosX][playerPosY] = 'o';
  175.         // light work
  176.         std::vector<std::vector<int>> vecsOfLight = getEndPosOfVecsOfLigth(mapSize, playerPosX, playerPosY, dirOfLight, flashLightPower);
  177.         revealMap(playerPosX, playerPosY, dirOfLight, map, vecsOfLight, darknessMask);
  178.         //
  179.         drawMap(darknessMask, mapSize);
  180.         playerOrientation = controlPlayer(playerPosX, playerPosY);
  181.         CLRSCR
  182.     }
  183.  
  184.     std::cout << std::endl;
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement