[英]What's wrong with this recursion for checkers move generation?
I'm writing a simple checkers game in Java. 我正在用Java编写一个简单的跳棋游戏。 The hard part so far is to generate a list of possible jumps for a piece, especially when there are several paths to jump. 到目前为止,最困难的部分是生成一个片段可能发生的跳转的列表,尤其是当存在多个跳转路径时。 I have this recursion to find all possible paths to do a series of jumps. 我有这个递归来找到所有可能的路径来进行一系列的跳跃。 Here's some code: 这是一些代码:
/*
* possibleJumps(Square pos, Board b, Move jump, ArrayList<Move> moves)
* Add all possible jumps from the position b on board b to movelist moves.
*/
void possibleJumps(Square pos, Board b, Move jump, ArrayList<Move> moves) {
ArrayList<Move> simpleJ = this.simpleJumps(pos, b);
//jump.addJumpSquare(pos);
//System.out.println("check jump " + pos + " so far:" + jump);
if (simpleJ.isEmpty()) {
// no more jumps -> end this jump and add it to the list
jump.endJump(pos);
moves.add(jump);
System.out.println("found jump" + jump);
return;
}
for(Move j:simpleJ) {
jump.addJumpSquare(j.jumped.get(0)); // add the jumped square to the jump path
possibleJumps(j.to, b.doMove(j), new Move(jump), moves);
}
}
Just to explain: simpleJumps generates a list of possible jumps over one square (so basically all attack moves). 只是说明一下:simpleJumps会生成一个超过一个平方的可能跳跃的列表(因此基本上是所有攻击动作)。 That works fine. 很好
Here's the test board: 这是测试板:
A B C D E F G H
---------------------------------
1 | | o | | | | | | | 1 (0)
---------------------------------
2 | | | | | | | | | 2 (1)
---------------------------------
3 | | o | | o | | o | | o | 3 (2)
---------------------------------
4 | | | | | | | | | 4 (3)
---------------------------------
5 | | o | | o | | o | | o | 5 (4)
---------------------------------
6 | | | | | | | | | 6 (5)
---------------------------------
7 | | o | | O | | o | | | 7 (6)
---------------------------------
8 | x | | | | | | | | 8 (7)
---------------------------------
Here's the output I get: 这是我得到的输出:
found jumpa8c2 via b7-b5-b3-
found jumpa8c2 via b7-b5-d5-d3-
found jumpa8g2 via b7-b5-d5-d3-f3-
What it should be is: 应该是:
found jumpa8c2 via b7-b5-b3-
found jumpa8c2 via b7-d5-d3-
found jumpa8g2 via b7-d5-f3-
This is, in principle, very similar to: Print all paths from root to leaf in a Binary tree which I read and used to get as far as I have gotten, but I'm missing something probably very simple... 从原则上讲,这非常类似于: 在二叉树中打印从根到叶的所有路径,该路径我已经阅读并用来达到我所学到的范围,但是我缺少的东西可能非常简单...
Any ideas? 有任何想法吗?
In this loop 在这个循环中
for(Move j:simpleJ) {
jump.addJumpSquare(j.jumped.get(0)); // add the jumped square to the jump path
possibleJumps(j.to, b.doMove(j), new Move(jump), moves);
}
you are reusing the same jump
for all possible moves - if you got 3 possible moves x,y,z you will end up with x, y and z added to the jump path. 您对所有可能的动作都重复使用相同的jump
-如果您获得3个可能的动作x,y,z,则最终会将x,y和z添加到跳转路径中。
So you either have to 所以你要么必须
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.