Question: A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in the maze and is asked to

A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in the maze and is asked to try to reach another position (the goal position). Positions in the maze will either be open or blocked with an obstacle. Positions are identified by (x,y) coordinates.

At any given moment, the robot can only move 1 step in one of 4 directions. Valid moves are:

Go North: (x,y) -> (x,y-1)

Go East: (x,y) -> (x+1,y)

Go South: (x,y) -> (x,y+1)

Go West: (x,y) -> (x-1,y)

Note that positions are specified in zero-based coordinates (i.e., 0...size-1, where size is the size of the maze in the corresponding dimension).

The robot can only move to positions without obstacles and must stay within the maze.

Can anyone please rectify this code

MazeGame

import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Choice; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.Label; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener;

@SuppressWarnings("serial") public class MazeGame extends Applet implements ItemListener, ActionListener { private Button bRowsPlus; private Button bRowsMinus; private Label lRows; private Button bColsPlus; private Button bColsMinus; private Label lCols; private Button bGen; private Choice cTheme; private Label lRooms; private int cols = 3; private int rows = 3; private int rooms = 9; private String theme = "Default"; private Maze maze = null; public void init() { Color bgColor = Color.CYAN; Color fgColor = Color.BLACK; setLayout(new GridLayout(4, 1, 0, 4)); setFont(new Font("Helvetica", Font.BOLD, 12)); setBackground(bgColor); setForeground(fgColor); bRowsMinus = new Button("Reduce"); bRowsMinus.addActionListener(this); bRowsMinus.setBackground(Color.BLUE); bRowsMinus.setForeground(Color.WHITE); if(rows <= 3){ bRowsMinus.setEnabled(false); rows = 3; } bRowsPlus = new Button("Add"); bRowsPlus.addActionListener(this); bRowsPlus.setBackground(Color.BLUE); bRowsPlus.setForeground(Color.WHITE); Panel rowsBtnPanel = new Panel(); rowsBtnPanel.setLayout(new GridLayout(1, 2)); rowsBtnPanel.add(bRowsMinus); rowsBtnPanel.add(bRowsPlus); bColsMinus = new Button("Reduce"); bColsMinus.addActionListener(this); bColsMinus.setBackground(Color.BLUE); bColsMinus.setForeground(Color.WHITE); if(cols <= 3){ bColsMinus.setEnabled(false); cols = 3; } bColsPlus = new Button("Add"); bColsPlus.addActionListener(this); bColsPlus.setBackground(Color.BLUE); bColsPlus.setForeground(Color.WHITE); Panel colsBtnPanel = new Panel(); colsBtnPanel.setLayout(new GridLayout(1, 2)); colsBtnPanel.add(bColsMinus); colsBtnPanel.add(bColsPlus); lRows = new Label(Integer.toString(rows), Label.CENTER); lRows.setBackground(Color.YELLOW); Panel rowsPanel = new Panel(); rowsPanel.setLayout(new BorderLayout(2, 0)); rowsPanel.add("West", new Label("Rows:")); rowsPanel.add("Center", lRows); rowsPanel.add("East", rowsBtnPanel); add(rowsPanel); lCols = new Label(Integer.toString(cols), Label.CENTER); lCols.setBackground(Color.YELLOW); Panel colsPanel = new Panel(); colsPanel.setLayout(new BorderLayout(2, 0)); colsPanel.add("West", new Label("Cols: ")); colsPanel.add("Center", lCols); colsPanel.add("East", colsBtnPanel); add(colsPanel); lRooms = new Label(Integer.toString(rooms), Label.CENTER); lRooms.setBackground(Color.YELLOW); Panel roomsPanel = new Panel(); roomsPanel.setLayout(new BorderLayout(2, 0)); roomsPanel.add("West", new Label("Total No. of Rooms are:")); roomsPanel.add("Center", lRooms); // roomsPanel.add("East", roomsPanel); add(roomsPanel); bGen = new Button("Press Here to Create Maze"); bGen.setBackground(Color.BLUE); bGen.setForeground(Color.WHITE); bGen.addActionListener(this); add(bGen); } @Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if(source == bGen){ bGen.setEnabled(false); launch(); bGen.setEnabled(true); }else if(source == bRowsPlus){ rows++; //int columns = Integer.parseInt(lCols.getText()); //int rows = Integer.parseInt(lRows.getText()); rooms = rows*cols; lRooms.setText(Integer.toString(rooms)); if(rows == 20){ bRowsPlus.setEnabled(false); } if(!bRowsMinus.isEnabled()){ bRowsMinus.setEnabled(true); } lRows.setText(Integer.toString(rows)); }else if(source == bRowsMinus){ rows--; rooms = rows*cols; lRooms.setText(Integer.toString(rooms)); if(rows == 5){ bRowsMinus.setEnabled(false); } if(!bRowsPlus.isEnabled()){ bRowsPlus.setEnabled(true); } lRows.setText(Integer.toString(rows)); }else if(source == bColsPlus){ cols++; rooms = rows*cols; lRooms.setText(Integer.toString(rooms)); if(cols == 20){ bColsPlus.setEnabled(false); } if(!bColsMinus.isEnabled()){ bColsMinus.setEnabled(true); } lCols.setText(Integer.toString(cols)); }else if(source == bColsMinus){ cols--; rooms = rows*cols; lRooms.setText(Integer.toString(rooms)); if(cols == 5){ bColsMinus.setEnabled(false); } if(!bColsPlus.isEnabled()){ bColsPlus.setEnabled(true); } lCols.setText(Integer.toString(cols)); } }

@Override public void itemStateChanged(ItemEvent e) { String _theme = cTheme.getSelectedItem(); if(_theme != null){ theme = _theme.trim(); } } private void launch(){ if(maze != null){ maze.disposeFrame(); maze = null; } MazeFactory factory = null; if ("Harry".equals(theme)){ // factory = new HarryPotterMazeFactory(); }else if ("Snow".equals(theme)){ // factory = new SnowWhiteMazeFactory(); }else{ factory = new MazeFactory(); } maze = CustomMazeBuilder.makeMaze(cols, rows, factory); maze.showFrame("Maze"); } }

Maze

import java.applet.AudioClip; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import java.util.Stack; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.WindowConstants;

public class Maze implements Cloneable { protected static AudioClip applause = AudioUtility.getAudioClip("audio/untie.wav");

private static final int ROOM_SIZE = 40; private static final int WALL_THICKNESS = 6; private static final int MARGIN = 20; private static final boolean debug = false;

protected List rooms = new ArrayList(); protected Dimension dim = null; protected Point offset = null; protected Room curRoom = null; protected Room endRoom = null; protected Stack moves = new Stack(); protected JFrame frame = null; protected boolean initializing = true;

protected Component view;

public Component getView(){ return view; }

public Stack getMoves(){ return moves; }

static class MazeKeyListener extends KeyAdapter { Maze maze;

MazeKeyListener(Maze maze) { this.maze = maze; }

public void keyPressed(KeyEvent e) { System.out.println("Key pressed"); Command command = null; int code = e.getKeyCode(); switch (code) { case KeyEvent.VK_UP: System.out.println("Up key"); command = new MazeMoveCommand(maze, Direction.NORTH); walls.play(); break;

case KeyEvent.VK_DOWN: System.out.println("Down key"); command = new MazeMoveCommand(maze, Direction.SOUTH); walls.play(); break;

case KeyEvent.VK_LEFT: System.out.println("Left key"); command = new MazeMoveCommand(maze, Direction.WEST); walls.play(); break;

case KeyEvent.VK_RIGHT: System.out.println("Right key"); command = new MazeMoveCommand(maze, Direction.EAST); walls.play(); break;

case KeyEvent.VK_ENTER: System.out.println("Enter key"); command = new OpenDoorCommand(maze); dooraud.play(); break;

case (int)'U': case (int)'u' : System.out.println("Undo key"); maze.undoCommand(); break;

default: System.out.println("Key press ignored"); } if (command != null) { maze.doCommand(command); } } }

public void setStartEndRoom(int start, int end){ if(start > 0 && start <= rooms.size()){ Room room = findRoom(start); room.markAsStartRoom(); setCurrentRoom(room); }else{ Room room = findRoom(1); setCurrentRoom(room); room.markAsStartRoom(); } if(end > 0 && end <= rooms.size()){ Room room = findRoom(end); setEndRoom(room); room.markAsEndRoom(); }else{ Room room = findRoom(rooms.size()); setEndRoom(room); room.markAsEndRoom(); } initializing = false; }

public Object clone() throws CloneNotSupportedException { return super.clone(); }

public void addRoom(Room room) { if (room != null) { rooms.add(room); } }

public Room findRoom(int roomNumber) { for (int i = 0; i < rooms.size(); i++) { Room room = (Room) rooms.get(i); if (roomNumber == room.getRoomNumber()) { return room; } } return null; }

public void setCurrentRoom(Room room) { if (room != curRoom) { if (curRoom != null) { curRoom.setInRoom(false); } if (room != null) { room.setInRoom(true); curRoom = room; System.out.println(curRoom); } if (view != null) { view.repaint(); } if(!initializing && curRoom != null && curRoom.isEndRoom()){ if(applause != null) applause.loop(); JOptionPane.showMessageDialog(null, "You have successfully completed the maze !!!", "Awesome", JOptionPane.INFORMATION_MESSAGE); if(applause != null) applause.stop(); frame.dispose(); } } } public void setEndRoom(Room room) { endRoom = room; System.out.println(endRoom); room.setGoalRoom(true); } public Room getCurrentRoom() { return curRoom; } public void move(Direction direction) { if (curRoom != null) { MapSite side = curRoom.getSide(direction); if (side != null) { side.enter(this); } } } public void draw(Graphics g) { if (dim == null) { calculateDimension(); } int dx = MARGIN + -offset.x * ROOM_SIZE; int dy = MARGIN + -offset.y * ROOM_SIZE;

if (debug) { System.out.println("Maze.Draw(): offset=" + offset.x + ", " + offset.y); } // draw rooms first for (int i = 0; i < rooms.size(); i++) { Room room = (Room) rooms.get(i); if (room != null) { Point location = room.getLocation(); if (location != null) {

if (debug) { System.out.println("Maze.Draw(): Room " + room.getRoomNumber() + " location: " + location.x + ", " + location.y); } room.draw(g, dx + location.x * ROOM_SIZE, dy + location.y * ROOM_SIZE, ROOM_SIZE, ROOM_SIZE); } } } // draw walls and doors for (int i = 0; i < rooms.size(); i++) { Room room = (Room) rooms.get(i); if (room != null) { Point location = room.getLocation(); if (location != null) { for (Direction dir = Direction.first(); dir != null; dir = dir.next()) { MapSite side = room.getSide(dir); if (side != null) { if (dir == Direction.NORTH) { side.draw(g, dx + location.x * ROOM_SIZE - WALL_THICKNESS / 2, dy + location.y * ROOM_SIZE - WALL_THICKNESS / 2, ROOM_SIZE + WALL_THICKNESS, WALL_THICKNESS); } else if (dir == Direction.EAST) { side.draw(g, dx + location.x * ROOM_SIZE + ROOM_SIZE - WALL_THICKNESS / 2, dy + location.y * ROOM_SIZE - WALL_THICKNESS / 2, WALL_THICKNESS, ROOM_SIZE + WALL_THICKNESS); } else if (dir == Direction.SOUTH) { side.draw(g, dx + location.x * ROOM_SIZE - WALL_THICKNESS / 2, dy + location.y * ROOM_SIZE + ROOM_SIZE - WALL_THICKNESS / 2, ROOM_SIZE + WALL_THICKNESS, WALL_THICKNESS); } else { side.draw(g, dx + location.x * ROOM_SIZE - WALL_THICKNESS / 2, dy + location.y * ROOM_SIZE - WALL_THICKNESS / 2, WALL_THICKNESS, ROOM_SIZE + WALL_THICKNESS); } } } } } } }

public Dimension getDimension() { if (dim == null) { calculateDimension(); } return dim; }

protected void calculateDimension() { if (rooms.size() > 0) { int minX = 0, maxX = 0, minY = 0, maxY = 0; Room room = (Room) rooms.get(0); room.setLocation(new Point(0, 0)); boolean changed = true; while (changed && !isAllRoomsSet()) { changed = false; for (int i = 0; i < rooms.size(); i++) { room = (Room) rooms.get(i); Point location = room.getLocation(); if (location != null) { for (Direction dir = Direction.first(); dir != null; dir = dir.next()) { MapSite side = room.getSide(dir); if (side instanceof Door) { Door door = (Door) side; Room otherSide = door.otherSideFrom(room); if (otherSide != null && otherSide.getLocation() == null) { if (dir == Direction.NORTH) { otherSide.setLocation(new Point(location.x, location.y - 1)); minY = Math.min(minY, location.y - 1); } else if (dir == Direction.EAST) { otherSide.setLocation(new Point(location.x + 1, location.y)); maxX = Math.max(maxX, location.x + 1); } else if (dir == Direction.SOUTH) { otherSide.setLocation(new Point(location.x, location.y + 1)); maxY = Math.max(maxY, location.y + 1); } else { otherSide.setLocation(new Point(location.x - 1, location.y)); minX = Math.min(minX, location.x - 1); } changed = true; } } } } } } offset = new Point(minX, minY); dim = new Dimension(maxX - minX + 1, maxY - minY + 1); } else { offset = new Point(0, 0); dim = new Dimension(0, 0); } }

protected boolean isAllRoomsSet() { for (int i = 0; i < rooms.size(); i++) { Room room = (Room) rooms.get(i); if (room.getLocation() == null) { return false; } } return true; }

protected void setView(Component view) { this.view = view; }

protected void doCommand(Command command) { if (command != null){ moves.push(command); command.execute(); } }

protected void undoCommand() { if (!moves.empty()) { Object top = moves.peek(); // looking at the top element without popping it if (top instanceof UndoableCommand) { moves.pop(); UndoableCommand undoableCommand = (UndoableCommand) top; undoableCommand.undo(); } } }

public void disposeFrame(){ if(frame != null){ frame.dispose(); } }

public void showFrame(String frameTitle) { frame = new JFrame(frameTitle); frame.setContentPane(new Maze.MazePanel(this)); frame.pack(); Dimension frameDim = frame.getSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(screenSize.width/2 - frameDim.width/2, screenSize.height/2 - frameDim.height/2); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.setVisible(true); }

static class MazePanel extends JPanel { private static final long serialVersionUID = 6600808034263523518L; private Maze maze; private Dimension dim;

MazePanel(Maze maze) { this.maze = maze; if (maze != null) { maze.setView(this); Dimension d = maze.getDimension(); if (d != null) { dim = new Dimension(d.width * ROOM_SIZE + 2 * MARGIN, d.height * ROOM_SIZE + 2 * MARGIN); } addKeyListener(new MazeKeyListener(maze)); } }

public void paint(Graphics g) { Dimension d = getSize(); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); if (maze != null) { maze.draw(g); } requestFocus(); }

public boolean isFocusable() { return true; }

public Dimension getPreferredSize() { return dim; }

public Dimension getMinimumSize() { return dim; } }

public void setGameComplete(boolean b) { // TODO Auto-generated method stub }

public boolean isGameComplete() { // TODO Auto-generated method stub return false; }

public int getLastRoomNumber() { // TODO Auto-generated method stub return 0; }

protected static AudioClip dooraud = AudioUtility.getAudioClip("door.wav"); protected static AudioClip walls = AudioUtility.getAudioClip("walls.wav");

}

Room

import java.awt.*; import java.applet.AudioClip;

@SuppressWarnings("unused") public class Room implements MapSite {

public static final Color ROOM_COLOR = new Color(252, 251, 152); public static final Color PLAYER_COLOR = Color.red;

public Room(int roomNumber) { this.roomNumber = roomNumber; }

public Object clone() throws CloneNotSupportedException { Room room = (Room) super.clone(); room.sides = new MapSite[4]; for (int i = 0; i < 4; i++) { if (sides[i] != null) { room.sides[i] = (MapSite) sides[i].clone(); } } return room; } public MapSite getSide(Direction dir) { if (dir != null) { return sides[dir.getOrdinal()]; } return null; }

public void setSide(Direction dir, MapSite site) { if (dir != null) { sides[dir.getOrdinal()] = site; if (site instanceof Door) { Door door = (Door) site; if (dir == Direction.NORTH || dir == Direction.SOUTH) { door.setOrientation(Orientation.HORIZONTAL); } else { door.setOrientation(Orientation.VERTICAL); } } } } public void setRoomNumber(int roomNumber) { this.roomNumber = roomNumber; }

public int getRoomNumber() { return roomNumber; }

public Point getLocation() { return location; }

public void setLocation(Point location) { this.location = location; }

public void enter(Maze maze) { maze.setCurrentRoom(this); if(this.goalroom){ complete.play(); maze.setGameComplete(true); } else { if (maze.isGameComplete()){ complete.stop(); // doubletake.play(); } else gong.play(); } }

public boolean isInRoom() { return inroom; }

public void setInRoom(boolean inroom) { this.inroom = inroom; }

public void setGoalRoom (boolean goalroom){ this.goalroom = goalroom; }

public void draw(Graphics g, int x, int y, int w, int h) { g.setColor(ROOM_COLOR); g.fillRect(x, y, w, h); if (inroom) { g.setColor(PLAYER_COLOR); g.fillOval(x + w / 2 - 5, y + h / 2 - 5, 10, 10); } if (goalroom) { g.setColor(Color.magenta); g.setFont (new Font ("Maiandra ",Font.BOLD, 22)); //g.drawString("D",x,y); g.fillRect(x, y, w, h); } }

protected int roomNumber = 0; protected boolean inroom = false; protected boolean goalroom=false; protected MapSite[] sides = new MapSite[4]; protected Point location = null;

public boolean isEndRoom() { // TODO Auto-generated method stub return false; }

public void markAsStartRoom() { // TODO Auto-generated method stub }

public void markAsEndRoom() { // TODO Auto-generated method stub }

protected static AudioClip gong = AudioUtility.getAudioClip("enter_room.wav"); protected static AudioClip complete = AudioUtility.getAudioClip("applause.wav"); }

MazeGameBuilder

import java.awt.*; import javax.swing.*;

@SuppressWarnings("unused") public class MazeGameBuilder {

public static Maze createMaze(MazeBuilder builder) { builder.newMaze(); //Added below code to create all 20 rooms builder.buildRooms(20); //Added code below to handle maze of size 20 rooms builder.buildDoor(1, 2, Direction.WEST, true); builder.buildDoor(2, 3, Direction.WEST, false); builder.buildDoor(3, 4, Direction.WEST, false); builder.buildDoor(5, 6, Direction.WEST, true); builder.buildDoor(6, 7, Direction.WEST, true); builder.buildDoor(7, 8, Direction.WEST, true); builder.buildDoor(5, 9, Direction.NORTH, false); builder.buildDoor(6, 10, Direction.NORTH, true); builder.buildDoor(11, 15, Direction.NORTH, false); builder.buildDoor(15, 19, Direction.NORTH, false); builder.buildDoor(10, 11, Direction.WEST, true); builder.buildDoor(11, 12, Direction.WEST, true); builder.buildDoor(1, 5, Direction.NORTH, true); builder.buildDoor(13, 14, Direction.WEST, true); builder.buildDoor(14, 15, Direction.WEST, true); builder.buildDoor(15, 16, Direction.WEST, false); builder.buildDoor(10, 14, Direction.NORTH, true); builder.buildDoor(9, 13, Direction.NORTH, true); builder.buildDoor(3, 7, Direction.NORTH, true); builder.buildDoor(17, 18, Direction.WEST, true); builder.buildDoor(18, 19, Direction.WEST, true); builder.buildDoor(19, 20, Direction.WEST, true); //Set the Room which is to be the Goal Room builder.getMaze().findRoom(20).setGoalRoom(true); return builder.getMaze(); }

/*public static void main(String[] args) { Maze maze; MazeBuilder builder; MazeFactory factory = null;

if (args.length > 0) { if ("Harry".equals(args[0])) { factory = new HarryPotterMazeFactory(); } else if ("Snow".equals(args[0])) { factory = new SnowWhiteMazeFactory(); } else if ("Default".equals(args[0])) { factory = new MazeFactory(); } } if (factory != null) { builder = new FactoryMazeBuilder(factory); } else { builder = new SimpleMazeBuilder(); } maze = createMaze(builder); maze.setCurrentRoom(1);

JFrame frame; frame = new JFrame("Maze -- Builder"); frame.setContentPane(new Maze.MazePanel(maze)); frame.pack(); Dimension frameDim = frame.getSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(screenSize.width / 2 - frameDim.width / 2, screenSize.height / 2 - frameDim.height / 2); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); } */ }

Wall

import java.awt.*; import java.applet.AudioClip;

@SuppressWarnings("unused") public class Wall implements MapSite {

public static final Color WALL_COLOR = Color.gray;

public Object clone() throws CloneNotSupportedException { return super.clone(); }

public void enter(Maze maze) { //hurts.play(); }

public void draw(Graphics g, int x, int y, int w, int h) { g.setColor(WALL_COLOR); g.fillRect(x, y, w, h); }

// protected static AudioClip hurts = AudioUtility.getInstance().getAudioClip("ow.wav");

}

UndoableCommand

public interface UndoableCommand extends Command {

public void undo();

}

Orientation

public class Orientation {

public static final Orientation VERTICAL = new Orientation("Vertical"); public static final Orientation HORIZONTAL = new Orientation("Horizontal");

public String toString() { return name; }

private Orientation(String name) { this.name = name; } private final String name;

}

OpenDoorCommand

public class OpenDoorCommand implements UndoableCommand { private final Maze maze; /** * Creates a new command. * @param maze the maze to affect */ public OpenDoorCommand(Maze maze) { this.maze = maze; } // get current room and invert any adjacent doors @Override public void execute() { Door door = null; // open all doors Direction d = Direction.first(); while (d != null) { MapSite m = maze.getCurrentRoom().getSide(d); if (m instanceof Door) { door = ((Door) m); door.setOpen(!door.isOpen()); } d = d.next(); } // if( door != null ) ((Object) AudioUtility.PLAY( Door.AudioUrl)).play(); if( maze.view != null ) maze.view.repaint(); } // refer to execute() @Override public void undo() { execute(); } } MazeOpenDoorCommand public class MazeOpenDoorCommand implements UndoableCommand {

public MazeOpenDoorCommand(Maze maze) { this.maze = maze; }

public void execute() { Room currRoom = maze.getCurrentRoom(); Direction dir = Direction.first(); while (dir != null){ MapSite m = currRoom.getSide(dir); if (m instanceof Door){ ((Door)m).setOpen(true); currRoom.setSide(dir, m); } dir = dir.next(); } }

public void undo() {}

protected Maze maze;

}

MazeMoveCommand

public class MazeMoveCommand implements UndoableCommand {

public MazeMoveCommand(Maze maze, Direction direction) { this.maze = maze; this.direction = direction; }

public void execute() { maze.move(direction); }

public void undo() { maze.move(direction.opposite()); }

protected Maze maze; protected Direction direction;

}

MazeFactory

public class MazeFactory {

public Maze makeMaze() { return new Maze(); }

public Wall makeWall() { return new Wall(); }

public Room makeRoom(int roomNumber) { return new Room(roomNumber); }

public Door makeDoor(Room room1, Room room2) { return new Door(room1, room2); }

}

Command

public interface Command {

public void execute();

}

AudioUtility

import java.applet.Applet; import java.applet.AudioClip; import java.net.MalformedURLException; import java.net.URL;

public class AudioUtility { public static final AudioClip getAudioClip(String filename) { if (filename != null) { try { return Applet.newAudioClip(new URL("http://harp.njit.edu/~dkk8/cs602/cs602all/Assignments/Assignment10/" + filename)); } catch (MalformedURLException e) { e.printStackTrace(); } } return null; }

public static Object PLAY(String audiourl) { // TODO Auto-generated method stub return null; }

}

CustomMazeBuilder

import java.util.ArrayList; import java.util.List; /*import maze.Direction; import maze.MapSite; import maze.Maze; import maze.Door; import maze.MazeFactory; import maze.Room;*/

public class CustomMazeBuilder { private static int randomInt(int i){ return (int)((double)i * Math.random()); } private static void setStartEnd(Maze maze, int cols, int rows) { int startRoom = 0; int endRoom = 0;

switch(randomInt(4)) { case 0: startRoom = 1; endRoom = cols * rows; break; case 1: startRoom = cols; endRoom = (cols * (rows - 1)) + 1; break; case 2: startRoom = cols * rows; endRoom = 1; break; case 3: startRoom = (cols * (rows - 1)) + 1; endRoom = cols; break; } maze.setStartEndRoom(startRoom, endRoom); }

private static void setOpenableDoors(Maze maze, int cols, int rows) { List allDoors = new ArrayList(); int totalRooms = cols * rows; for(int i=1; i <= totalRooms; i++){ Room room = maze.findRoom(i); for (Direction dir = Direction.first(); dir != null; dir = dir.next()) { MapSite side = room.getSide(dir); if(side instanceof Door){ allDoors.add((Door)side); } } } for(int i=0; i < allDoors.size(); i++){ if(i%4==0){ Door door = (Door)allDoors.get(i); door.setOpen(false); } } } public static Maze makeMaze(int cols, int rows, MazeFactory factory) { Maze maze = factory.makeMaze();

int totalRooms = cols * rows; for(int i=1; i <= totalRooms; i++){ Room room = factory.makeRoom(i); for (Direction dir = Direction.first(); dir != null; dir = dir.next()) { room.setSide(dir, factory.makeWall()); } maze.addRoom(room); }

int [][] m = new int[cols][rows]; for(int i = 0; i < cols; i++) { for(int j = 0; j < rows; j++) { m[i][j] |= 15; //Initialize room

if(j == 0) // First row m[i][0] |= 16;

if(i == cols - 1) // Last column m[i][j] |= 32;

if(j == rows - 1) // Last row m[i][j] |= 64;

if(i == 0) // First Column m[0][j] |= 128; } } int ai[] = new int[4]; int l = randomInt(cols); int i1 = randomInt(rows); int stackPtr = 0; int [] stack = new int[totalRooms];

for(int i = 1; i < totalRooms;) { int j = 0; if((m[l][i1] & 16) == 0 && (m[l][i1 - 1] & 15) == 15) ai[j++] = 0; if((m[l][i1] & 32) == 0 && (m[l + 1][i1] & 15) == 15) ai[j++] = 1; if((m[l][i1] & 64) == 0 && (m[l][i1 + 1] & 15) == 15) ai[j++] = 2; if((m[l][i1] & 128) == 0 && (m[l - 1][i1] & 15) == 15) ai[j++] = 3; if(j != 0) { int k = ai[randomInt(j)]; stack[stackPtr++] = k;

// l = col number // i1 = row number Direction dir = null; int room1 = (i1 * cols) + l + 1; int room2 = 0; switch(k) { case 0: dir = Direction.SOUTH; room2 = room1-cols; break; case 1: dir = Direction.EAST; room2 = room1 + 1; break; case 2: dir = Direction.NORTH; room2 = room1 + cols; break; case 3: dir = Direction.WEST; room2 = room1 - 1; break; }

Room roomObj1 = maze.findRoom(room1); Room roomObj2 = maze.findRoom(room2); Door door = factory.makeDoor(roomObj1, roomObj2); roomObj1.setSide(dir, door); roomObj2.setSide(dir.opposite(), door); door.setOpen(true);

switch(k) { case 0: m[l][i1] &= -2; m[l][--i1] &= -5; break;

case 1: m[l][i1] &= -3; m[++l][i1] &= -9; break; case 2: m[l][i1] &= -5; m[l][++i1] &= -2; break;

case 3: m[l][i1] &= -9; m[--l][i1] &= -3; break; } i++; } else { switch(stack[--stackPtr]) { case 0: i1++; break; case 1: l--; break;

case 2: i1--; break;

case 3: l++; break; } } }

setStartEnd(maze, cols, rows); setOpenableDoors(maze, cols, rows); return maze; } }

MapSite

import java.awt.*;

public interface MapSite extends Cloneable {

public Object clone() throws CloneNotSupportedException; public void enter(Maze maze); public void draw(Graphics g, int x, int y, int w, int h);

}

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!