Java 生成给定深度下的国际象棋骑士招式
我正在编写一个函数,该函数获取骑士在棋盘上的初始位置作为参数,以及骑士将执行的一些移动,并返回一个列表,列出骑士在给定移动次数下能够实现的所有字段。我运行了JUnit测试,对于深度1、2和3,该函数工作正常,但是对于深度4,该列表缺少4个字段。我不知道错误在哪里。代码如下:Java 生成给定深度下的国际象棋骑士招式,java,recursion,chess,Java,Recursion,Chess,我正在编写一个函数,该函数获取骑士在棋盘上的初始位置作为参数,以及骑士将执行的一些移动,并返回一个列表,列出骑士在给定移动次数下能够实现的所有字段。我运行了JUnit测试,对于深度1、2和3,该函数工作正常,但是对于深度4,该列表缺少4个字段。我不知道错误在哪里。代码如下: Knight.java private boolean moveIsPossible (Position pos) { return (pos.x >= 0 && pos.x <
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测试
位置和移动的代码可能是什么?@Zoyd我更新了description@dasblinkenlight我打错了单词,只有一个Knight,很抱歉混淆了我运行了JUnit测试,对于深度1、2和3,函数工作正常,但是对于深度4,列表缺少4个字段-您在该测试中的起始位置是什么?考虑到起始位置,这四个动作可能吗?由于您没有向我们展示测试,或者moveIsPossible的定义,我们无法回答这些问题。作为一般性的评论:向您的Position对象添加equals和hashCode,并使用实际的集合作为可达集合。实际上,您的代码被破坏了,因为contains总是返回false。
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);
}