我正在编写一个函数,将骑士在棋盘上的初始位置作为参数和骑士将要做的一些动作,并返回骑士将能够实现的所有领域的列表对于给定的移动次数。我运行了一个JUnit测试,对于深度1,2和3,该函数正常工作,但是对于深度4,列表错过了4个字段。我不知道,错误可能在哪里。这是代码:
Knight.java
private boolean moveIsPossible (Position pos) {
return (pos.x >= 0 && pos.x <= 7) && (pos.y >= 0 && pos.y <= 7);
}
int [] x = {1, 2, 2, 1, -1, -2, -2, -1};
int [] y = {2, 1, -1, -2, -2, -1, 1, 2};
private ArrayList<Position> reachableSet = new ArrayList<Position>();
@Override
public ArrayList<Position> getReachableSet(Position pos, int numberofMoves) {
if (!reachableSet.contains(pos)) reachableSet.add(pos);
if (numberofMoves == 0) return reachableSet;
ArrayList<Position> moves = new ArrayList<Position>();
for (int i = 0; i < 8; i++) {
Position candidate = new Position (pos.x+x[i], pos.y+y[i]);
if (!moveIsPossible(candidate)) continue;
if (!reachableSet.contains(candidate)) {
moves.add(candidate);
}
}
for (int j = 0; j < moves.size(); j++) {
reachableSet = getReachableSet(moves.get(j), numberofMoves-1);
}
return reachableSet;
}
这里是职业类:
public class Position implements Comparable<Position> {
/**
* The row on the chess board
*/
public int x;
/**
* The column on the chess board
*/
public int y;
/**
* Create a new position object
* @param x the row on the chess board
* @param y the column on the chess board
*/
public Position(int x, int y) {
this.x = x;
this.y = y;
}
}
这是我使用的JUnit测试。
public class Tests {
private IKnight knight;
@Before
public void setup() {
knight = KnightFactory.create();
}
private String printSet(ArrayList<Position> positions)
{
StringBuffer s = new StringBuffer("\n ");
for (int x=0; x<IKnight.boardSize; x++) {
s.append(x);
}
s.append("\n ");
for (int x=0; x<IKnight.boardSize; x++) {
s.append("-");
}
s.append("\n");
for (int y=0; y<IKnight.boardSize; y++) {
s.append(y).append("|");
for (int x=0; x<IKnight.boardSize; x++) {
if (positions.contains(new Position(x, y))) {
s.append("X");
} else {
s.append(" ");
}
}
s.append("\n");
}
return s.toString();
}
private void compare(ArrayList<Position> result, int[]... expected) {
Collections.sort(result);
ArrayList<Position> expectedVec = new ArrayList<Position>(expected.length);
for (int[] pos : expected) {
expectedVec.add(new Position(pos[0], pos[1]));
}
Collections.sort(expectedVec);
Assert.assertEquals(printSet(result), expectedVec.toString(), result.toString());
}
@Test
public void noHop() {
ArrayList<Position> result = knight.getReachableSet(new Position(4, 4), 0);
int expected[][] = { { 4, 4 } };
compare(result, expected);
}
@Test
public void oneHop() {
ArrayList<Position> result = knight.getReachableSet(new Position(4, 4), 1);
int expected[][] = { { 4, 4 }, { 5, 6 }, { 6, 5 }, { 6, 3 }, { 5, 2 },
{ 3, 2 }, { 2, 3 }, { 2, 5 }, { 3, 6 } };
compare(result, expected);
}
@Test
public void oneHopCorner() {
ArrayList<Position> result = knight.getReachableSet(new Position(0, 0), 1);
int expected[][] = { { 0, 0 }, { 1, 2 }, { 2, 1 } };
compare(result, expected);
}
@Test
public void twoHopsCorner() {
ArrayList<Position> result = knight.getReachableSet(new Position(0, 0), 2);
int expected[][] = { { 0, 0 }, { 0, 2 }, { 0, 4 }, { 1, 2 }, { 1, 3 },
{ 2, 0 }, { 2, 1 }, { 2, 4 }, { 3, 1 }, { 3, 3 }, { 4, 0 },
{ 4, 2 } };
compare(result, expected);
}
@Test
public void threeHops() {
ArrayList<Position> result = knight.getReachableSet(new Position(4, 4), 3);
int expected[][] = {{ 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 },
{ 0, 5 }, { 0, 6 }, { 0, 7 }, { 1, 0 }, { 1, 1 }, { 1, 2 },
{ 1, 3 }, { 1, 4 }, { 1, 5 }, { 1, 6 }, { 1, 7 }, { 2, 0 },
{ 2, 1 }, { 2, 3 }, { 2, 4 }, { 2, 5 },
{ 2, 7 }, { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }, { 3, 4 },
{ 3, 5 }, { 3, 6 }, { 3, 7 }, { 4, 0 }, { 4, 1 }, { 4, 2 },
{ 4, 3 }, { 4, 4 }, { 4, 5 }, { 4, 6 }, { 4, 7 }, { 5, 0 },
{ 5, 1 }, { 5, 2 }, { 5, 3 }, { 5, 4 }, { 5, 5 }, { 5, 6 },
{ 5, 7 }, { 6, 0 }, { 6, 1 }, { 6, 3 }, { 6, 4 },
{ 6, 5 }, { 6, 7 }, { 7, 0 }, { 7, 1 }, { 7, 2 },
{ 7, 3 }, { 7, 4 }, { 7, 5 }, { 7, 6 }, { 7, 7 }, };
compare(result, expected);
}
@Test
public void fourHops() {
ArrayList<Position> result = knight.getReachableSet(new Position(4, 4), 4);
int expected[][] = { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 },
{ 0, 5 }, { 0, 6 }, { 0, 7 }, { 1, 0 }, { 1, 1 }, { 1, 2 },
{ 1, 3 }, { 1, 4 }, { 1, 5 }, { 1, 6 }, { 1, 7 }, { 2, 0 },
{ 2, 1 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, { 2, 5 }, { 2, 6 },
{ 2, 7 }, { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }, { 3, 4 },
{ 3, 5 }, { 3, 6 }, { 3, 7 }, { 4, 0 }, { 4, 1 }, { 4, 2 },
{ 4, 3 }, { 4, 4 }, { 4, 5 }, { 4, 6 }, { 4, 7 }, { 5, 0 },
{ 5, 1 }, { 5, 2 }, { 5, 3 }, { 5, 4 }, { 5, 5 }, { 5, 6 },
{ 5, 7 }, { 6, 0 }, { 6, 1 }, { 6, 2 }, { 6, 3 }, { 6, 4 },
{ 6, 5 }, { 6, 6 }, { 6, 7 }, { 7, 0 }, { 7, 1 }, { 7, 2 },
{ 7, 3 }, { 7, 4 }, { 7, 5 }, { 7, 6 }, { 7, 7 }, };
compare(result, expected);
}