Write an application which implements this graph maze using reference to a Node
ID: 3588840 • Letter: W
Question
Write an application which implements this graph maze using reference to a Node class. Each node in the graph will correspond to an instance of the Node class. The edges correspond to the links that connect one node to another and can be represented in Node as an instance variable which references another Node class. Start the user in node A. The user’s goal is to finish in node L. The program should output possible moves in the cardinal directions.
Include a main method in a class called Maze which can be used to test this maze. Sample output can be shown as follows:
You are in room A of a maze of twisty little passages, all alike. You can go east or south.
E
You are in room B of a maze of twisty little passages, all alike. You can go west or south.
S
You are in room F of a maze of twisty little passages, all alike. You can go north or east.
E
Save your submissions in Maze.java and Node.java (Use Java programming language )
Consider the following graph with edges and vertices defined as follows: North Start LFinishExplanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Node
{
public:
Node() { north = south = east = west = NULL; visited = false; }
Node(char newID) { north = south = east = west = NULL; visited = false; ID = newID; }
char getID() { return ID; }
void setConnections(Node *n, Node *s, Node *e, Node *w)
{
north = n; south = s; west = w; east = e;
}
Node* getNorth() { return north; }
Node* getSouth() { return south; }
Node* getEast() { return east; }
Node* getWest() { return west; }
void setVisited()
{
visited = true;
}
bool getVisited() { return visited; }
private:
char ID;
bool visited;
Node *north, *south, *east, *west;
};
int main()
{
Node *A = new Node('A');
Node *B = new Node('B');
Node *C = new Node('C');
Node *D = new Node('D');
Node *E = new Node('E');
Node *F = new Node('F');
Node *G = new Node('G');
Node *H = new Node('H');
Node *I = new Node('I');
Node *J = new Node('J');
Node *K = new Node('K');
Node *L = new Node('L');
A->setConnections(NULL, E, B, NULL);
B->setConnections(NULL, F, NULL, A);
C->setConnections(NULL, G, D, NULL);
D->setConnections(NULL, NULL, NULL, C);
E->setConnections(A, I, NULL, NULL);
F->setConnections(B, NULL, G, NULL);
G->setConnections(C, K, H, F);
H->setConnections(NULL, L, NULL, G);
I->setConnections(E, NULL, J, NULL);
J->setConnections(NULL, NULL, NULL, I);
K->setConnections(G, NULL, NULL, NULL);
L->setConnections(H, NULL, NULL, NULL);
Node*current = A;
cout << "You are in room " << current->getID() << " of a maze of twisty little passages, all like. You can go: " << endl;
if (current->getNorth())
cout << "north (n) | ";
if (current->getSouth())
cout << "south (s) | ";
if (current->getEast())
cout << "east (e) | ";
if (current->getWest())
cout << "west (w) | ";
string go;
cin >> go;
if (go == "n")
current = current->getNorth();
else if (go == "s")
current = current->getSouth();
else if (go == "e")
current = current->getEast();
else
current = current->getWest();
while (current->getID() != 'L')
{
cout << "You are in room " << current->getID() << " of a maze of twisty little passages, all like. You can go: " << endl;
if (current->getNorth()){
cout << "north (n) | ";
current = current->getNorth();
}
if (current->getSouth()){
cout << "south (s) | ";
current = current->getSouth();
}
if (current->getEast()){
cout << "east (e) | ";
current = current->getEast();
}
if (current->getWest()){
cout << "west (w) | ";
current = current->getWest();
}
cin >> go;
}
if (current->getID() == 'L')
cout << "Congratulations! You made it.";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.