Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 06. Splender.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include <vector>
- #include <sstream>
- #include <cmath>
- #include <map>
- #include <string>
- using namespace std;
- class Splender
- {
- char** maps;
- int rows, cols;
- char direction = 'S';
- size_t x, y;
- bool invertedPriorities = false;
- bool breakerMode = false;
- public:
- Splender(istream& istr);
- bool move(ostream& ostr);
- bool finishes(void) const
- {
- if (maps[x][y] == '$')
- return true;
- else
- return false;
- }
- void changeDirection(void);
- void teleporter(void);
- int getCoordsX(void) const {return x;}
- int getCoordsY(void) const { return y;}
- ~Splender()
- {
- for (size_t i = 0; i < rows; i++)
- {
- delete[] maps[i];
- }
- delete[] maps;
- }
- };
- Splender::Splender(istream& istr)
- {
- istr >> rows >> cols; istr.ignore();
- string input;
- x = -1;
- y = -1;
- maps = new char* [rows];
- for (size_t i = 0; i < rows; i++)
- {
- getline(istr, input);
- maps[i] = new char[cols];
- for (int j = 0; j < cols; j++)
- {
- maps[i][j] = input.at(j);
- //Find start coordinates
- if (input.at(j) == '@')
- {
- x = i;
- y = j;
- }
- }
- }
- }
- bool Splender::move(ostream& ostr)
- {
- bool isMove = false;
- switch (direction)
- {
- case 'S':
- if ((maps[x + 1][y] != '#'
- && maps[x + 1][y] != 'X')
- || (breakerMode && maps[x + 1][y] == 'X'))
- {
- ostr << "SOUTH" << endl;
- x++;
- isMove = true;
- }
- break;
- case 'E':
- if ((maps[x][y + 1] != '#'
- && maps[x][y + 1] != 'X')
- || (breakerMode && maps[x][y + 1] == 'X'))
- {
- ostr << "EAST" << endl;
- y++;
- isMove = true;
- }
- break;
- case 'N':
- if ((maps[x - 1][y] != '#'
- && maps[x - 1][y] != 'X')
- ||(breakerMode && maps[x - 1][y] == 'X'))
- {
- ostr << "NORTH" << endl;
- x--;
- isMove = true;
- }
- break;
- default:
- if ((maps[x][y - 1] != '#'
- && maps[x][y - 1] != 'X')
- || (breakerMode && maps[x][y - 1] == 'X'))
- {
- ostr << "WEST" << endl;
- y--;
- isMove = true;
- }
- break;
- }
- if(isMove)
- switch (maps[x][y])
- {
- case 'S':
- direction = 'S';
- break;
- case 'E':
- direction = 'E';
- break;
- case 'N':
- direction = 'N';
- break;
- case 'W':
- direction = 'W';
- break;
- case 'I':
- if (invertedPriorities) invertedPriorities = false; else invertedPriorities = true;
- break;
- case 'B':
- if (breakerMode) breakerMode = false; else breakerMode = true;
- break;
- case 'X':
- if (breakerMode)
- maps[x][y] = ' ';
- break;
- case 'T':
- teleporter();
- break;
- default:
- break;
- }
- return isMove;
- }
- void Splender::changeDirection(void)
- {
- if (!invertedPriorities)
- if ((maps[x + 1][y] != '#'
- && maps[x + 1][y] != 'X')
- || (breakerMode && maps[x][y - 1] == 'X'))
- direction = 'S';
- else if ((maps[x][y + 1] != '#'
- && maps[x][y + 1] != 'X')
- || (breakerMode && maps[x][y + 1] == 'X'))
- direction = 'E';
- else if ((maps[x - 1][y] != '#'
- && maps[x - 1][y] != 'X')
- || (breakerMode && maps[x][y + 1] == 'X'))
- direction = 'N';
- else
- direction = 'W';
- else
- if ((maps[x][y - 1] != '#'
- && maps[x][y - 1] != 'X')
- || (breakerMode && maps[x][y - 1] == 'X'))
- direction = 'W';
- else if ((maps[x - 1][y] != '#'
- && maps[x - 1][y] != 'X')
- || (breakerMode && maps[x - 1][y] == 'X'))
- direction = 'N';
- else if (maps[x][y + 1] != '#'
- && maps[x][y + 1] != 'X'
- || (breakerMode && maps[x][y + 1] == 'X'))
- direction = 'E';
- else
- direction = 'S';
- }
- void Splender::teleporter(void)
- {
- for (size_t i = 0; i < rows; i++)
- {
- for (size_t j = 0; j < cols; j++)
- {
- if (maps[i][j] == 'T'
- && i != x
- && j != y)
- {
- x = i;
- y = j;
- break;
- }
- }
- }
- }
- int main()
- {
- Splender spl(cin);
- while (true)
- {
- if (!spl.move(cout))
- spl.changeDirection();
- if (spl.finishes())
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment