diff --git a/cs621/proj4/KnightsTour.java b/cs621/proj4/KnightsTour.java index b6f1d76..cb9ffdd 100644 --- a/cs621/proj4/KnightsTour.java +++ b/cs621/proj4/KnightsTour.java @@ -67,6 +67,9 @@ public class KnightsTour extends JFrame setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(m_startButton = new JButton("Start Knight's Tour")); m_startButton.addActionListener(m_handler); + add(new Box.Filler(new Dimension(0, 4), + new Dimension(0, 4), + new Dimension(0, 4))); add(m_statusLabel = new JLabel()); }}); }}); @@ -90,10 +93,10 @@ public class KnightsTour extends JFrame m_boardPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); for (int x = 0; x < width; x++) { - for (int y = 0; y < width; y++) + for (int y = 0; y < height; y++) { JLabel l = new JLabel(m_ktBoard.getStepAt(x, y) == 0 - ? "h" + ? "" : new Integer(m_ktBoard.getStepAt(x, y)).toString(), SwingConstants.CENTER); l.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); @@ -117,6 +120,42 @@ public class KnightsTour extends JFrame if (e.getSource() == m_startButton) { m_statusLabel.setText("Running..."); + + /* first validate input text fields */ + int width, height, startx, starty; + try + { + width = Integer.parseInt(m_widthField.getText()); + height = Integer.parseInt(m_heightField.getText()); + startx = Integer.parseInt(m_startXField.getText()); + starty = Integer.parseInt(m_startYField.getText()); + } + catch (NumberFormatException nfe) + { + m_statusLabel.setText("Non-numerical input!"); + return; + } + if (width < 1 || height < 1 || startx < 0 || starty < 0 || + startx >= width || starty >= height) + { + m_statusLabel.setText("Invalid input!"); + return; + } + + /* create a new KnightsTourBoard object */ + m_ktBoard = new KnightsTourBoard(width, height); + createBoardPanel(); + + /* run the tour and update GUI */ + if (m_ktBoard.tour(startx, starty)) + { + createBoardPanel(); + m_statusLabel.setText("Finished."); + } + else + { + m_statusLabel.setText("Solution not found!"); + } } } }