Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <cstring>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. const int k_MazeWidth = 42;
  9. const int k_MazeHeight = 42;
  10. int playerXposition = 1;
  11. int playerYposition = 1;
  12. int goalXposition = 39;
  13. int goalYposition = 39;
  14. int i = 0;
  15. int j = 0;
  16. char maze[k_MazeWidth][k_MazeHeight]
  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.  
  61. char GetInput()
  62. {
  63.     char input = _getch();
  64.     return input;
  65. }
  66. void Drawmaze()
  67. {
  68.     for (int i = 0; i < k_MazeHeight; i++) // First dimension loop.
  69.     {
  70.         for (int j = 0; j < k_MazeWidth; j++) // Second dimension loop.
  71.         {
  72.             if (j == playerXposition && i == playerYposition)
  73.             {
  74.                 cout << "@";
  75.             }
  76.             else if (j == goalXposition && i == goalYposition)
  77.             {
  78.                 cout << "X";
  79.             }
  80.             else
  81.             {
  82.                 cout << maze[i][j];  // Use both loop variables.
  83.             }
  84.         }
  85.         cout << endl;  // Print a newline character so we get a nice matrix.
  86.     }
  87. }
  88. int main()
  89. {
  90.     int acknoledgeControls;
  91.     cout << "Use w,a,s,d keys to navigate the maze. Press any key and enter to continue." << endl;
  92.     cin >> acknoledgeControls;
  93.     // declare step variable ++ has to do with x, y position 4 different places anytime player position changes
  94.     int stepCounter = 0;
  95.         while (playerYposition != goalYposition || playerXposition != goalXposition)
  96.         {
  97.             system("cls"); // clears the previous movement, leaving a nice clean slate for the user
  98.             Drawmaze(); // this draws the maze in the "for loop"
  99.             char userInput;
  100.             userInput = GetInput();
  101.             if (userInput == 'd')
  102.             {
  103.             // Check if the slope besides the player x is blank
  104.                 if (maze[playerYposition][playerXposition + 1] == ' ')
  105.                 {
  106.                     // Update player position
  107.                     playerXposition = playerXposition + 1; //right 1 space
  108.                     stepCounter++; // this will count steps ONLY when d is pressed by the user AND if the space is blank.
  109.                 }
  110.                 else
  111.                 {
  112.                     maze[playerYposition][playerXposition]; //wall
  113.                 }
  114.             }
  115.             else if (userInput == 'a')
  116.             {
  117.                 // Check if the slope besides the player x is blank
  118.                 if (maze[playerYposition][playerXposition - 1] == ' ')
  119.                 {
  120.                     playerXposition = playerXposition - 1; //left 1 space
  121.                     stepCounter++; // this will count steps ONLY when a is pressed by the user AND if the space is blank.
  122.                 }
  123.                 else
  124.                 {
  125.                     maze[playerYposition][playerXposition]; //wall
  126.                 }
  127.             }
  128.             else if (userInput == 'w')
  129.             {
  130.                 // Check if the slope besides the player y is blank
  131.                 if (maze[playerYposition - 1][playerXposition] == ' ')
  132.                 {
  133.                     playerYposition = playerYposition - 1; //up 1 space
  134.                     stepCounter++; // this will count steps ONLY when w is pressed by the user AND if the space is blank.
  135.                 }
  136.                 else
  137.                 {
  138.                     maze[playerYposition][playerXposition]; //wall
  139.                 }
  140.             }
  141.             else if (userInput == 's')
  142.             {
  143.                 // Check if the slope besides the player y is blank
  144.                 if (maze[playerYposition + 1][playerXposition] == ' ')
  145.                 {
  146.                     playerYposition = playerYposition + 1; //down 1 space
  147.                     stepCounter++; // this will count steps ONLY when s is pressed by the user AND if the space is blank.
  148.                 }
  149.                 else
  150.                 {
  151.                     maze[playerYposition][playerXposition]; //wall
  152.                 }
  153.             }
  154.         }
  155.         cout << "Congratulations! You Have Completed The Maze In " << stepCounter; cout << " Steps!" << endl;
  156.         system("pause");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement