Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ^
- |
- <- x ->
- |
- */
- // R, L, T, B, P
- // pair<int, int>
- // vector<X>
- #include <cstdlib>
- #include <iostream>
- #include <utility>
- #include <vector>
- const int MAP_DIMENSION = 22;
- bool hits_wall(std::vector<std::vector<bool>> map_walls,
- std::pair<int, int> position, std::pair<int, int> exit) {
- int x = position.first;
- int y = position.second;
- // Puoi raggiungere l'uscita
- if (x == exit.first && y == exit.second) return false;
- return map_walls.at(x).at(y) || (x == 0 || x == map_walls.size() - 1) ||
- (y == map_walls.size() - 1 || y == 0);
- // if(map_walls[x][y] == true) return true; else return false;
- }
- void print_map(std::vector<std::vector<bool>> map_walls,
- std::pair<int, int> position, std::pair<int, int> exit) {
- using namespace std;
- for (int i = 0; i < map_walls.size(); i++) {
- for (int j = 0; j < map_walls.size(); j++) {
- // Bordo in alto o a sinistra
- if (position.first == i && position.second == j) {
- cout << "@";
- continue;
- }
- // Uscita
- if (i == exit.first && j == exit.second) {
- cout << " ";
- continue;
- }
- if (i == 0 || i == map_walls.size() - 1) {
- cout << "═";
- continue;
- }
- // Bordo in basso o a destra
- if (j == map_walls.size() - 1 || j == 0) {
- cout << "║";
- continue;
- }
- if (map_walls.at(i).at(j)) {
- cout << "▓";
- continue;
- }
- cout << " ";
- }
- cout << endl;
- }
- cout << endl << endl << endl << endl;
- }
- int main() {
- using namespace std;
- pair<int, int> current_position(5, 6); // { first: 0, second: 0 }
- vector<pair<int, int>> history; // { }
- vector<vector<bool>> map_walls(MAP_DIMENSION, vector<bool>(MAP_DIMENSION));
- // Positioning a wall at 3, (7, 8, 9)
- map_walls.at(3).at(7) = true;
- map_walls.at(3).at(8) = true;
- map_walls.at(3).at(9) = true;
- map_walls.at(1).at(4) = true;
- map_walls.at(1).at(3) = true;
- map_walls.at(16).at(14) = true;
- map_walls.at(15).at(14) = true;
- map_walls.at(13).at(14) = true;
- while (true) {
- pair<int, int> exit(14, 21);
- print_map(map_walls, current_position, exit);
- cout << endl << "Command(L, R, D, U): ";
- char command;
- cin >> command;
- switch (command) {
- case 'D':
- if (!(hits_wall(map_walls,
- pair<int, int>(current_position.first + 1,
- current_position.second),
- exit))) {
- current_position.first++;
- }
- // y - 1
- break;
- case 'U':
- if (!(hits_wall(map_walls,
- pair<int, int>(current_position.first - 1,
- current_position.second),
- exit))) {
- current_position.first--;
- }
- // x + 1
- break;
- case 'R':
- if (!(hits_wall(map_walls,
- pair<int, int>(current_position.first,
- current_position.second + 1),
- exit))) {
- current_position.second++;
- }
- // y + 1
- break;
- case 'L':
- if (!(hits_wall(map_walls,
- pair<int, int>(current_position.first,
- current_position.second - 1),
- exit))) {
- current_position.second--;
- }
- // y - 1
- break;
- }
- if (current_position.first == exit.first &&
- current_position.second == exit.second) {
- cout << endl << "YOU WIN!" << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement