Question: When working with labyrinths and hexagons, there are two situations that come up. 1. UnknownLabyrinthCharacterException a. As you can see in the Labyrinth file, the

When working with labyrinths and hexagons, there are two situations that come up.

1. UnknownLabyrinthCharacterException a. As you can see in the Labyrinth file, the constructor may throw this exception while reading the file describing the labyrinth.

2. InvalidNeighbourIndexException a. This is to cover the case where an entity requests a neighbour that is not 0-5 inclusive. Y

our task is to create these two exceptions as .java files. Take a look at the EmptyCollectionException to get an idea of what this would look like.

*****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

****Hexagon.java****

import java.awt.Color; /** * This class represents a pointed-top Hexagon tile used to make up a Maze object. * 

* 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. *

    *
  • The hexagons are pointed-top in orientation, the 0 index is the upper-right side
  • *
  • Indexes for the sides progress incrementally clockwise from the 0 index, to 5 on the upper-left side *
* 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); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!