# Thread: a complicated ArrayList function

1. Member
Join Date
May 2014
Posts
2
Rep Power
0

## a complicated ArrayList function

Hi there,

i have been working on this function for 5 hours now, and i can't get it to work. It's a little bit complicated so let me first explain what this is about:

1. As a little exam in my studies i have to program a halma console game with KI an stuff.
2. Everything is finished and works except for the Move-Calculation.
3. A Move is a Move from Field a to Field b which a player can perform in one round based on the game rules (Careful: We are not using the standard halma rules, we use different ones).
4. Class Move consists of the Starting Cell where the Figure before the Move stands and a target Cell where the figure will stay on at the Move end. It may include an Array of serveral Cells, the stop cells which the figure passes from start -> target since several jumps can be performed in one Move.

Valid Examples for Moves:
a -> b (From Field a to Field b)
d -> f through g,h,i (From Field d to Field f through the Field g,h,i

My Move Calculation where all possible Moves are calculated for a given figure consists of 2 parts. An expand() function which will generate all possible moves (works perfectly) and a jumpFix() function which isn't working properly. Example:

After expand() I'm getting something like that:
a -> b
b -> c
c -> d
s -> t

This is however not finished, because the first 3 Moves are actually 1 Move. Because the player can Move from a -> d in one turn because those 3 are consecutive Move. The fixed Move would look like that:

a -> d through b,c

jumpFix() is also perfectly working for that situation, however there is one specific situation when it doesn't work. Let's say we have this situation.

a -> b
b -> c
c -> d
b -> e
e -> f
f -> g
s -> t

Then the only valid jumpFix() output would be:

a -> d through b,c
a -> g through b,e,f
s -> t

However i can not get it to work yet. Somebody got an idea? Note: It definitely needs to be iterative, not rekursive else i would get an StackoverflowError.

This is my current Code of jumpFix():

Java Code:
```/**
* Takes a look into all calculated moves and finds those which should be seen as one move, but are still
* considered to be more than one move (several jumps in one move)
*/
public static List<Move> jumpFix(List<Move> moves) {

Set<Move> result = new HashSet<Move>();
Set<Move> remove = new HashSet<Move>();

int lastSize = -1;

// repeat action until no moves could be merged
while (lastSize != remove.size()) {

lastSize = remove.size();

for (Move m : moves) {

if (remove.contains(m)) {
// move already in delete list, skip
continue;
}

for (Move n : moves) {

if (m == n) {
// don't chain yourself, skip
continue;
}

if (remove.contains(n)) {
// move already in delete list, skip
continue;
}

if (m.contigious(n)	&& !m.contains(n)) {
// Consecutive Move found
result.remove(m); // remove from set to avoid hashing conflicts
m.chain(n); // chain moves
}
}

if (!result.contains(m)) {
// sole move, add to results
}
}
}

// remove deleted nodes that made it into the result list (due to
// arbitrary ordering)
result.removeAll(remove);

return new ArrayList<Move>(result);
}```
If anybody has a good idea how to implement the special case where a Move splits into 2 or more branches and jumpFix() able to handle this case, i would be very glad. I spent 5 hours on that and can not figure it out at the Moment.

Thanks a lot!

2. ## Re: a complicated ArrayList function

How are you trying to debug the code? If you know what the code should be doing, looking at the values of the variables as it executes should help you understand what is wrong.

Or is this a finding the right algorithm problem?

3. Member
Join Date
May 2014
Posts
2
Rep Power
0

## Re: a complicated ArrayList function

Originally Posted by Norm
How are you trying to debug the code? If you know what the code should be doing, looking at the values of the variables as it executes should help you understand what is wrong.

Or is this a finding the right algorithm problem?
The algorithm is doing what it is programmed for. However when we programmed that function we didn't think of that special case where a Move could split into 2 different Moves. So yes, finding the right algorithm is the problem. I can not think of an iterative way to do it right now. Any help would be appreciated.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•