// Description: Maze Program // Author: Chris Wilcox // Date: 2/1/2013 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Image; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Scanner; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.border.LineBorder; public class Maze extends JFrame { // Window information private int windowWidth; private int windowHeight; // Maze variables private char mazeData[][]; private int mazeRows; private int mazeCols; private int finalRow; private int finalCol; private int currRow; private int currCol; private int prevRow = -1; private int prevCol = -1; // User interface private JPanel panel; private Image nemo, dory, both, shark, jelly; private ArrayList buttons; // Robot constructor public Maze(String fileName) { // Read maze readMaze(fileName); // Graphics setup setupGraphics(); } // Get width public int getRows() { return mazeRows; } // Get height public int getCols() { return mazeCols; } // Move right public boolean moveRight() { // Legal move? if ((currCol+1) < mazeCols) if ((mazeData[currRow][currCol+1] == 'W') || // Water (mazeData[currRow][currCol+1] == 'N')) // Nemo { currCol++; redraw(true); return true; } System.out.println("currentCol:" + currCol); return false; } // Move left public boolean moveLeft() { // Legal move? if (currCol > 0) if ((mazeData[currRow][currCol-1] == 'W') || // Water (mazeData[currRow][currCol-1] == 'N')) // Nemo { currCol--; redraw(true); return true; } System.out.println("currentCol:" + currCol); return false; } // Move up public boolean moveUp() { // Legal move? if (currRow > 0) if ((mazeData[currRow-1][currCol] == 'W') || // Water (mazeData[currRow-1][currCol] == 'N')) // Nemo { currRow--; redraw(true); return true; } System.out.println("currentRow:" + currRow); return false; } // Move down public boolean moveDown() { // Legal move? if ((currRow+1) < mazeRows) if ((mazeData[currRow+1][currCol] == 'W') || // Water (mazeData[currRow+1][currCol] == 'N')) // Nemo { currRow++; redraw(true); return true; } System.out.println("currentRow:" + currRow); return false; } private void redraw(boolean print) { // Wait for awhile try { Thread.sleep(500); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } // if (print) // System.out.println("Moving to row " + currRow + ", column " + currCol); // Update position if ((prevRow >= 0) && (prevCol >= 0)) mazeData[prevRow][prevCol] = 'W'; mazeData[currRow][currCol] = 'D'; // Compute index and remove icon int index = (prevRow * mazeCols) + prevCol; if ((prevRow >= 0) && (prevCol >= 0)) buttons.get(index).setIcon(null); // Compute index and add icon index = (currRow * mazeCols) + currCol; if ((currRow == finalRow) && (currCol == finalCol)) buttons.get(index).setIcon(new ImageIcon(both)); else buttons.get(index).setIcon(new ImageIcon(dory)); // Maze solved if ((currRow == finalRow) && (currCol == finalCol)) {System.out.println("currentRow when found Nemo:" + currRow); System.out.println("currentCol when found Nemo:" + currCol); System.out.println("Dory found Nemo!");} // Store previous location prevRow = currRow; prevCol = currCol; } // Set button private void setButton(JButton button, int row, int col) { if (mazeData[row][col] == 'D') { button.setIcon(new ImageIcon(dory)); currRow = row; currCol = col; } else if (mazeData[row][col] == 'N') { button.setIcon(new ImageIcon(nemo)); finalRow = row; finalCol = col; } else if (mazeData[row][col] == 'S') button.setIcon(new ImageIcon(shark)); else if (mazeData[row][col] == 'J') button.setIcon(new ImageIcon(jelly)); } // Read maze private void readMaze(String filename) { try { // Open file Scanner scan = new Scanner(new File(filename)); // Read numbers mazeRows = scan.nextInt(); mazeCols = scan.nextInt(); // Allocate maze mazeData = new char[mazeRows][mazeCols]; // Read maze for (int row = 0; row < mazeRows; row++) { // Read line String line = scan.next(); for (int col = 0; col < mazeCols; col++) { mazeData[row][col] = line.charAt(col); } } // Close file scan.close(); } catch (IOException e) { System.out.println("Cannot read maze: " + filename); System.exit(0); } } // Setup graphics private void setupGraphics() { // Create grid panel = new JPanel(); panel.setLayout(new GridLayout(mazeRows, mazeCols, 0, 0)); add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH); add(panel, BorderLayout.CENTER); // Look and feel try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } // Configure window windowWidth = mazeCols * 100; windowHeight = mazeRows * 100; setSize(windowWidth, windowHeight); setTitle("Maze"); setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setAlwaysOnTop(true); // Load and scale images ImageIcon icon0 = new ImageIcon("Nemo.jpg"); Image image0 = icon0.getImage(); nemo = image0.getScaledInstance(100, 100, Image.SCALE_DEFAULT); ImageIcon icon1 = new ImageIcon("Dory.jpg"); Image image1 = icon1.getImage(); dory = image1.getScaledInstance(100, 100, Image.SCALE_DEFAULT); ImageIcon icon2 = new ImageIcon("Both.jpg"); Image image2 = icon2.getImage(); both = image2.getScaledInstance(100, 100, Image.SCALE_DEFAULT); ImageIcon icon3 = new ImageIcon("Shark.jpg"); Image image3 = icon3.getImage(); shark = image3.getScaledInstance(100, 100, Image.SCALE_DEFAULT); ImageIcon icon4 = new ImageIcon("Jelly.jpg"); Image image4 = icon4.getImage(); jelly = image4.getScaledInstance(100, 100, Image.SCALE_DEFAULT); // Build panel of buttons buttons = new ArrayList(); for (int row = 0; row < mazeRows; row++) { for (int col = 0; col < mazeCols; col++) { // Initialize and add button JButton button = new JButton(); Border border = new LineBorder(Color.darkGray, 4); button.setOpaque(true); button.setBackground(Color.blue); button.setBorder(border); setButton(button, row, col); panel.add(button); buttons.add(button); } } // Show window redraw(false); setVisible(true); } }