0

I'm writing a simple checkers game in 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). 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?

Community
  • 1
  • 1
jackthehipster
  • 978
  • 8
  • 26

1 Answers1

1

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.

So you either have to

  1. create a new jump-object (with appropriate state) at each iteration or
  2. remove the added square at the end of the iteration
piet.t
  • 11,718
  • 21
  • 43
  • 52
  • Well, that's what I first thought, too, and that's why I create a new Move-object in the recursive call (with new Move(jump)). And removing the added square after the call doesn't do the trick either... it only gives me another wrong path:found jumpa8c2 via b7-b5-b3- found jumpa8c2 via b5-d5-d3- found jumpa8g2 via d5-d3-f3- – jackthehipster Dec 10 '13 at 14:28
  • Maybe I should explain that one jump can have several squares, and a jump is only one complete move if all squares that CAN be jumped are in fact jumped. And because there can be many paths to jump, each single path is one possible move... I hope this is clear enough. But my desired output should help to understand what I mean :) – jackthehipster Dec 10 '13 at 14:31
  • I guess you were right indeed. This loop works: for(Move j:simpleJ) { Move nj = new Move(jump); nj.addJumpSquare(j.jumped.get(0)); possibleJumps(j.to, b.doMove(j), nj, moves); } ... even though I don't quite understand why :-) – jackthehipster Dec 10 '13 at 14:44
  • 1
    In case of 3 possible moves you are repeating the loop-body three times, and in your original code you added all three moves to the same jump-object while you would want to start a new branch for each possible move - and you have to do this before adding the next square... – piet.t Dec 10 '13 at 14:50
  • Yeah, I need a new move BEFORE I add the square, that's right! Thanks a lot, this has bothered me forever! – jackthehipster Dec 10 '13 at 14:53