Question: Files required LABYRINTH.JAVA import java.io.*; import java.util.*; import javax.swing.JFrame; import javax.swing.JPanel; /** * The Labyrinth class creates a window that shows a hexagon-tile based Labyrinth.
Files required
LABYRINTH.JAVA
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* The Labyrinth class creates a window that shows a hexagon-tile based Labyrinth.
*
* The Labyrinth is built from a file with the following specifications:
*
- The first line has the number of rows and cols
- Each subsequent line (there will be the same number of lines as rows)
*
*
*
*
* (Note: because this Labyrinth is based on hexagons, each alternating row is
* offset from the left side by half a hexagon, indicated by a space in the input file)
*
*
* @author CS1027
*
*/
public class Labyrinth extends JFrame {
private static final long serialVersionUID = 1L;
//Characters used by the input and output files
private static final char UNVISITED_CHAR = 'U';
private static final char TREASURE_CHAR = 'T';
private static final char END_CHAR = 'E';
private static final char START_CHAR = 'S';
private static final char WALL_CHAR = 'W';
private static final char END_PROCESSED_CHAR = 'G';
private static final char PROCESSED_CHAR = 'P';
private static final char PUSHED_CHAR = 'H';
private static final String MY_TITLE = "Labyrinth";
//Default time delay when repainting the Labyrinth to reflect hexagon changes
public static final int DEFAULT_TIME_DELAY = 1;
private int timeDelay = DEFAULT_TIME_DELAY;
protected Hexagon start;
private Hexagon[][] hexLabyrinth;
/**
* Constructor to build a Graphical Labyrinth with hexagonal tiles
* from a file containing a Labyrinth specification
* @param inFile
* @throws UnknownLabyrinthCharacterException
* @throws FileNotFoundException
* @throws IOException
*/
public Labyrinth(String inFile) throws UnknownLabyrinthCharacterException, FileNotFoundException, IOException{
// set up GUI aspects of the Labyrinth component
super("Labyrinth");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
// set up the file reader and read the first line
BufferedReader in;
String line="";
in = new BufferedReader(new FileReader(inFile));
line = in.readLine();
// Tokenize the first line to get the row and column
StringTokenizer lineTokens = new StringTokenizer(line);
// First line is the number of rows then the number of columns
int row = Integer.parseInt(lineTokens.nextToken());
int col = Integer.parseInt(lineTokens.nextToken());
// The hexagons form a linked structure, and we don't
// use the 2D array for anything after building the labyrinth
// .....except to output the result to a file
hexLabyrinth = new Hexagon[row+2][col+2];
// HexLayout will arrange the Hexagons in the window
p.setLayout(new HexLayout(row, col, 4));
// for each row
for (int r = 1; r line = in.readLine(); lineTokens = new StringTokenizer(line); // for each token on the line (col in the Labyrinth) for(int c = 1; c< col+1; c++){ // read the token and generate the hexagon type char token = lineTokens.nextToken().charAt(0); switch(token){ case WALL_CHAR: hexLabyrinth[r][c] = new Hexagon(Hexagon.HexType.WALL); break; case START_CHAR: hexLabyrinth[r][c] = new Hexagon(Hexagon.HexType.START); this.start = hexLabyrinth[r][c]; break; case END_CHAR: hexLabyrinth[r][c] = new Hexagon(Hexagon.HexType.END); break; case TREASURE_CHAR: hexLabyrinth[r][c] = new Hexagon(Hexagon.HexType.TREASURE); break; case UNVISITED_CHAR: hexLabyrinth[r][c] = new Hexagon(Hexagon.HexType.UNVISITED); break; default: // cannot build correct Labyrinth throw new UnknownLabyrinthCharacterException(token); } // add to the GUI layout p.add(hexLabyrinth[r][c]); }// end for cols }// end for rows //go through the 2D matrix again and build the neighbors int offset = 0; for(int r=1;r for(int c=1;c // on even rows(inset from left side) need to add one to the upper and lower neighbors // on odd, do not add anything (offset should be 0) offset = 1 - r%2; // set the neighbors for this hexagon in the builder hexLabyrinth[r][c].setNeighbour(hexLabyrinth[r-1][c+offset], 0); hexLabyrinth[r][c].setNeighbour(hexLabyrinth[r][c+1], 1); hexLabyrinth[r][c].setNeighbour(hexLabyrinth[r+1][c+offset], 2); hexLabyrinth[r][c].setNeighbour(hexLabyrinth[r+1][c-1+offset], 3); hexLabyrinth[r][c].setNeighbour(hexLabyrinth[r][c-1], 4); hexLabyrinth[r][c].setNeighbour(hexLabyrinth[r-1][c-1+offset], 5); } // end for cols } // end for rows //close the file in.close(); //set up the GUI window this.add(p); this.pack(); this.setSize(40*row,40*col ); this.setVisible(true); } /** * Method will return a reference to the hexagon that is the * start of the Labyrinth. * @return A reference to the hexagon that is the start of the Labyrinth */ public Hexagon getStart() { return this.start; } /** * Get the current time delayed used when repainting the Labyrinth to reflect changes * made to the hexagon tiles * @return the timeDelay */ public int getTimeDelay() { return timeDelay; } /** * Set the amount of time to wait when repainting the Labyrinth to reflect changes * made to the hexagon tiles * @param timeDelay the timeDelay to set */ public void setTimeDelay(int timeDelay) { this.timeDelay = timeDelay; } @Override public void repaint() { try { Thread.sleep(this.timeDelay); }catch(Exception e){ System.err.println("Error while issuing time delay " + e.getMessage()); } super.repaint(); } /** * Goes through the current state of the labyrinth, and prints the results to * a file specified in the parameter * * @param outFile The name of the file to save the labyrinth to * @throws IOException */ public void saveLabyrith(String outFile) throws FileNotFoundException, IOException { // set up the file writer and read the first line BufferedWriter out; String line=""; out = new BufferedWriter(new FileWriter(outFile)); // for each row for (int r = 1; r line = ""; //on even row we inset by a space if(r%2 == 0) line +=" "; // for each col for(int c = 1; c< this.hexLabyrinth[r].length; c++){ if (this.hexLabyrinth[r][c] !=null) { switch(this.hexLabyrinth[r][c].getHexagonType()) { case UNVISITED: line += UNVISITED_CHAR; break; case TREASURE: line += TREASURE_CHAR; break; case END: line += END_CHAR; break; case START: line += START_CHAR; break; case WALL: line += WALL_CHAR; break; case PUSHED: line += PUSHED_CHAR; break; case PROCESSED: line += PROCESSED_CHAR; break; case END_PROCESSED: line += END_PROCESSED_CHAR; break; default: line += '?'; } line += ' '; } } out.write(line + " "); } out.close(); } HEXAGON.JAVA import java.awt.Color; /** * This class represents a pointed-top Hexagon tile used to make up a * * Each tile has a type. It can be a Wall, Start, End, Unvisited, Visited or Pushed tile. * Each tile type will be a different colour: black, green, red, cyan, blue and magenta respectively. * * Hexagon tiles know about their neighbors (if set using setNeighbor method). * * The neighbors of a tile are accessed by an index 0-5 inclusive. * * * Maze object.
*
* Eg.
* 5 / \ 0
* 4 | | 1
* 3 \ / 2
* @author CS1027
*
*/
public class Hexagon extends HexComponent
{
// constants
private static final Color WALL_COLOR = Color.BLACK;
private static final Color START_COLOR = Color.GREEN;
private static final Color END_COLOR = Color.RED;
private static final Color UNVISITED_COLOR = Color.CYAN;
private static final Color PROCESSED_COLOR = Color.BLUE;
private static final Color PUSHED_COLOR = Color.MAGENTA;
private static final Color END_PROCESSED_COLOR = Color.ORANGE;
private static final Color TREASURE_COLOR = Color.YELLOW;
//enum to represent available hexagon types
public static enum HexType{WALL, START, END, UNVISITED, PROCESSED, PUSHED, END_PROCESSED, TREASURE};
// Attributes
private HexType type; // Stores the type of Hexagon this currently is
private boolean isStart; // Is this the start?
private boolean isEnd; // Is this the end?
private Hexagon[] neighbors; // Stores the hexagons which surround this one on each of 6 sides
/**
* Create a Hexagon tile of the specified type
* @param t the HexType to create
*/
public Hexagon(HexType t){
this.type = t;
this.isStart = t==HexType.START;
this.isEnd = t==HexType.END;
//set the initial color based on the initial type
this.setColor(this.type);
//allocate space for the neighbor array
this.neighbors = new Hexagon[6];
}
/**
* Set the neighbor for this hexagon using the neighbor index.
*
* The index for the neighbor indicates which side of the hexagon
* this new neighbor is on. 0-5 inclusive.
*
* @param neighbor The new Hexagon neighbor
* @param i The index specifying which side this neighbor is on (0-5 inclusive)
* @throws InvalidNeighborIndexException When an index is specified that is not 0-5 inclusive.
*/
public void setNeighbour(Hexagon neighbor, int i) throws InvalidNeighbourIndexException{
if (0<=i && i <=5)
this.neighbors[i] = neighbor;
else
throw new InvalidNeighbourIndexException(i);
}
/**
* Returns the neighbor for this hexagon using the neighbor index
*
* The index for the neighbor indicates which side of the hexagon
* the neighbor to get is on. 0-5 inclusive.
*
* @param i The index indicating the side of the hexagon this neighbor is on
* @return The hexagon the is on the i-th side of the current hexagon, or null if no neighbor
* @throws InvalidNeighbourIndexException When an index is specified that is not 0-5 inclusive.
*/
public Hexagon getNeighbour(int i) throws InvalidNeighbourIndexException{
if (0<=i && i <=5)
return this.neighbors[i];
else
throw new InvalidNeighbourIndexException(i);
}
/**
* This method checks if the current hexagon is a Wall tile.
* @return true if this is a Wall tile, false otherwise.
*/
public boolean isWall(){
return type == HexType.WALL;
}
/**
* This method checks if the current hexagon is a Visited tile.
* @return true if this is a Visited tile, false otherwise.
*/
public boolean isProcessed(){
return type == HexType.PROCESSED;
}
/**
* This method checks if the current hexagon is an Unvisited tile.
* @return true if this is an Unvisited tile, false otherwise.
*/
public boolean isUnvisited(){
return type == HexType.UNVISITED || (this.isEnd() && type==HexType.END);
}
/**
* This method checks if the current hexagon is a Start tile.
* @return true if this is a Start tile, false otherwise.
*/
public boolean isStart(){
return this.isStart;
}
/**
* This method checks if the current hexagon is an End tile.
* @return true if this is an End tile, false otherwise.
*/
public boolean isEnd(){
return this.isEnd;
}
/**
* This method gets the hexagon type and returns a HexType
* value, which can be checked against the enum above
* @return HexType value - see the enum
*/
public HexType getHexagonType() {
return this.type;
}
/**
* This method sets the tile to be a Pushed tile
* and updates the tile's colour
*/
public void setPushed(){
this.type = HexType.PUSHED;
this.setColor(this.type);
}
/**
* This method set the tile to be a Visited tile
* and updates the tile's colour
*/
public void setProcessed(){
this.type = HexType.PROCESSED;
if (isEnd){
this.type = HexType.END_PROCESSED;
}
this.setColor(this.type);
}
/**
* Helper method to set the current tile color based on the
* type of tile.
* @param t The type to use to set the color
*/
private void setColor(HexType t){
switch(t){
case WALL:
this.setBackground(WALL_COLOR);
break;
case START:
this.setBackground(START_COLOR);
break;
case END:
this.setBackground(END_COLOR);
break;
case UNVISITED:
this.setBackground(UNVISITED_COLOR);
break;
case PROCESSED:
this.setBackground(PROCESSED_COLOR);
break;
case PUSHED:
this.setBackground(PUSHED_COLOR);
break;
case END_PROCESSED:
this.setBackground(END_PROCESSED_COLOR);
break;
case TREASURE:
this.setBackground(TREASURE_COLOR);
break;
default:
this.setBackground(WALL_COLOR);
break;
}
this.setForeground(Color.black);
}
}
You will create a class SearchForTreasure that has a main method only, which will search a labyrinth and look for all the treasure Exits are not important to us anymore! The algorithm will likely be similar to SearchForExit.java, but you will use a LinkedStack for your stack of tiles, instead of an ArrayStack. At the end, you must output (substitute in actual values for numTiles and numTreasure): Number of tiles in labyrinth: numTiles Amount of treasure found: numTreasure Where the number of tiles is the number of open tiles (non walls) in the labyrinth that you searched. The labyrith4.txt file contains treasure
labyrinth4.txt
15 15 W W W W W W W W W W W W W W W W U W W U W W W W W U W W W W W T W W U W W U W W U W W T W W U W W U W W U W W U W U W W W U W U U W W U W W U W U W W W U W U W W W U W U W U W W W W U W U T U W U W W U U W W W W U W U U W U U W W U W W U W W U W W U W U S U W W U W U W W U W U U W U U U U U U U W W W U W U W U W U U W U W U U W W U W U W U U W U W U W W U W W U W T U W U W U W W U U W W W U W W W U W U W W W W U W W W U U U U U W W U U U W W W W W W W W W W W W W W W W W W W
LINKEDSTACK.java
/**
* @author Lewis and Chase
*
* Represents a linked implementation of a stack.
*/
public class LinkedStack implements StackADT
{
/** indicates number of elements stored */
private int count;
/** pointer to top of stack */
private LinearNode top;
/**
* Creates an empty stack.
*/
public LinkedStack()
{
count = 0;
top = null;
}
/**
* Adds the specified element to the top of this stack.
* @param element element to be pushed on stack
*/
public void push (T element)
{
LinearNode temp = new LinearNode (element);
temp.setNext(top);
top = temp;
count++;
}
/**
* Removes the element at the top of this stack and returns a
* reference to it. Throws an EmptyCollectionException if the stack
* is empty.
* @return T element from top of stack
* @throws EmptyCollectionException on pop from empty stack
*/
public T pop(){
if (isEmpty())
throw new EmptyCollectionException("Stack");
T result = top.getElement();
top = top.getNext();
count--;
return result;
}
/**
* Returns a reference to the element at the top of this stack.
* The element is not removed from the stack. Throws an
* EmptyCollectionException if the stack is empty.
* @return T element on top of stack
* @throws EmptyCollectionException on peek at empty stack
*/
public T peek() {
if (isEmpty())
throw new EmptyCollectionException("Stack");
return top.getElement();
}
/**
* Returns true if this stack is empty and false otherwise.
* @return boolean true if stack is empty
*/
public boolean isEmpty()
{
//return count == 0;
return top==null;
}
/**
* Returns the number of elements in this stack.
* @return int number of elements in this stack
*/
public int size()
{
int size=0;
//return count;
LinearNode current = top;
while(current!=null) {
size++;
current = current.getNext();
}
return size;
}
/**
* Returns a string representation of this stack.
* @return String representation of this stack
*/
public String toString()
{
return "";
}
}
the searching can be done in any method, the java files above may be helpful
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
