Advertisement
informaticage

Bri bro robottino obesizzante

Nov 12th, 2021
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. /*
  2.     ^
  3.     |
  4.  <- x ->
  5.     |
  6.  
  7. */
  8.  
  9. // R, L, T, B, P
  10.  
  11. // pair<int, int>
  12. // vector<X>
  13.  
  14. #include <cstdlib>
  15. #include <iostream>
  16. #include <utility>
  17. #include <vector>
  18.  
  19. const int MAP_DIMENSION = 22;
  20.  
  21. bool hits_wall(std::vector<std::vector<bool>> map_walls,
  22.                std::pair<int, int> position, std::pair<int, int> exit) {
  23.   int x = position.first;
  24.   int y = position.second;
  25.  
  26.   // Puoi raggiungere l'uscita
  27.   if (x == exit.first && y == exit.second) return false;
  28.  
  29.   return map_walls.at(x).at(y) || (x == 0 || x == map_walls.size() - 1) ||
  30.          (y == map_walls.size() - 1 || y == 0);
  31.   // if(map_walls[x][y] == true) return true; else return false;
  32. }
  33.  
  34. void print_map(std::vector<std::vector<bool>> map_walls,
  35.                std::pair<int, int> position, std::pair<int, int> exit) {
  36.   using namespace std;
  37.  
  38.   for (int i = 0; i < map_walls.size(); i++) {
  39.     for (int j = 0; j < map_walls.size(); j++) {
  40.       // Bordo in alto o a sinistra
  41.       if (position.first == i && position.second == j) {
  42.         cout << "@";
  43.         continue;
  44.       }
  45.  
  46.       // Uscita
  47.       if (i == exit.first && j == exit.second) {
  48.         cout << " ";
  49.         continue;
  50.       }
  51.  
  52.       if (i == 0 || i == map_walls.size() - 1) {
  53.         cout << "═";
  54.         continue;
  55.       }
  56.  
  57.       // Bordo in basso o a destra
  58.       if (j == map_walls.size() - 1 || j == 0) {
  59.         cout << "║";
  60.         continue;
  61.       }
  62.  
  63.       if (map_walls.at(i).at(j)) {
  64.         cout << "▓";
  65.         continue;
  66.       }
  67.  
  68.       cout << " ";
  69.     }
  70.     cout << endl;
  71.   }
  72.   cout << endl << endl << endl << endl;
  73. }
  74.  
  75. int main() {
  76.   using namespace std;
  77.   pair<int, int> current_position(5, 6);  // { first: 0, second: 0 }
  78.   vector<pair<int, int>> history;         // { }
  79.   vector<vector<bool>> map_walls(MAP_DIMENSION, vector<bool>(MAP_DIMENSION));
  80.  
  81.   // Positioning a wall at 3, (7, 8, 9)
  82.   map_walls.at(3).at(7) = true;
  83.   map_walls.at(3).at(8) = true;
  84.   map_walls.at(3).at(9) = true;
  85.   map_walls.at(1).at(4) = true;
  86.   map_walls.at(1).at(3) = true;
  87.   map_walls.at(16).at(14) = true;
  88.   map_walls.at(15).at(14) = true;
  89.   map_walls.at(13).at(14) = true;
  90.  
  91.   while (true) {
  92.     pair<int, int> exit(14, 21);
  93.     print_map(map_walls, current_position, exit);
  94.     cout << endl << "Command(L, R, D, U): ";
  95.     char command;
  96.     cin >> command;
  97.  
  98.     switch (command) {
  99.       case 'D':
  100.         if (!(hits_wall(map_walls,
  101.                         pair<int, int>(current_position.first + 1,
  102.                                        current_position.second),
  103.                         exit))) {
  104.           current_position.first++;
  105.         }
  106.         // y - 1
  107.         break;
  108.  
  109.       case 'U':
  110.         if (!(hits_wall(map_walls,
  111.                         pair<int, int>(current_position.first - 1,
  112.                                        current_position.second),
  113.                         exit))) {
  114.           current_position.first--;
  115.         }
  116.  
  117.         // x + 1
  118.         break;
  119.  
  120.       case 'R':
  121.         if (!(hits_wall(map_walls,
  122.                         pair<int, int>(current_position.first,
  123.                                        current_position.second + 1),
  124.                         exit))) {
  125.           current_position.second++;
  126.         }
  127.         // y + 1
  128.         break;
  129.  
  130.       case 'L':
  131.         if (!(hits_wall(map_walls,
  132.                         pair<int, int>(current_position.first,
  133.                                        current_position.second - 1),
  134.                         exit))) {
  135.           current_position.second--;
  136.         }
  137.         // y - 1
  138.         break;
  139.     }
  140.  
  141.     if (current_position.first == exit.first &&
  142.         current_position.second == exit.second) {
  143.       cout << endl << "YOU WIN!" << endl;
  144.       break;
  145.     }
  146.   }
  147.  
  148.   return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement