Results 1 to 3 of 3
Thread: a complicated ArrayList function
 05272014, 08:00 PM #1Member
 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 MoveCalculation.
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 result.add(m); // add to result remove.add(n); // remove chained node } } if (!result.contains(m)) { // sole move, add to results result.add(m); } } } // remove deleted nodes that made it into the result list (due to // arbitrary ordering) result.removeAll(remove); return new ArrayList<Move>(result); }
Thanks a lot!
 05272014, 10:50 PM #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?If you don't understand my response, don't ignore it, ask a question.
 05272014, 11:03 PM #3Member
 Join Date
 May 2014
 Posts
 2
 Rep Power
 0
Re: a complicated ArrayList function
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.
Similar Threads

Create a function that sorts an arraylist of objects
By Stavrosgr in forum New To JavaReplies: 2Last Post: 12032012, 07:33 PM 
Return ArrayList function  Java
By skp123 in forum New To JavaReplies: 23Last Post: 05012011, 05:11 AM 
Help in test function for ArrayList
By swalf in forum New To JavaReplies: 5Last Post: 02182011, 07:14 PM 
Trying to create an undo function using an arraylist.
By Spirit356 in forum New To JavaReplies: 6Last Post: 04272010, 08:01 PM 
Search function for ArrayList?
By javanoobie in forum New To JavaReplies: 11Last Post: 04172009, 09:38 PM
Bookmarks