CursorCntl.h code:
#ifndef CURSORCNTL_H
#define CURSORCNTL_H
#ifndef _WIN32
#define NoGraphics
#endif
#ifdef NoGraphics
void Sleep(unsigned ms);
#endif
void getxy(int &x, int &y);
void gotoxy(int x, int y);
void clrscr();
void clreol();
void SaveXY();
void RestoreXY();
#endif
CursorCntl.cpp code:
#include <iostream>
using namespace std;
#include "CursorCntl.h"
#if defined NoGraphics
#include <ctime>
void Sleep(unsigned ms)
{
#ifdef __LINUX__
#include <unistd.h>
usleep(1000 * ms);
#else
const unsigned MsPerSec = 1000;
clock_t clocksPerMs = CLOCKS_PER_SEC / MsPerSec;
if (clocksPerMs < 1)
clocksPerMs = 1;
clock_t tDone = clock() + ms * clocksPerMs;
while (clock() < tDone)
;
#endif
}
void getxy(int &x, int &y)
{
x = 0;
y = 0;
}
void gotoxy(int x, int y)
{
}
void clrscr(void)
{
}
void clreol(void)
{
}
void SaveXY(void)
{
}
void RestoreXY(void)
{
}
#else
#include <windows.h>
enum COLORS
{
BLACK,
BLUE,
GREEN,
CYAN,
RED,
MAGENTA,
BROWN,
LIGHTGRAY,
DARKGRAY,
LIGHTBLUE,
LIGHTGREEN,
LIGHTCYAN,
LIGHTRED,
LIGHTMAGENTA,
YELLOW,
WHITE
};
static int cursorSaveX;
static int cursorSaveY;
static int BACKGROUND = BLACK;
static int FOREGROUND = LIGHTGRAY;
void getxy(int &x, int &y)
{
CONSOLE_SCREEN_BUFFER_INFO screenBfr;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &screenBfr);
x = screenBfr.dwCursorPosition.X;
y = screenBfr.dwCursorPosition.Y;
}
void gotoxy(int x, int y)
{
COORD point;
point.X = (SHORT)x;
point.Y = (SHORT)y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
}
void clrscr()
{
system("CLS");
gotoxy(0, 0);
}
void clreol()
{
COORD start;
DWORD written;
CONSOLE_SCREEN_BUFFER_INFO screenBfr;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &screenBfr);
int x;
int y;
getxy(x, y);
start.X = (SHORT)x;
start.Y = (SHORT)y;
int numChars = screenBfr.dwSize.X - start.X;
FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND + (BACKGROUND << 4),
numChars,
start,
&written);
FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),
' ',
numChars,
start,
&written);
}
void SaveXY()
{
getxy(cursorSaveX, cursorSaveY);
}
void RestoreXY()
{
gotoxy(cursorSaveX, cursorSaveY);
}
#endif
Maze.h code :
#ifndef POSITION_H
#define POSITION_H
#ifndef MAZE_H
#define MAZE_H
#include <fstream>
#include <string>
using namespace std;
#include "Position.h"
const unsigned GridSize = 10;
typedef char CellState;
const char Open = ' ';
const char Obstacle = '-';
const char Visited = 'V';
const char Rejected = 'R';
const char StartCell = 'S';
const char GoalCell = 'G';
const char PathCell = 'P';
class Maze
{
public:
Maze();
bool IsOpen(const Position &cellPos) const;
bool IsVisited(const Position &cellPos) const;
Position Start() { return start; }
Position Goal() { return goal; }
void Visit(const Position &p);
void Reject(const Position &p);
void MarkPathCell(const Position &p);
private:
CellState cell[GridSize][GridSize];
Position start;
Position goal;
string mazeFileName;
ofstream logFile;
int speed;
void Show() const;
void ShowCell(const Position &p, const CellState state) const;
void OpenMazeFile(string &mazeFileName, ifstream &mazeFile);
void StoreCell(char c, int rowNum, int colNum);
void LoadMazeFile();
void SetSpeed();
};
#endif
Position.h code:
#include <iostream>
#include <string>
class Position
{
public:
Position() { defined = false; }
Position(const int theRow, const int theCol) : row(theRow), col(theCol), defined(true) { }
int Row() const { return row; }
int Col() const { return col; }
bool Defined() { return defined; }
Position operator+(const Position &b) const;
Position operator+=(const Position &b);
bool operator!=(const Position &b) const { return (row != b.row) || (col != b.col); }
bool operator==(const Position &b) const { return (row == b.row) && (col == b.col); }
private:
int row;
int col;
bool defined;
};
const Position StepEast = Position(0, +1);
const Position StepSouth = Position(+1, 0);
const Position StepWest = Position(0, -1);
const Position StepNorth = Position(-1, 0);
#endif
position.cpp code:
#include <iostream>
#include <cassert>
using namespace std;
#include "Position.h"
Position Position::operator+(const Position &b) const
{
Position result;
result.row = row + b.row;
result.col = col + b.col;
result.defined = true;
return result;
}
Position Position::operator+=(const Position &b)
{
row = row + b.row;
col = col + b.col;
defined = true;
return *this;
}
stack.h code:
#ifndef STACK_H
#define STACK_H
#include <cassert>
using namespace std;
#include "Position.h"
const int StackCapacity = 100;
typedef Position StackElement;
class Stack
{
struct Node
{
StackElement data;
Node *next;
Node(){}
Node(const StackElement &theData, Node *const theNext = 0): data(theData), next(theNext) {}
};
public:
Stack() { tos = -1; }
bool Empty() const { return tos < 0; }
bool Full() const { return tos >= StackCapacity - 1; }
void Push(const StackElement &elem);
StackElement Pop();
StackElement Top() const;
private:
int tos;
Node *top;
StackElement stack[StackCapacity];
};
#endif
stack.cpp code:
#include "stack.h"
#include"Maze.h"
void Stack::Push(const StackElement &elem)
{
assert(!Full());
stack[++tos] = elem;
}
StackElement Stack::Pop()
{
assert(!Empty());
tos = Rejected;
return stack[tos--];
}
main program code:
#include <stdlib.h>
#include <iostream>
using namespace std;
#include "CursorCntl.h"
#include "Maze.h"
#include "Stack.h"
bool Solve(Maze &maze, Stack &stack)
{
Position curPos;
curPos = maze.Start();
maze.Visit(curPos);
while (curPos != maze.Goal())
{
if (maze.IsOpen(curPos + StepEast))
curPos += StepEast;
else if (maze.IsOpen(curPos + StepSouth))
curPos = curPos + StepSouth;
else if (maze.IsOpen(curPos + StepWest))
curPos = curPos + StepWest;
else if (maze.IsOpen(curPos + StepNorth))
curPos = curPos + StepNorth;
else
return false;
maze.Visit(curPos);
}
return true;
}
void RetracePath(Maze &maze, Stack &stack)
{
Stack object;
object.Pop();
}
int main(void)
{
const unsigned XResult = 15;
const unsigned YResult = 5;
const unsigned XFinish = 0;
const unsigned YFinish = 20;
Stack posStack;
Maze maze;
bool success = Solve(maze, posStack);
gotoxy(XResult, YResult);
if (!success)
cout << "Failed: No path from start to goal exists." << endl;
else
{
cout << "Success: Found a path. Press <enter> to retrace." << endl;
cin.get();
RetracePath(maze, posStack);
}
gotoxy(XFinish, YFinish);
return 0;
}
maze.cpp code:
#include <limits.h>#include <cassert>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <ctime>
using namespace std;
#include "CursorCntl.h"
#include "Maze.h"
#include "Stack.h"
#if (defined _WIN32) && (!defined NoGraphics)
#include <windows.h>
#endif
const unsigned DefaultSpeed = 6;
const unsigned MsPerSec = 1000;
void Maze::OpenMazeFile(string &fileName, ifstream &mazeFile)
{
const char DefFileName[] = "maze";
const string ext = ".txt";
for (;;)
{
cout << "Maze file name [default = \"" << DefFileName << "\", ctrl-C quits]: ";
if (cin.peek() == '\n')
{
cin.ignore(INT_MAX, '\n');
fileName = DefFileName;
}
else
getline(cin, fileName);
if (fileName.length() >= ext.length())
{
if (fileName.substr(fileName.length() - ext.length()) != ext)
fileName += ext;
}
else
fileName += ext;
mazeFile.open(fileName.c_str());
if (mazeFile.is_open())
{
const string LogFileExt = ".log";
string logFileName = mazeFileName;
logFileName.erase(logFileName.length() - ext.length(), ext.length());
logFileName += LogFileExt;
logFile.open(logFileName.c_str());
assert(logFile.is_open());
break;
}
mazeFile.clear();
cout << "*** ERROR: No such file: " << fileName << endl;
}
}
void Maze::StoreCell(char cellStateChar, int rowNum, int colNum)
{
switch (toupper(cellStateChar))
{
case '0':
case ' ':
case 'O':
cell[rowNum][colNum] = Open;
break;
case StartCell:
if (start.Defined())
{
cout << "*** ERROR: More than one start position specified." << endl;
exit(EXIT_FAILURE);
}
cell[rowNum][colNum] = StartCell;
start = Position(rowNum, colNum);
break;
case GoalCell:
if (goal.Defined())
{
cout << "*** ERROR: More than one goal position specified." << endl;
exit(EXIT_FAILURE);
}
cell[rowNum][colNum] = GoalCell;
goal = Position(rowNum, colNum);
break;
default:
cell[rowNum][colNum] = Obstacle;
break;
}
}
void Maze::LoadMazeFile()
{
ifstream mazeFile;
OpenMazeFile(mazeFileName, mazeFile);
for (int rowNum = 0; rowNum < GridSize; rowNum++)
{
for (int colNum = 0; colNum < GridSize; colNum++)
{
char cellStateChar;
mazeFile.get(cellStateChar);
if (mazeFile.eof())
{
cout << "*** ERROR: Unexpected end of file on " << mazeFileName << endl;
exit(EXIT_FAILURE);
}
StoreCell(cellStateChar, rowNum, colNum);
}
mazeFile.ignore(INT_MAX, '\n');
}
mazeFile.close();
if (!start.Defined())
{
cout << "*** ERROR: No start positon specified." << endl;
exit(EXIT_FAILURE);
}
if (!goal.Defined())
{
cout << "*** ERROR: No goal positon specified." << endl;
exit(EXIT_FAILURE);
}
}
void Maze::SetSpeed()
{
const unsigned MinSpeed = 1;
const unsigned DefSpeed = 6;
const unsigned XPrompt = 0;
const unsigned YPrompt = 15;
bool needSpeed;
do
{
needSpeed = true;
gotoxy(XPrompt, YPrompt);
clreol();
cout << "Speed [minimum = " << MinSpeed << ", default = " << DefSpeed << ", ctrl-C quits]: ";
if (cin.peek() == '\n')
{
speed = DefaultSpeed;
needSpeed = false;
}
else
{
cin >> speed;
if (cin.fail())
{
clreol();
cout << "***ERROR: Speed must be a positive integer." << endl;
cin.clear();
}
else if (speed < MinSpeed)
{
clreol();
cout << "***ERROR: Speed must be at least " << MinSpeed << "." << endl;
}
else
needSpeed = false;
}
cin.ignore(INT_MAX, '\n');
} while (needSpeed);
gotoxy(XPrompt, YPrompt + 1);
clreol();
}
Maze::Maze()
{
LoadMazeFile();
Show();
SetSpeed();
}
void Maze::Show(void) const
{
const char ColHeadings[] = " 0123456789";
clrscr();
cout << ColHeadings << " " << mazeFileName << endl;
for (int rowNum = 0; rowNum < GridSize; rowNum++)
{
cout << rowNum;
for (int colNum = 0; colNum < GridSize; colNum++)
cout << cell[rowNum][colNum];
cout << rowNum << endl;
}
cout << ColHeadings << endl;
}
bool Maze::IsOpen(const Position &cellPos) const
{
if (cellPos.Row() < 0 || cellPos.Row() >= GridSize)
return false;
if (cellPos.Col() < 0 || cellPos.Col() >= GridSize)
return false;
if (cell[cellPos.Row()][cellPos.Col()] == StartCell)
return true;
if (cell[cellPos.Row()][cellPos.Col()] == GoalCell)
return true;
return cell[cellPos.Row()][cellPos.Col()] == Open;
}
bool Maze::IsVisited(const Position &cellPos) const
{
if (cellPos.Row() < 0 || cellPos.Row() >= GridSize)
return false;
if (cellPos.Col() < 0 || cellPos.Col() >= GridSize)
return false;
return cell[cellPos.Row()][cellPos.Col()] == Visited;
}
void Maze::ShowCell(const Position &p, const CellState state) const
{
const char CurPosChar = '+';
const unsigned curPosX = 15;
const unsigned curPosY = 2;
gotoxy(curPosX, curPosY);
cout << "Position: (" << p.Col() << ", " << p.Row() << ")";
gotoxy(p.Col() + 1, p.Row() + 1);
cout << state;
}
void Maze::Visit(const Position &p)
{
logFile << "Visit (" << p.Row() << ", " << p.Col() << ")" << endl;
cell[p.Row()][p.Col()] = Visited;
#if (defined NoGraphics)
Show();
#else
ShowCell(p, Visited);
#endif
Sleep(MsPerSec / speed);
}
void Maze::Reject(const Position &p)
{
logFile << "Reject (" << p.Row() << ", " << p.Col() << ")" << endl;
cell[p.Row()][p.Col()] = Rejected;
#if (defined NoGraphics)
Show();
#else
ShowCell(p, Rejected);
#endif
Sleep(MsPerSec / speed);
}
void Maze::MarkPathCell(const Position &p)
{
logFile << "Retrace (" << p.Row() << ", " << p.Col() << ")" << endl;
cell[p.Row()][p.Col()] = PathCell;
#if (defined NoGraphics)
Show();
#else
ShowCell(p, PathCell);
#endif
Sleep(MsPerSec / speed);
}
What I have tried:
I tried to modify these codes (stack .h and stack.cpp and prog4.cpp )
questions can be found here:
Stack.h , Stack.cpp And Prog4.cpp Need To Be Modif... | Chegg.com[
^]