Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #define ROW 10
- #define COL 10
- using namespace std;
- char Maze[ROW][COL] = {
- {'S',' ','*','*','*','*','*','*','*','*'},
- {'*',' ','*',' ','*','*',' ',' ','*','*'},
- {'*',' ',' ',' ','*','*','*','*',' ','*'},
- {'*','*','*',' ',' ','*','*',' ',' ','*'},
- {'*',' ',' ',' ','*','*',' ',' ','*','*'},
- {'*','*','*',' ',' ',' ','*','*',' ','*'},
- {'*','*','*','*','*',' ',' ','*',' ','*'},
- {'*','*',' ',' ',' ',' ',' ',' ',' ','*'},
- {'*',' ',' ','*',' ',' ','*',' ','*','*'},
- {'*','E','*','*','*','*','*','*','*','*'},
- };
- class aMaze {
- public:
- void draw() {
- for (int i = 0; i < ROW; i++) {
- for (int j = 0; j < COL; j++) {
- cout << Maze[i][j];
- }
- cout << " " << endl;
- }
- }
- void print(int energy=10) {
- for (int i = 0; i < ROW; i++) {
- for (int j = 0; j < COL; j++) {
- cout << Maze[i][j];
- }
- cout << " " << endl;
- }
- cout << "Remaining energy: " << energy << endl;
- }
- };
- class Hero :public aMaze {
- private:
- char hero;
- char move;
- int row, col;
- protected:
- int energy;
- public:
- Hero() {
- row = 1;
- col = 1;
- hero = 'H';
- Maze[row][col] = hero;
- energy = 10;
- }
- int Move() {
- cout << "Enter w,a,s,z to move:";
- cin >> move;
- cout << " " << endl;
- if (move == 'W' || move == 'w') {
- row--;
- if (row >= 0 && row < ROW && col >= 0 && col < COL) {
- if (Maze[row][col] != '*') {
- Maze[row][col] = hero;
- Maze[row + 1][col] = ' ';
- energy--;
- }
- else {
- row++;
- Maze[row][col] = hero;
- }
- }
- else {
- row++;
- Maze[row][col] = hero;
- }
- }
- if (move == 'Z' || move == 'z') {
- row++;
- if (row >= 0 && row < ROW && col >= 0 && col < COL) {
- if (Maze[row][col] != '*') {
- Maze[row][col] = hero;
- Maze[row - 1][col] = ' ';
- energy--;
- }
- else {
- row--;
- Maze[row][col] = hero;
- }
- }
- else {
- row--;
- Maze[row][col] = hero;
- }
- }
- if (move == 'a' || move == 'A') {
- col--;
- if (col >= 0 && col < COL && row >= 0 && row < ROW) {
- if (Maze[row][col] != '*') {
- Maze[row][col] = hero;
- Maze[row ][col+1] = ' ';
- energy--;
- }
- else {
- col++;
- Maze[row][col] = hero;
- }
- }
- else {
- col++;
- Maze[row][col] = hero;
- }
- }
- if (move == 's' || move == 'S') {
- col++;
- if (col >= 0 && col < COL && row >= 0 && row < ROW) {
- if (Maze[row][col] != '*') {
- Maze[row][col] = hero;
- Maze[row][col-1] = ' ';
- energy--;
- }
- else {
- col--;
- Maze[row][col] = hero;
- }
- }
- else {
- col--;
- Maze[row][col] = hero;
- }
- }
- return energy;
- }
- };
- int main() {
- int energy;
- char move;
- aMaze m1;
- m1.draw();
- Hero h1;
- system("cls");
- h1.print();
- while (1) {
- energy = h1.Move();
- system("cls");
- h1.print(energy);
- if (energy == 0) {
- cout << "Hero died!" << endl;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement