HRusev

Splender

Jun 25th, 2023
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | Source Code | 0 0
  1. // 06. Splender.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <sstream>
  7. #include <cmath>
  8. #include <map>
  9. #include <string>
  10.  
  11.  
  12. using namespace std;
  13.  
  14.  
  15.  
  16. class Splender
  17. {
  18.     char** maps;
  19.     int rows, cols;
  20.     char direction = 'S';
  21.     size_t x, y;
  22.     bool invertedPriorities = false;
  23.     bool breakerMode = false;
  24.  
  25. public:
  26.     Splender(istream& istr);
  27.     bool move(ostream& ostr);
  28.     bool finishes(void) const
  29.     {
  30.         if (maps[x][y] == '$')
  31.             return true;
  32.         else
  33.             return false;
  34.     }
  35.     void changeDirection(void);
  36.     void teleporter(void);
  37.     int getCoordsX(void) const {return x;}
  38.     int getCoordsY(void) const { return y;}
  39.     ~Splender()
  40.     {
  41.         for (size_t i = 0; i < rows; i++)
  42.         {
  43.             delete[] maps[i];
  44.         }
  45.  
  46.         delete[] maps;
  47.     }
  48.  
  49.  
  50.  
  51. };
  52.  
  53. Splender::Splender(istream& istr)
  54. {
  55.     istr >> rows >> cols; istr.ignore();
  56.     string input;
  57.     x = -1;
  58.     y = -1;
  59.     maps = new char* [rows];
  60.     for (size_t i = 0; i < rows; i++)
  61.     {
  62.         getline(istr, input);
  63.         maps[i] = new char[cols];
  64.         for (int j = 0; j < cols; j++)
  65.         {
  66.             maps[i][j] = input.at(j);
  67.             //Find start coordinates
  68.             if (input.at(j) == '@')
  69.             {
  70.                 x = i;
  71.                 y = j;
  72.             }
  73.         }
  74.     }
  75. }
  76.  
  77. bool Splender::move(ostream& ostr)
  78. {
  79.     bool isMove = false;
  80.     switch (direction)
  81.     {
  82.     case 'S':
  83.             if ((maps[x + 1][y] != '#'
  84.                 && maps[x + 1][y] != 'X')
  85.                 || (breakerMode && maps[x + 1][y] == 'X'))
  86.             {
  87.                 ostr << "SOUTH" << endl;
  88.                 x++;
  89.                 isMove = true;
  90.             }
  91.         break;
  92.     case 'E':
  93.      
  94.             if ((maps[x][y + 1] != '#'
  95.                 && maps[x][y + 1] != 'X')
  96.                 || (breakerMode && maps[x][y + 1] == 'X'))
  97.             {
  98.                 ostr << "EAST" << endl;
  99.                 y++;
  100.                 isMove = true;
  101.             }
  102.         break;
  103.     case 'N':
  104.        
  105.             if ((maps[x - 1][y] != '#'
  106.                 && maps[x - 1][y] != 'X')
  107.                 ||(breakerMode && maps[x - 1][y] == 'X'))
  108.             {
  109.                 ostr << "NORTH" << endl;
  110.                 x--;
  111.                 isMove = true;
  112.             }
  113.         break;
  114.     default:
  115.        
  116.             if ((maps[x][y - 1] != '#'
  117.                 && maps[x][y - 1] != 'X')
  118.                 || (breakerMode && maps[x][y - 1] == 'X'))
  119.             {
  120.                 ostr << "WEST" << endl;
  121.                 y--;
  122.                 isMove = true;
  123.             }
  124.         break;
  125.     }
  126.    
  127.     if(isMove)
  128.         switch (maps[x][y])
  129.         {
  130.         case 'S':
  131.             direction = 'S';
  132.             break;
  133.         case 'E':
  134.             direction = 'E';
  135.             break;
  136.         case 'N':
  137.             direction = 'N';
  138.             break;
  139.         case 'W':
  140.             direction = 'W';
  141.             break;
  142.         case 'I':
  143.             if (invertedPriorities) invertedPriorities = false; else invertedPriorities = true;
  144.             break;
  145.         case 'B':
  146.             if (breakerMode) breakerMode = false; else breakerMode = true;
  147.                 break;
  148.         case 'X':
  149.             if (breakerMode)
  150.                 maps[x][y] = ' ';
  151.             break;
  152.         case 'T':
  153.             teleporter();
  154.             break;
  155.         default:
  156.             break;
  157.         }
  158.    
  159.     return isMove;
  160.  
  161. }
  162.  
  163. void Splender::changeDirection(void)
  164. {
  165.     if (!invertedPriorities)
  166.         if ((maps[x + 1][y] != '#'
  167.              && maps[x + 1][y] != 'X')
  168.              || (breakerMode && maps[x][y - 1] == 'X'))
  169.                 direction = 'S';
  170.                
  171.         else if ((maps[x][y + 1] != '#'
  172.                     && maps[x][y + 1] != 'X')
  173.                     || (breakerMode && maps[x][y + 1] == 'X'))
  174.                 direction = 'E';    
  175.         else if ((maps[x - 1][y] != '#'
  176.                  && maps[x - 1][y] != 'X')
  177.                  || (breakerMode && maps[x][y + 1] == 'X'))
  178.                 direction = 'N';
  179.         else
  180.                 direction = 'W';
  181.     else
  182.         if ((maps[x][y - 1] != '#'
  183.             && maps[x][y - 1] != 'X')
  184.             || (breakerMode && maps[x][y - 1] == 'X'))
  185.            
  186.             direction = 'W';
  187.            
  188.         else if ((maps[x - 1][y] != '#'
  189.                  && maps[x - 1][y] != 'X')
  190.                  || (breakerMode && maps[x - 1][y] == 'X'))
  191.             direction = 'N';
  192.                    
  193.         else if (maps[x][y + 1] != '#'
  194.                 && maps[x][y + 1] != 'X'
  195.                 || (breakerMode && maps[x][y + 1] == 'X'))
  196.             direction = 'E';
  197.              
  198.        else
  199.             direction = 'S';    
  200. }
  201.  
  202.  
  203. void Splender::teleporter(void)
  204. {
  205.     for (size_t i = 0; i < rows; i++)
  206.     {
  207.         for (size_t j = 0; j < cols; j++)
  208.         {
  209.             if (maps[i][j] == 'T'
  210.                 && i != x
  211.                 && j != y)
  212.             {
  213.                 x = i;
  214.                 y = j;
  215.                 break;
  216.             }
  217.         }
  218.     }
  219. }
  220.  
  221. int main()
  222. {
  223.     Splender spl(cin);
  224.  
  225.     while (true)
  226.     {
  227.         if (!spl.move(cout))
  228.             spl.changeDirection();
  229.         if (spl.finishes())
  230.             break;
  231.     }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment