#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <cstdlib>
#define _WIN32_WINNT 0x0500
using namespace std;
enum Colors{blue=1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite};
//global variables
int maze[20][20]; //the matrix for the maze itself
int i, j; //maze counters
int xpos, ypos; //position of the AI in the maze
int direction = 0; //direction of the AI
int wait; //pause counter
//function prototypes
void coutc(int color, char*output);
void generate();
void display();
void step(int &direction);
void move(int x, int y);
bool moveIsPossible(int x, int y);
int restart();
///initializes the program and calls the other functions
int main()
{
//initialization
cout << "Welcome to Maze Navigator!" << endl << endl;
system("pause");
generate();
//loop to generate each frame of the maze
while (maze[0][10] != 2)
{
//generates a frame
step(direction);
//pauses before generating the next frame
for (wait = 0; wait < 20000000; wait++);
}
//displays upon maze completion
system("cls");
coutc(grey, "Maze Solved!");
restart();
}
///outputs a value with a specified text color
void coutc(int color, char* output)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, color);
cout << output;
SetConsoleTextAttribute(handle, color);
}
///generates a randomly structured maze
void generate()
{
int spaceFreq;
srand(GetTickCount());
system("cls");
cout << "Enter a space frequency between 1 and 10: ";
cin >> spaceFreq;
spaceFreq++;
//checks whether the chosen space frequency is valid
if(spaceFreq < 2 || spaceFreq > 10)
{
system("cls");
cout << "Error: Invalid space frequency!" << endl << endl;
system("pause"); system("cls");
main();
}
//builds a randomized maze based on the chosen space frequency
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
maze[i][j] = rand() % spaceFreq + 1;
if(maze[i][j] != 0 && maze[i][j] != 1) maze[i][j] = 0;
}
}
//ensures the edges of the maze are solid
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
maze[0][j] = 1;
maze[19][j] = 1;
maze[i][0] = 1;
maze[i][19] = 1;
}
}
maze[19][9] = 2; //sets start point
maze[0][10] = 0; //sets exit point
maze[18][9] = 0; //sets start point opening
maze[1][10] = 0; //sets exit point opening
xpos = 9; //initializes AI x position
ypos = 19; //initializes AI y position
}
///displays the maze on the console
void display()
{
system("cls");
cout << endl;
coutc(hgreen, " $");
cout << endl;
for(int i = 0; i < 20; i++)
{
cout << " ";
//specifies the characters corresponding to different components of the maze
for(int j = 0; j < 20; j++)
{
switch(maze[i][j])
{
case 0: coutc(blue, " "); break; //empty space
case 1: coutc(green, " X"); break; //solid wall
case 2:
{
if(direction == 0){coutc(hblue, " >"); break;} //AI facing right
else if(direction == 1){coutc(hblue, " V"); break;} //AI facing down
else if(direction == 2){coutc(hblue, " <"); break;} //AI facing left
else if(direction == 3){coutc(hblue, " ^"); break;} //AI facing up
}
case 3: coutc(blue, " '"); break; //path marker
case 4: coutc(blue, " *"); break; //repeated path marker
default: cout << " " << maze[i][j]; //default component
}
}
cout << endl;
}
coutc(hred, " @");
}
///AI to make the character move through the maze
void step(int &direction)
{
display();
switch(direction)
{
case 0:
direction = 1;
moveIsPossible(0, 1);
if(moveIsPossible(0, 1)){move(0, 1); break;}
case 1:
direction = 2;
moveIsPossible(1, 0);
if(moveIsPossible(1, 0)){move(1, 0); break;}
case 2:
direction = 3;
moveIsPossible(0, -1);
if(moveIsPossible(0, -1)){move(0, -1); break;}
case 3:
direction = 0;
moveIsPossible(-1, 0);
if(moveIsPossible(-1, 0)){move(-1, 0); break;}
default: direction = 0;
}
}
///moves the AI through the maze
void move(int x, int y)
{
maze[ypos + x][xpos + y] = 2;
maze[ypos][xpos] = 3;
ypos = ypos + x;
xpos = xpos + y;
}
///checks to see if a move is possible
bool moveIsPossible(int x, int y)
{
if(maze[ypos + x][xpos + y] != 0) return false;
else if(ypos == 19 && direction == 1) return false;
else return true;
}
///program exits or restarts depending on user input
int restart()
{
int choice;
cout << endl << "Do you want to run another maze? (1/0)" << endl;
cin >> choice;
switch(choice)
{
case 0: return 0; break;
case 1: system("cls"); return main(); break;
default: return 0;
}
}