Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cstdlib>
  4. #define ROW 10
  5. #define COL 10
  6. using namespace std;
  7.  
  8. char Maze[ROW][COL] = {
  9. {'S',' ','*','*','*','*','*','*','*','*'},
  10. {'*',' ','*',' ','*','*',' ',' ','*','*'},
  11. {'*',' ',' ',' ','*','*','*','*',' ','*'},
  12. {'*','*','*',' ',' ','*','*',' ',' ','*'},
  13. {'*',' ',' ',' ','*','*',' ',' ','*','*'},
  14. {'*','*','*',' ',' ',' ','*','*',' ','*'},
  15. {'*','*','*','*','*',' ',' ','*',' ','*'},
  16. {'*','*',' ',' ',' ',' ',' ',' ',' ','*'},
  17. {'*',' ',' ','*',' ',' ','*',' ','*','*'},
  18. {'*','E','*','*','*','*','*','*','*','*'},
  19. };
  20.  
  21. class aMaze {
  22.  
  23. public:
  24.     void draw() {
  25.         for (int i = 0; i < ROW; i++) {
  26.             for (int j = 0; j < COL; j++) {
  27.                 cout << Maze[i][j];
  28.             }
  29.             cout << " " << endl;
  30.         }
  31.     }
  32.     void print(int energy=10) {
  33.         for (int i = 0; i < ROW; i++) {
  34.             for (int j = 0; j < COL; j++) {
  35.                 cout << Maze[i][j];
  36.             }
  37.             cout << " " << endl;
  38.         }
  39.         cout << "Remaining energy: " << energy << endl;
  40.     }
  41. };
  42.  
  43. class Hero :public aMaze {
  44. private:
  45.     char hero;
  46.     char move;
  47.     int row, col;
  48.    
  49. protected:
  50.     int energy;
  51.  
  52. public:
  53.     Hero() {
  54.         row = 1;
  55.         col = 1;
  56.         hero = 'H';
  57.         Maze[row][col] = hero;
  58.         energy = 10;
  59.     }
  60.     int Move() {
  61.         cout << "Enter w,a,s,z to move:";
  62.         cin >> move;
  63.         cout << " " << endl;
  64.         if (move == 'W' || move == 'w') {
  65.             row--;
  66.             if (row >= 0 && row < ROW && col >= 0 && col < COL) {
  67.                 if (Maze[row][col] != '*') {
  68.                     Maze[row][col] = hero;
  69.                     Maze[row + 1][col] = ' ';
  70.                     energy--;
  71.                 }
  72.                 else {
  73.                     row++;
  74.                     Maze[row][col] = hero;
  75.                 }
  76.             }
  77.             else {
  78.                 row++;
  79.                 Maze[row][col] = hero;
  80.            
  81.             }
  82.         }
  83.         if (move == 'Z' || move == 'z') {
  84.             row++;
  85.             if (row >= 0 && row < ROW && col >= 0 && col < COL) {
  86.                 if (Maze[row][col] != '*') {
  87.                     Maze[row][col] = hero;
  88.                     Maze[row - 1][col] = ' ';
  89.                     energy--;
  90.                 }
  91.                 else {
  92.                     row--;
  93.                     Maze[row][col] = hero;
  94.                 }
  95.             }
  96.             else {
  97.                 row--;
  98.                 Maze[row][col] = hero;
  99.  
  100.             }
  101.         }
  102.         if (move == 'a' || move == 'A') {
  103.             col--;
  104.             if (col >= 0 && col < COL && row >= 0 && row < ROW) {
  105.                 if (Maze[row][col] != '*') {
  106.                     Maze[row][col] = hero;
  107.                     Maze[row ][col+1] = ' ';
  108.                     energy--;
  109.                 }
  110.                 else {
  111.                     col++;
  112.                     Maze[row][col] = hero;
  113.                 }
  114.             }
  115.             else {
  116.                 col++;
  117.                 Maze[row][col] = hero;
  118.  
  119.             }
  120.         }
  121.         if (move == 's' || move == 'S') {
  122.             col++;
  123.             if (col >= 0 && col < COL && row >= 0 && row < ROW) {
  124.                 if (Maze[row][col] != '*') {
  125.                     Maze[row][col] = hero;
  126.                     Maze[row][col-1] = ' ';
  127.                     energy--;
  128.                 }
  129.                 else {
  130.                     col--;
  131.                     Maze[row][col] = hero;
  132.                 }
  133.             }
  134.             else {
  135.                 col--;
  136.                 Maze[row][col] = hero;
  137.  
  138.             }
  139.         }
  140.    
  141.         return energy;
  142.     }
  143. };
  144.  
  145. int main() {
  146.     int energy;
  147.     char move;
  148.  
  149.     aMaze m1;
  150.     m1.draw();
  151.     Hero h1;
  152.     system("cls");
  153.     h1.print();
  154.  
  155.     while (1) {
  156.         energy = h1.Move();
  157.         system("cls");
  158.         h1.print(energy);
  159.         if (energy == 0) {
  160.             cout << "Hero died!" << endl;
  161.             break;
  162.         }
  163.  
  164.     }
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement