Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <cstring>
- #include <conio.h>
- using namespace std;
- const int k_MazeWidth = 42;
- const int k_MazeHeight = 42;
- int playerXposition = 1;
- int playerYposition = 1;
- int goalXposition = 39;
- int goalYposition = 39;
- int i = 0;
- int j = 0;
- char maze[k_MazeWidth][k_MazeHeight]
- {
- { "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" },
- { "| | | | | | | |-|" },
- { "+ +-+-+ +-+ + + + + +-+-+-+ + + +-+-+ + +" },
- { "| | | | | | | | | |" },
- { "+-+ + + + + +-+-+-+-+-+-+-+-+-+-+-+ + + +" },
- { "| | | | | | | | | |" },
- { "+ +-+ +-+ + + +-+-+-+-+ + +-+ + +-+-+ + +" },
- { "| | | | | | | | | | | | |" },
- { "+ + +-+ + +-+ +-+ + + + + + +-+-+ +-+-+-+" },
- { "| | | | | | | | |" },
- { "+-+-+ +-+-+-+-+ +-+-+ +-+-+ +-+-+ +-+ + +" },
- { "| | | | | | | | | | |" },
- { "+ + +-+ + +-+-+-+ + +-+ + +-+ + +-+ + + +" },
- { "| | | | | | | | | | | |" },
- { "+ +-+-+-+-+ + +-+-+ + +-+-+ + +-+-+-+ + +" },
- { "| | | | | | | | | | |" },
- { "+-+ +-+-+ +-+ + + + +-+ + +-+ +-+-+ +-+ +" },
- { "| | | | | | | | | | | | |" },
- { "+ +-+ + +-+ +-+ + + + +-+-+ + + + +-+ + +" },
- { "| | | | | | | | | | | | | | | |" },
- { "+-+ +-+ + +-+ +-+ + +-+ +-+ +-+ + + + + +" },
- { "| | | | | | | | | | |" },
- { "+ +-+ +-+-+-+-+ + + + + + + +-+-+-+-+-+-+" },
- { "| | | | | | | | | | | | |" },
- { "+ + +-+ + +-+ + + + + + +-+ + +-+-+ + +-+" },
- { "| | | | | | | | | | | | |" },
- { "+ + + + +-+ +-+-+-+-+ +-+ +-+-+-+-+ +-+ +" },
- { "| | | | | | | | | | | |" },
- { "+ + + +-+-+-+ +-+ + + + + + +-+-+ + + +-+" },
- { "| | | | | | | | |" },
- { "+-+ +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+ +" },
- { "| | | | | | | | |" },
- { "+ +-+ + + + +-+ +-+ +-+-+ + +-+-+ +-+ + +" },
- { "| | | | | | | | | | | | |" },
- { "+ + + +-+-+-+ +-+-+-+ + +-+ +-+ +-+ + + +" },
- { "| | | | | | | | | | | |" },
- { "+ +-+ + +-+ +-+ +-+ +-+-+-+ + + + +-+-+ +" },
- { "| | | | | | | | | | | |" },
- { "+ + +-+ +-+-+ +-+ + + + + +-+ + + +-+ + +" },
- { "| | | | | | | | |" },
- { "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" }
- };
- char GetInput()
- {
- char input = _getch();
- return input;
- }
- void Drawmaze()
- {
- for (int i = 0; i < k_MazeHeight; i++) // First dimension loop.
- {
- for (int j = 0; j < k_MazeWidth; j++) // Second dimension loop.
- {
- if (j == playerXposition && i == playerYposition)
- {
- cout << "@";
- }
- else if (j == goalXposition && i == goalYposition)
- {
- cout << "X";
- }
- else
- {
- cout << maze[i][j]; // Use both loop variables.
- }
- }
- cout << endl; // Print a newline character so we get a nice matrix.
- }
- }
- int main()
- {
- int acknoledgeControls;
- cout << "Use w,a,s,d keys to navigate the maze. Press any key and enter to continue." << endl;
- cin >> acknoledgeControls;
- // declare step variable ++ has to do with x, y position 4 different places anytime player position changes
- int stepCounter = 0;
- while (playerYposition != goalYposition || playerXposition != goalXposition)
- {
- system("cls"); // clears the previous movement, leaving a nice clean slate for the user
- Drawmaze(); // this draws the maze in the "for loop"
- char userInput;
- userInput = GetInput();
- if (userInput == 'd')
- {
- // Check if the slope besides the player x is blank
- if (maze[playerYposition][playerXposition + 1] == ' ')
- {
- // Update player position
- playerXposition = playerXposition + 1; //right 1 space
- stepCounter++; // this will count steps ONLY when d is pressed by the user AND if the space is blank.
- }
- else
- {
- maze[playerYposition][playerXposition]; //wall
- }
- }
- else if (userInput == 'a')
- {
- // Check if the slope besides the player x is blank
- if (maze[playerYposition][playerXposition - 1] == ' ')
- {
- playerXposition = playerXposition - 1; //left 1 space
- stepCounter++; // this will count steps ONLY when a is pressed by the user AND if the space is blank.
- }
- else
- {
- maze[playerYposition][playerXposition]; //wall
- }
- }
- else if (userInput == 'w')
- {
- // Check if the slope besides the player y is blank
- if (maze[playerYposition - 1][playerXposition] == ' ')
- {
- playerYposition = playerYposition - 1; //up 1 space
- stepCounter++; // this will count steps ONLY when w is pressed by the user AND if the space is blank.
- }
- else
- {
- maze[playerYposition][playerXposition]; //wall
- }
- }
- else if (userInput == 's')
- {
- // Check if the slope besides the player y is blank
- if (maze[playerYposition + 1][playerXposition] == ' ')
- {
- playerYposition = playerYposition + 1; //down 1 space
- stepCounter++; // this will count steps ONLY when s is pressed by the user AND if the space is blank.
- }
- else
- {
- maze[playerYposition][playerXposition]; //wall
- }
- }
- }
- cout << "Congratulations! You Have Completed The Maze In " << stepCounter; cout << " Steps!" << endl;
- system("pause");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement