Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package maze;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.util.ArrayList;
  7. import java.io.*;
  8. import java.util.Scanner;
  9.  
  10. public class GUI implements ActionListener {
  11.  
  12.    private JButton northButton = new JButton("North");
  13.    private JButton southButton = new JButton("South");
  14.    private JButton eastButton = new JButton("East");
  15.    private JButton westButton = new JButton("West");
  16.    private JButton solveButton = new JButton("Solve");
  17.    private JButton saveButton = new JButton("Save");
  18.    private MazeCrawler maze;
  19.    private final String SAVE_FILE_NAME = "maze.txt";
  20.  
  21.    public static void pause() {
  22.       try {
  23.          Thread.sleep(25);        // wait 1/40 s
  24.       } catch (Exception e) {
  25.          e.printStackTrace();
  26.       }
  27.    } // end method pause
  28.  
  29.    private static ArrayList<String> getTextMaze(String filename) {
  30.       ArrayList<String> textMaze = new ArrayList<String>();
  31.       File handle = new File(filename);
  32.       try {
  33.          Scanner mazeFile = new Scanner(handle);
  34.          while (mazeFile.hasNext()) {
  35.             textMaze.add(mazeFile.nextLine());
  36.          }
  37.       } catch (Exception e) {
  38.          System.out.println("Error reading file");
  39.          System.exit(1);
  40.       }
  41.       return textMaze;
  42.    } // end getTextMaze
  43.  
  44.    public void go() {
  45.       JFrame frame = new JFrame();
  46.       frame.setTitle("CS241 Maze Crawl");
  47.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.       maze = new MazeCrawler(15, 32);
  49.       //maze = new MazeCrawler(getTextMaze(SAVE_FILE_NAME));
  50.  
  51.       int xSize = Math.max(MazeCell.size * (maze.getColumns() + 1), 400);
  52.       frame.setSize(xSize, MazeCell.size * (maze.getRows() + 1) + 60);
  53.       frame.setLocation(300, 50);
  54.       frame.getContentPane().add(BorderLayout.CENTER, maze);
  55.       JPanel buttonPanel = new JPanel();
  56.  
  57.       buttonPanel.add(westButton);
  58.       westButton.addActionListener(this);
  59.       buttonPanel.add(northButton);
  60.       northButton.addActionListener(this);
  61.       buttonPanel.add(southButton);
  62.       southButton.addActionListener(this);
  63.       buttonPanel.add(eastButton);
  64.       eastButton.addActionListener(this);
  65.       buttonPanel.add(solveButton);
  66.       solveButton.addActionListener(this);
  67.       buttonPanel.add(saveButton);
  68.       saveButton.addActionListener(this);
  69.      
  70.       frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
  71.       frame.setVisible(true);
  72.    } // end method main
  73.  
  74.    public static void win() {
  75.       JOptionPane.showMessageDialog(null, "Path Found!");
  76.       System.exit(0);
  77.    } // end method
  78.  
  79.    public static void lose() {
  80.       JOptionPane.showMessageDialog(null, "Nowhere to go from here!");
  81.       System.exit(1);
  82.    } // end method lose
  83.  
  84.    public void actionPerformed(ActionEvent event) {
  85.       if (event.getSource() == northButton) {
  86.          maze.moveNorth();
  87.       }
  88.       if (event.getSource() == southButton) {
  89.          maze.moveSouth();
  90.       }
  91.       if (event.getSource() == eastButton) {
  92.          maze.moveEast();
  93.       }
  94.       if (event.getSource() == westButton) {
  95.          maze.moveWest();
  96.       }
  97.       if (event.getSource() == saveButton) {
  98.          maze.saveToFile(SAVE_FILE_NAME);
  99.       }
  100.       if (event.getSource() == solveButton) {
  101.          maze.solveMaze();
  102.       }
  103.  
  104.       if (maze.isWin()) {
  105.          win();
  106.       }
  107.       if (maze.isNoMove()) {
  108.          lose();
  109.       }  
  110.    } // end actionPerformed
  111.    
  112. } // end class GUI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement