tour board working
git-svn-id: svn://anubis/gvsu@83 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
4654beb362
commit
bc575f8430
@ -7,6 +7,8 @@ public class KnightsTourBoard
|
|||||||
|
|
||||||
KnightsTourBoard(int width, int height)
|
KnightsTourBoard(int width, int height)
|
||||||
{
|
{
|
||||||
|
m_width = width;
|
||||||
|
m_height = height;
|
||||||
m_board = new int[width][height];
|
m_board = new int[width][height];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +27,8 @@ public class KnightsTourBoard
|
|||||||
|
|
||||||
private boolean tourFrom(int nextStep, int startx, int starty)
|
private boolean tourFrom(int nextStep, int startx, int starty)
|
||||||
{
|
{
|
||||||
|
// System.out.println("tourFrom(" + nextStep + ", " +
|
||||||
|
// startx + ", " + starty + ")");
|
||||||
final int[] xoffsets = {-2, -1, 1, 2, -2, -1, 1, 2};
|
final int[] xoffsets = {-2, -1, 1, 2, -2, -1, 1, 2};
|
||||||
final int[] yoffsets = {-1, -2, -2, -1, 1, 2, 2, 1};
|
final int[] yoffsets = {-1, -2, -2, -1, 1, 2, 2, 1};
|
||||||
|
|
||||||
@ -35,7 +39,7 @@ public class KnightsTourBoard
|
|||||||
|
|
||||||
if (x >= 0 && x < m_width && y >= 0 && y < m_height)
|
if (x >= 0 && x < m_width && y >= 0 && y < m_height)
|
||||||
{
|
{
|
||||||
if (m_board[x][y] == 0) /* space has not been visited yet */
|
if (m_board[x][y] == 0) /* if space has not been visited yet */
|
||||||
{
|
{
|
||||||
m_board[x][y] = nextStep; /* we just took step number
|
m_board[x][y] = nextStep; /* we just took step number
|
||||||
* nextStop, stop if that is all
|
* nextStop, stop if that is all
|
||||||
|
@ -14,20 +14,23 @@ public class KnightsTourBoardTest extends TestCase
|
|||||||
|
|
||||||
public void testKnightsTourBoardImpossible2()
|
public void testKnightsTourBoardImpossible2()
|
||||||
{
|
{
|
||||||
KnightsTourBoard ktb = new KnightsTourBoard(3, 4);
|
KnightsTourBoard ktb = new KnightsTourBoard(2, 3);
|
||||||
|
|
||||||
assertFalse(ktb.tour(0, 0));
|
assertFalse(ktb.tour(0, 0));
|
||||||
assertFalse(ktb.tour(1, 1));
|
|
||||||
assertFalse(ktb.tour(0, 1));
|
assertFalse(ktb.tour(0, 1));
|
||||||
assertFalse(ktb.tour(1, 2));
|
|
||||||
assertFalse(ktb.tour(1, 3));
|
|
||||||
assertFalse(ktb.tour(2, 3));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testKnightsTourBoardPossible1()
|
public void testKnightsTourBoardPossible1()
|
||||||
{
|
{
|
||||||
KnightsTourBoard ktb = new KnightsTourBoard(5, 5);
|
KnightsTourBoard ktb = new KnightsTourBoard(5, 5);
|
||||||
|
|
||||||
assertTrue(ktb.tour(2, 2));
|
assertTrue(ktb.tour(0, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testKnightsTourBoardPossible2()
|
||||||
|
{
|
||||||
|
KnightsTourBoard ktb = new KnightsTourBoard(3, 4);
|
||||||
|
|
||||||
|
assertTrue(ktb.tour(0, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user