diff --git a/cs621/proj4/KnightsTour.java b/cs621/proj4/KnightsTour.java index 74bca40..6e61895 100644 --- a/cs621/proj4/KnightsTour.java +++ b/cs621/proj4/KnightsTour.java @@ -5,6 +5,9 @@ import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; +/** + * This class implements a GUI interface to the KnightsTourBoard class + */ public class KnightsTour extends JFrame { private KnightsTourBoard m_ktBoard = new KnightsTourBoard(5, 5); @@ -24,6 +27,9 @@ public class KnightsTour extends JFrame private JPanel m_boardPanel; private JLabel m_statusLabel; + /** + * Construct the KnightsTour GUI interface + */ public KnightsTour() { super("Josh's Knight's Tour project for CS621"); @@ -103,6 +109,8 @@ public class KnightsTour extends JFrame setVisible(true); } + /* Create a JPanel containing a grid showing the step number + * for each board position in the KnightsTourBoard object */ private void createBoardPanel() { int width = m_ktBoard.getWidth(); @@ -128,6 +136,7 @@ public class KnightsTour extends JFrame m_mainPanel.validate(); } + /* This helper class catches actions that occur on JButton objects */ private class ActionEventHandler implements ActionListener { private KnightsTour m_kt; @@ -163,6 +172,10 @@ public class KnightsTour extends JFrame } } + /** + * Helper method to set up the Knight's Tour GUI board + * @param runit whether to run the tour or not (false means just reset board) + */ private void setUpTour(boolean runit) { /* first validate input text fields */ @@ -207,16 +220,22 @@ public class KnightsTour extends JFrame } } + /* This class runs as a separate thread to actually call + * tour() on the KnightsTourBoard object. This way, the GUI + * stays responsive and the user can cancel the tour by clicking + * a button if it runs for too long. */ private class TourThread implements Runnable { int m_startx, m_starty; + /* create the tour thread with the given starting position */ public TourThread(int startx, int starty) { m_startx = startx; m_starty = starty; } + /* called when the thread starts executing */ public void run() { if (m_ktBoard.tour(m_startx, m_starty)) @@ -249,6 +268,10 @@ public class KnightsTour extends JFrame } } + /** + * This method is invoked first when the program is run. + * It simply creates a KnightsTour GUI object + */ public static void main(String[] args) { KnightsTour kt = new KnightsTour(); diff --git a/cs621/proj4/KnightsTourBoard.java b/cs621/proj4/KnightsTourBoard.java index cfa6f43..fa0b795 100644 --- a/cs621/proj4/KnightsTourBoard.java +++ b/cs621/proj4/KnightsTourBoard.java @@ -1,10 +1,18 @@ +/** + * This class implements the model for the Knight's Tour problem. + */ public class KnightsTourBoard { protected int m_width; protected int m_height; protected int[][] m_board; + /** + * Construct a Knight's Tour Board object + * @param width the width of the board + * @param height the height of the board + */ KnightsTourBoard(int width, int height) { m_width = width; @@ -12,10 +20,29 @@ public class KnightsTourBoard m_board = new int[width][height]; } + /** + * @return the width of the board + */ public int getWidth() { return m_width; } + + /** + * @return the height of the board + */ public int getHeight() { return m_height; } + + /** + * @param x the x coordinate of the position in question + * @param y the y coordinate of the position in question + * @return the step number at the given position in the board + */ public int getStepAt(int x, int y) { return m_board[x][y]; } + /** + * Attempt to run a Knight's Tour from the starting position + * @param startx the x coordinate of the starting position + * @param starty the y coordinate of the starting position + * @return true if it was possible to perform the tour, otherwise false + */ public boolean tour(int startx, int starty) { for (int x = 0; x < m_width; x++) @@ -25,6 +52,8 @@ public class KnightsTourBoard return tourFrom(2, startx, starty); } + /* recursive helper function to tour() + * which stores the current step number */ private boolean tourFrom(int nextStep, int startx, int starty) { // System.out.println("tourFrom(" + nextStep + ", " + diff --git a/cs621/proj4/KnightsTourBoardTest.java b/cs621/proj4/KnightsTourBoardTest.java index 45f4fb3..468c10b 100644 --- a/cs621/proj4/KnightsTourBoardTest.java +++ b/cs621/proj4/KnightsTourBoardTest.java @@ -1,6 +1,9 @@ import junit.framework.TestCase; +/** + * This class tests the KnightsTourBoard class with junit test cases. + */ public class KnightsTourBoardTest extends TestCase { public void testKnightsTourBoardImpossible1()