232 lines
No EOL
6.6 KiB
C++
232 lines
No EOL
6.6 KiB
C++
/*
|
||
Program Name: maze
|
||
Process: Does the following according to spec:
|
||
Develop a text-based maze game where the user will navigate through to
|
||
find the exit. Your game will do the following:
|
||
● Welcome the user to the Text Maze Game (or rename it something else)
|
||
● Explain the goal to the user (leave the maze).
|
||
● Create a 3x3 array that will represent the maze where each cell is a room.
|
||
● Define the starting point and the exit point in the maze/array.
|
||
● Prompt the user to move through the maze using “N” for north,
|
||
“S” for south, “E” for east, and “W” for west.
|
||
● Make sure the user can’t leave the boundary of the array.
|
||
● Display a message that congratulates them for finding the exit.
|
||
Function Input/Parameters: ???
|
||
Function Output/Parameters: ???
|
||
Function Output/Returned: ???
|
||
Device Input/Device: ???
|
||
Device Output/Monitor: ???
|
||
Dependencies: ???
|
||
*/
|
||
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <iostream>
|
||
#include <cstring>
|
||
|
||
using namespace std;
|
||
|
||
// function prototypes
|
||
|
||
// class definitions
|
||
|
||
// main program
|
||
int main(int argc, char **argv)
|
||
{
|
||
/*
|
||
Maze Layout:
|
||
Y Y Y
|
||
=============
|
||
| S | | | X
|
||
| B | | B | X
|
||
| E | | | X
|
||
=============
|
||
*/
|
||
|
||
bool mazeStop = true;
|
||
bool wrongWayFlag = false;
|
||
int currentX = 0;
|
||
int currentY = 0;
|
||
int currentIteration = 0;
|
||
// int endX = 0;
|
||
// int endY = 2;
|
||
// int blockerOneX = 0;
|
||
// int blockerOneY = 1;
|
||
// int blockerTwoX = 2;
|
||
// int blockerTwoY = 1;
|
||
char directionCharacter;
|
||
|
||
// welcome user to the maze game, explain rules, etc.
|
||
cout << "Welcome to the maze game!" << endl;
|
||
cout << "Find the end of the maze by entering in directions, such as N(North), E(East), S(South), W(West)." << endl;
|
||
cout << "Start game!" << endl;
|
||
cout << endl;
|
||
while (mazeStop)
|
||
{
|
||
// if at the starting position, display starting message.
|
||
if(currentX == 0 && currentY == 0)
|
||
{
|
||
// display starting message.
|
||
cout << "You are at the start of the maze. The only opening is east." << endl;
|
||
if(directionCharacter == 'e')
|
||
{
|
||
//
|
||
currentX = 0;
|
||
currentY = 1;
|
||
wrongWayFlag = false;
|
||
}
|
||
else
|
||
{
|
||
wrongWayFlag = true;
|
||
}
|
||
}
|
||
if(currentX == 0 && currentY == 1)
|
||
{
|
||
//
|
||
cout << "You have continued through the maze. There are openings to the east, west, and south." << endl;
|
||
if(directionCharacter == 'e')
|
||
{
|
||
//
|
||
currentX = 0;
|
||
currentY = 0;
|
||
wrongWayFlag = false;
|
||
}
|
||
if(directionCharacter == 'w')
|
||
{
|
||
//
|
||
currentX = 0;
|
||
currentY = 2;
|
||
wrongWayFlag = false;
|
||
}
|
||
if(directionCharacter == 's')
|
||
{
|
||
//
|
||
currentX = 1;
|
||
currentY = 1;
|
||
wrongWayFlag = false;
|
||
}
|
||
else
|
||
{
|
||
wrongWayFlag = true;
|
||
}
|
||
}
|
||
if(currentX == 0 && currentY == 2)
|
||
{
|
||
//
|
||
cout << "You are continuing through the maze, however, the only opening is to the west." << endl;
|
||
if(directionCharacter == 'w')
|
||
{
|
||
//
|
||
currentX = 0;
|
||
currentY = 1;
|
||
wrongWayFlag = false;
|
||
}
|
||
else
|
||
{
|
||
wrongWayFlag = true;
|
||
}
|
||
}
|
||
if(currentX == 1 && currentY == 1)
|
||
{
|
||
//
|
||
cout << "Continuing through the maze, the available directions are north and south." << endl;
|
||
if(directionCharacter == 'n')
|
||
{
|
||
//
|
||
currentX = 0;
|
||
currentY = 1;
|
||
wrongWayFlag = false;
|
||
}
|
||
if(directionCharacter == 's')
|
||
{
|
||
//
|
||
currentX = 2;
|
||
currentY = 1;
|
||
wrongWayFlag = false;
|
||
}
|
||
else
|
||
{
|
||
wrongWayFlag = true;
|
||
}
|
||
}
|
||
if(currentX == 2 && currentY == 0)
|
||
{
|
||
//
|
||
cout << "You have found the end! Congratulations!" << endl;
|
||
cout << "Press X to exit." << endl;
|
||
if(directionCharacter == 'x')
|
||
{
|
||
// set the ending flag to false.
|
||
mazeStop = false;
|
||
}
|
||
}
|
||
if(currentX == 2 && currentY == 1)
|
||
{
|
||
//
|
||
cout << "Wandering along, the new directions open are north, east, and west." << endl;
|
||
if(directionCharacter == 'n')
|
||
{
|
||
//
|
||
currentX = 1;
|
||
currentY = 1;
|
||
wrongWayFlag = false;
|
||
}
|
||
if(directionCharacter == 'e')
|
||
{
|
||
//
|
||
currentX = 2;
|
||
currentY = 2;
|
||
wrongWayFlag = false;
|
||
}
|
||
if(directionCharacter == 'w')
|
||
{
|
||
//
|
||
currentX = 2;
|
||
currentY = 0;
|
||
wrongWayFlag = false;
|
||
}
|
||
else
|
||
{
|
||
wrongWayFlag = true;
|
||
}
|
||
}
|
||
if(currentX == 2 && currentY == 2)
|
||
{
|
||
//
|
||
cout << "While continuing through the maze, you see the only opening is the west." << endl;
|
||
if(directionCharacter == 'w')
|
||
{
|
||
//
|
||
currentX = 2;
|
||
currentY = 1;
|
||
wrongWayFlag = false;
|
||
}
|
||
else
|
||
{
|
||
wrongWayFlag = true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if(currentIteration != 0 && wrongWayFlag)
|
||
{
|
||
// print direction out of bounds.
|
||
cout << "You cannot move that way. Try again." << endl;
|
||
}
|
||
|
||
}
|
||
// ask user for a direction, check if in bounds
|
||
cout << "Which direction would you like to go in?" << endl;
|
||
cout << ">";
|
||
cin.ignore();
|
||
directionCharacter = getchar();
|
||
currentIteration = currentIteration + 1;
|
||
// if in bounds, move. (if not in bounds, warn them and start over.)
|
||
}
|
||
cout << "Thanks for playing!" << endl;
|
||
|
||
// return 0
|
||
return 0;
|
||
}
|
||
|
||
// compiles using "g++ -Wall -pedantic maze.cpp -o maze" - note to programmer
|