Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include "Player.h"
  4.  
  5. using namespace std;
  6.  
  7. const int k_MazeWidth = 42;
  8. const int k_MazeHeight = 42;
  9.  
  10. class Maze
  11. {
  12. public:
  13.     int stepCounter;
  14.  
  15.     char maze[k_MazeWidth][k_MazeHeight]
  16.     {
  17.         { "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" },
  18.     { "| |     |   |   |   |         |       |-|" },
  19.     { "+ +-+-+ +-+ + + + + +-+-+-+ + + +-+-+ + +" },
  20.     { "|   |     |   |   |         |   |   | | |" },
  21.     { "+-+ + + + + +-+-+-+-+-+-+-+-+-+-+-+ + + +" },
  22.     { "|   | | | | |           |     |       | |" },
  23.     { "+ +-+ +-+ + + +-+-+-+-+ + +-+ + +-+-+ + +" },
  24.     { "| |     |   | |     | | | | |   |   |   |" },
  25.     { "+ + +-+ + +-+ +-+ + + + + + +-+-+ +-+-+-+" },
  26.     { "|   |   |   |   | |     |             | |" },
  27.     { "+-+-+ +-+-+-+-+ +-+-+ +-+-+ +-+-+ +-+ + +" },
  28.     { "|   |   |       |   |     | |   |   | | |" },
  29.     { "+ + +-+ + +-+-+-+ + +-+ + +-+ + +-+ + + +" },
  30.     { "| |     |   |     | |   |   | |     | | |" },
  31.     { "+ +-+-+-+-+ + +-+-+ + +-+-+ + +-+-+-+ + +" },
  32.     { "|       |   | |   | | |   |   |     |   |" },
  33.     { "+-+ +-+-+ +-+ + + + +-+ + +-+ +-+-+ +-+ +" },
  34.     { "|   |   |   |   | | |   |   | |     | | |" },
  35.     { "+ +-+ + +-+ +-+ + + + +-+-+ + + + +-+ + +" },
  36.     { "|   | |   |   | | |   |   | | | | | | | |" },
  37.     { "+-+ +-+ + +-+ +-+ + +-+ +-+ +-+ + + + + +" },
  38.     { "|   |   |   |   | |   | |       |   |   |" },
  39.     { "+ +-+ +-+-+-+-+ + + + + + + +-+-+-+-+-+-+" },
  40.     { "| |   | |     | | | | |   | |       |   |" },
  41.     { "+ + +-+ + +-+ + + + + + +-+ + +-+-+ + +-+" },
  42.     { "| | |   |   |     | | | |   |     | |   |" },
  43.     { "+ + + + +-+ +-+-+-+-+ +-+ +-+-+-+-+ +-+ +" },
  44.     { "| | | |     |   |   | |   |       | |   |" },
  45.     { "+ + + +-+-+-+ +-+ + + + + + +-+-+ + + +-+" },
  46.     { "|   |       |     |   | |       |   |   |" },
  47.     { "+-+ +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+ +" },
  48.     { "|   |   | |     |         |       |   | |" },
  49.     { "+ +-+ + + + +-+ +-+ +-+-+ + +-+-+ +-+ + +" },
  50.     { "| | | |     | |     | |   | |   |   |   |" },
  51.     { "+ + + +-+-+-+ +-+-+-+ + +-+ +-+ +-+ + + +" },
  52.     { "|   | |   |     |   |   |     | | |   | |" },
  53.     { "+ +-+ + +-+ +-+ +-+ +-+-+-+ + + + +-+-+ +" },
  54.     { "| |   |     |   |   |   |   | | |     | |" },
  55.     { "+ + +-+ +-+-+ +-+ + + + + +-+ + + +-+ + +" },
  56.     { "| |         |     |   |     |   |   |   |" },
  57.     { "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" }
  58.     };
  59.  
  60.     void DrawMaze(Player *player, int goalX, int goalY)
  61.     {
  62.             for (int i = 0; i < k_MazeHeight; i++) // First dimension loop.
  63.             {
  64.                 for (int j = 0; j < k_MazeWidth; j++) // Second dimension loop.
  65.                 {
  66.                     if (j == player->GetX() && i == player->GetY)
  67.                     {
  68.                         cout << "@";
  69.                     }
  70.                     else if (j == goalX && i == goalY)
  71.                     {
  72.                         cout << "X";
  73.                     }
  74.                     else
  75.                     {
  76.                         cout << maze[i][j];  // Use both loop variables.
  77.                     }
  78.                 }
  79.                 cout << endl;  // Print a newline character so we get a nice matrix.
  80.             }
  81.     }
  82.  
  83.     bool CheckCollision(char userInput, Player *player)
  84.     {
  85.         if (userInput == 'd')
  86.         {
  87.             // Check if the slope besides the player x is blank
  88.             if (maze[player->GetY()][player->GetX() + 1] == ' ')
  89.             {
  90.                 // Update player position
  91.                 player->SetX(player->GetX() + 1); //right 1 space
  92.                 stepCounter++; // this will count steps ONLY when d is pressed by the user AND if the space is blank.
  93.             }
  94.             else
  95.             {
  96.                 maze[player->GetY()][player->GetX()]; //wall
  97.             }
  98.         }
  99.         else if (userInput == 'a')
  100.         {
  101.             // Check if the slope besides the player x is blank
  102.             if (maze[player->GetY()][player->GetX() - 1] == ' ')
  103.             {
  104.                 player->SetX(player->GetX() - 1); //left 1 space
  105.                 stepCounter++; // this will count steps ONLY when a is pressed by the user AND if the space is blank.
  106.             }
  107.             else
  108.             {
  109.                 maze[player->GetY()][player->GetX()]; //wall
  110.             }
  111.         }
  112.         else if (userInput == 'w')
  113.         {
  114.             // Check if the slope besides the player y is blank
  115.             if (maze[player->GetY() - 1][player->GetX()] == ' ')
  116.             {
  117.                 player->SetY(player->GetY() - 1); //up 1 space
  118.                 stepCounter++; // this will count steps ONLY when w is pressed by the user AND if the space is blank.
  119.             }
  120.             else
  121.             {
  122.                 maze[player->GetY()][player->GetX()]; //wall
  123.             }
  124.         }
  125.         else if (userInput == 's')
  126.         {
  127.             // Check if the slope besides the player y is blank
  128.             if (maze[player->GetY() + 1][player->GetX()] == ' ')
  129.             {
  130.                 player->SetY(player->GetY + 1); //down 1 space
  131.                 stepCounter++; // this will count steps ONLY when s is pressed by the user AND if the space is blank.
  132.             }
  133.             else
  134.             {
  135.                 maze[player->GetY()][player->GetX()]; //wall
  136.             }
  137.         }
  138.     }
  139. private:
  140.    
  141.    
  142.     int i, j;
  143. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement