fixed y to loop 0..height not 0..width, performing tour on click now
git-svn-id: svn://anubis/gvsu@89 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
9a09f532ff
commit
66748cb762
@ -67,6 +67,9 @@ public class KnightsTour extends JFrame
|
|||||||
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||||
add(m_startButton = new JButton("Start Knight's Tour"));
|
add(m_startButton = new JButton("Start Knight's Tour"));
|
||||||
m_startButton.addActionListener(m_handler);
|
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());
|
add(m_statusLabel = new JLabel());
|
||||||
}});
|
}});
|
||||||
}});
|
}});
|
||||||
@ -90,10 +93,10 @@ public class KnightsTour extends JFrame
|
|||||||
m_boardPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
|
m_boardPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
|
||||||
for (int x = 0; x < width; x++)
|
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
|
JLabel l = new JLabel(m_ktBoard.getStepAt(x, y) == 0
|
||||||
? "h"
|
? ""
|
||||||
: new Integer(m_ktBoard.getStepAt(x, y)).toString(),
|
: new Integer(m_ktBoard.getStepAt(x, y)).toString(),
|
||||||
SwingConstants.CENTER);
|
SwingConstants.CENTER);
|
||||||
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
|
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
|
||||||
@ -117,6 +120,42 @@ public class KnightsTour extends JFrame
|
|||||||
if (e.getSource() == m_startButton)
|
if (e.getSource() == m_startButton)
|
||||||
{
|
{
|
||||||
m_statusLabel.setText("Running...");
|
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!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user