Java program that implements a maze using references to instances of a Node class
The following figure is called a graph. The circles are called nodes and the lines are
called edges. An edge connects two nodes. You can interpret the graph as a maze of
rooms and passages. The nodes can be thought of as rooms, and an edge connects one
room to another. Note that each node has at most four edges in the graph that follows.
Write a java program that implements the previous maze using references to instances
of a Node class. Each node in the graph will correspond to an instance of Node.
The edges correspond to links that connect one node to another and can be
represented in Node as instance variables that reference another Node class. Start
the user in node A. The user’s goal is to reach the finish in node L. The program
should output possible moves in the north, south, east, or west direction. Sample
execution is shown next.
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
Answer:
/** Maze game implemented with references to Node classes. */ import java.util.Scanner; class Node { char ID; Node north, south, east, west; public Node() { } public Node(char newID) { ID = newID; } public char getID() { return ID; } public Node getNorth() { return north; } public Node getSouth() { return south; } public Node getEast() { return east; } public Node getWest() { return west; } public void setConnections(Node north, Node south, Node east, Node west) { this.north = north; this.south = south; this.east = east; this.west = west; } } public class Question { public static void main(String[] args) { 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; while (current != L) { System.out.println("You are in room " + current.getID() + " of a maze of twisty " + "passages, all alike."); System.out.println("You can go: "); if (current.getNorth() != null) System.out.println(" North"); if (current.getSouth() != null) System.out.println(" South"); if (current.getEast() != null) System.out.println(" East"); if (current.getWest() != null) System.out.println(" West"); Scanner kbd = new Scanner(System.in); String s; s = kbd.next(); if ((s.charAt(0) == 'N') && (current.getNorth() != null)) current = current.getNorth(); else if ((s.charAt(0) == 'S') && (current.getSouth() != null)) current = current.getSouth(); else if ((s.charAt(0) == 'E') && (current.getEast() != null)) current = current.getEast(); else if ((s.charAt(0) == 'W') && (current.getWest() != null)) current = current.getWest(); } System.out.println("You reached the exit of the maze!"); } }
Leave a reply