Results 1 to 3 of 3
  1. #1
    Mav3N is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default 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
    						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);
    	}
    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. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.

  3. #3
    Mav3N is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: a complicated ArrayList function

    Quote Originally Posted by Norm View Post
    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.

Similar Threads

  1. Create a function that sorts an arraylist of objects
    By Stavrosgr in forum New To Java
    Replies: 2
    Last Post: 12-03-2012, 07:33 PM
  2. Return ArrayList function - Java
    By skp123 in forum New To Java
    Replies: 23
    Last Post: 05-01-2011, 05:11 AM
  3. Help in test function for ArrayList
    By swalf in forum New To Java
    Replies: 5
    Last Post: 02-18-2011, 07:14 PM
  4. Trying to create an undo function using an arraylist.
    By Spirit356 in forum New To Java
    Replies: 6
    Last Post: 04-27-2010, 08:01 PM
  5. Search function for ArrayList?
    By javanoobie in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 09:38 PM

Posting Permissions

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