1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int WIDTH = 20;
const int HEIGHT = 10;
//---------------------------------------------------------------
// Function prototypes
//---------------------------------------------------------------
void printMaze(char maze[][WIDTH], int curx, int cury);
bool validMove(char maze[][WIDTH], int newX, int newY);
bool move(char maze[][WIDTH], int& curX, int& curY,
int newX, int newY);
// Return true or false if moving to the specified coordinate is valid
bool validMove(char maze[][WIDTH], int newX, int newY)
{
// Check for going off the maze edges
if (newX < 0 || newX >= WIDTH)
return false;
if (newY < 0 || newY >= HEIGHT)
return false;
// Check if target is a wall
if (maze[newY][newX] == 'X')
return false;
return true;
}
//---------------------------------------------------------------
// Make the move on the maze to move to a new coordinate
// I passed curX and curY by reference so they are changed to
// the new coordinates. I assume the move coordinates are valid.
// This returns true if we move onto the exit, false otherwise.
//---------------------------------------------------------------
bool move(char maze[][WIDTH], int& curX, int& curY, int newX, int newY)
{
bool foundExit = false;
if (maze[newY][newX] == 'E') // Check for exit
foundExit = true;
curX = newX; // Update location
curY = newY;
return foundExit;
}
//---------------------------------------------------------------
// Display the maze in ASCII
//---------------------------------------------------------------
void printMaze(char maze[][WIDTH], int curx, int cury)
{
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
if ((x == curx) && (y == cury))
cout << "T";
else
cout << maze[y][x];
}
cout << endl;
}
}
//---------------------------------------------------------------
// MAIN PROGRAM
//---------------------------------------------------------------
int main()
{
char maze[HEIGHT][WIDTH] = {
{'X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X'},
{'X',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','X',' ',' ',' ','X'},
{'X',' ','X',' ',' ',' ','X',' ',' ',' ',' ','X',' ',' ',' ','X',' ',' ',' ','X'},
{'X',' ','X','X','X',' ','X',' ','X','X','X','X',' ',' ',' ',' ',' ',' ',' ','X'},
{'X',' ',' ',' ','X',' ','X',' ',' ',' ',' ',' ',' ','X','X','X',' ',' ','X','X'},
{'X',' ',' ',' ','X',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','X',' ',' ',' ','X'},
{'X',' ','X','X','X',' ',' ',' ',' ',' ',' ','X',' ','X','X','X',' ','X',' ','X'},
{'X',' ','X',' ',' ',' ','X',' ','X','X','X','X',' ',' ',' ',' ',' ','X',' ','X'},
{'X',' ',' ',' ',' ',' ','X',' ',' ',' ',' ',' ',' ',' ',' ','X',' ','X','E','X'},
{'X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X'}
};
int x = 1, y = 1;
bool foundExit = false;
while (!foundExit)
{
printMaze(maze, x, y);
cout << "Enter WASD (T) or JIKL (M) to move." << endl;
cout << "TAKE TURNS" << endl;
char c;
cin >> c;
c = tolower(c);
switch (c)
{
case 'w':
if (validMove(maze, x, y - 1))
foundExit = move(maze, x, y, x, y - 1);
break;
case 'a':
if (validMove(maze, x - 1, y))
foundExit = move(maze, x, y, x - 1, y);
break;
case 's':
if (validMove(maze, x, y + 1))
foundExit = move(maze, x, y, x, y + 1);
break;
case 'd':
if (validMove(maze, x + 1, y))
foundExit = move(maze, x, y, x + 1, y);
}
}
}
//=========================================================================
|