Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #define CLRSCR system("cls"); // windows
- //#define CLRSCR system("clear"); // linux
- /*
- . . . . . . . . . .
- . . . . . . . . . .
- . . | . . . .
- . . . |____ . .
- . . . . . . .
- . . . . . . . . .
- . . . . . o . . . .
- . . . . . . . . . .
- */
- std::vector<std::vector<char>> createMap(const int& mapSize, char symbol) {
- std::vector<std::vector<char>> map(mapSize);
- for (int x = 0; x < mapSize; ++x){
- for (int y = 0; y < mapSize; ++y) {
- map[x].push_back(symbol);
- }
- }
- return map;
- }
- void drawMap(const std::vector<std::vector<char>>& map, const int& mapSize) {
- for (int x = 0; x < mapSize; ++x) {
- std::cout << ' ';
- for (int y = 0; y < mapSize; ++y) {
- std::cout << map[x][y] << ' ';
- }
- std::cout << std::endl;
- }
- }
- std::vector<std::vector<int>> getEndPosOfVecsOfLigth(const int& mapSize, const int& posX, const int& posY, const int& dir, const int& power) {
- std::vector<std::vector<int>> vecsLight;
- int x = 0;
- int y = 0;
- int endPos = power - 1;
- std::vector<int> vecX;
- std::vector<int> vecY;
- for (int i = 0 - power + 2; i < endPos; ++i) {
- if (dir % 2 != 0) {
- x = posX + endPos * dir;
- y = posY + i;
- } else {
- x = posY + i;
- y = posX + endPos * (dir / 2);
- }
- vecX.push_back(x);
- vecY.push_back(y);
- }
- vecsLight.push_back(vecX);
- vecsLight.push_back(vecY);
- return vecsLight;
- }
- int moveOverVec(const int& endPos, int& currPos) {
- if (currPos < endPos) {
- currPos++;
- }
- else if (currPos > endPos) {
- currPos--;
- }
- return currPos;
- }
- void revealMap(const int posX, const int posY, const int dir,
- const std::vector<std::vector<char>>& map,
- const std::vector<std::vector<int>>& vecsLight,
- std::vector<std::vector<char>>& darknessMask) {
- size_t numOfLigthsVecs = vecsLight[0].size();
- int beginX = 0;
- int beginY = 0;
- if (dir % 2 != 0) {
- beginX = posX + dir;
- beginY = posY;
- } else {
- beginX = posX;
- beginY = posY + (dir / 2);
- }
- darknessMask[beginX][beginY] = map[beginX][beginY];
- if (map[beginX][beginY] != ' ') {
- return;
- }
- int endX = 0;
- int endY = 0;
- int x = 0;
- int y = 0;
- for (int i = 0; i < numOfLigthsVecs; ++i) {
- x = beginX;
- y = beginY;
- endX = vecsLight[0][i];
- endY = vecsLight[1][i];
- while (x != endX || y != endY) {
- x = moveOverVec(endX, x);
- y = moveOverVec(endY, y);
- // trim light on the edge of the map
- if (x > 15 || x < 0) { // to change 15 with mapSize -1
- x = endX - 1;
- break;
- }
- if (y > 15 || y < 0) { // to change 15 with mapSize -1
- y = endY - 1;
- break;
- }
- //
- if (map[x][y] != ' ') {
- darknessMask[x][y] = map[x][y];
- x = endX;
- y = endY;
- continue;
- }
- darknessMask[x][y] = map[x][y];
- }
- }
- }
- int getDirection(int direction) {
- int dir = 0;
- // 1 - north, 2 - east, 3 - south, 4 - west
- switch (direction) { // +- shows the direction, 1 - vertical, 2 - horizontal
- case (1): dir = -1; break;
- case (2): dir = +2; break;
- case (3): dir = +1; break;
- case (4): dir = -2; break;
- }
- return dir;
- }
- int controlPlayer(int& playerPosX, int& playerPosY) {
- std::cout << " Make a move (w/d/s/a): ";
- char pressedKey;
- int direction = 0;
- std::cin >> pressedKey;
- switch (pressedKey) { // 1 - north, 2 - east, 3 - south, 4 - west
- case ('w'): direction = 1; playerPosX--; break;
- case ('d'): direction = 2; playerPosY++; break;
- case ('s'): direction = 3; playerPosX++; break;
- case ('a'): direction = 4; playerPosY--; break;
- default:
- std::cout << "Error key!";
- direction = controlPlayer(playerPosX, playerPosY);
- break;
- }
- return direction;
- }
- int main()
- {
- const int mapSize = 16;
- int playerPosX = mapSize / 2;
- int playerPosY = mapSize / 2;
- int playerOrientation = 1;
- const int flashLightPower = 6;
- std::pair<std::vector<int>, std::vector<int>> visibleObjects;
- std::vector<std::vector<char>> map = createMap(mapSize, ' ');
- std::vector<std::vector<char>> darknessMask = createMap(mapSize,'.');
- //objects
- map[6][8] = 'L';
- map[5][6] = 'L';
- map[8][5] = 'L';
- map[9][5] = 'L';
- map[10][5] = 'L';
- while (1) {
- int dirOfLight = getDirection(playerOrientation);
- darknessMask = createMap(mapSize, '.');
- darknessMask[playerPosX][playerPosY] = 'o';
- // light work
- std::vector<std::vector<int>> vecsOfLight = getEndPosOfVecsOfLigth(mapSize, playerPosX, playerPosY, dirOfLight, flashLightPower);
- revealMap(playerPosX, playerPosY, dirOfLight, map, vecsOfLight, darknessMask);
- //
- drawMap(darknessMask, mapSize);
- playerOrientation = controlPlayer(playerPosX, playerPosY);
- CLRSCR
- }
- std::cout << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement