Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Angry [SOLVED] Calculating movement help! (Advance Wars / FF Tactics like movement).

    Alright, so I'm having problems with lines 11 - 53.
    I'm trying to calculate the possible tiles the clicked unit can be moved on and display it to the player.

    The field is a 9 (columns) by 5 (rows) grid. A unit with can move tiles horizontally or vertically based on its movement.
    Example: (unit = o; possible tiles = x) (assuming movement of 2).

    [ ][ ][ ][ ][x][ ][ ][ ][ ]
    [ ][ ][ ][x][x][x][ ][ ][ ]
    [ ][ ][x][x][o][x][x][ ][ ]
    [ ][ ][ ][x][x][x][][ ][ ]
    [ ][ ][ ][ ][x][ ][ ][ ][ ]

    I tried to calculate movement by finding out what column the unit is in, subtracting the column it wants to go to, multiplying the difference by *-1 if the difference is less than 0. Then, doing the same with rows and adding the difference of columns to the difference of rows and comparing it to the unit's movement.
    Unfortunately, with my code, it seems to move in any directions.

    Java Code:
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		if (Main.cards.size() > 0) {
    			mainloop: for (int i = 0; i < Main.cards.size(); i++) {
    				Card c = (Card) Main.cards.get(i);
    				int startingColumn = 0;
    				int startingRow = 0;
    				int endingColumn = 0;
    				int endingRow = 0;
    
    				// If card is not selected:
    				if (Card.cardSelected != c || Card.cardSelected == null) {
    					if (new Rectangle(c.x, c.y, Main.maxTileSize, Main.maxTileSize).contains(e.getX(), e.getY())) {
    						Card.cardSelected = c;
    
    						// Calculate valid moves:
    						validfromloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
    							for (int k = 0; k < Main.BOARD_ROWS; k++) {
    								if (AssetLoader.TILE[j][k].contains(Card.cardSelected.x, Card.cardSelected.y)) {
    									startingColumn = j;
    									startingRow = k;
    
    									System.out.println("VALID--Starting column: " + startingColumn + " Starting Row: " + startingRow);
    
    									break validfromloop;
    								}
    							}
    						}
    
    						System.out.println("validtoloop");
    						validtoloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
    							for (int k = 0; k < Main.BOARD_ROWS; k++) {
    								if (startingColumn - j < 0) {
    									endingColumn = (startingColumn - j) * -1;
    
    									System.out.println("Ending column: " + endingColumn);
    								}
    								if (startingRow - k < 0) {
    									endingRow = (startingRow - k) * -1;
    
    									System.out.println("Ending row: " + endingRow);
    									System.out.println("---------------------");
    								}
    								if (endingColumn + endingRow <= Card.cardSelected.mvt) {
    									AssetLoader.VALID_MOVE[j][k] = true;
    								}
    							}
    						}
    
    						Main.frame.repaint();
    
    						System.out.println("Card selected: " + Card.cardSelected);
    
    						break mainloop;
    
    					}
    					// If card is selected:
    				} else if (Card.cardSelected == c && Card.cardSelected != null) {
    
    					for (int l = 0; l < Main.BOARD_COLUMNS; l++) {
    						for (int m = 0; m < Main.BOARD_ROWS; m++) {
    							AssetLoader.VALID_MOVE[l][m] = false;
    						}
    					}
    
    					fromloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
    						for (int k = 0; k < Main.BOARD_ROWS; k++) {
    
    							System.out.println("loop1");
    
    							if (AssetLoader.TILE[j][k].contains(Card.cardSelected.x, Card.cardSelected.y)) {
    								startingColumn = j;
    								startingRow = k;
    
    								System.out.println("loop1a");
    
    								break fromloop;
    							}
    						}
    					}
    
    					toloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
    						for (int k = 0; k < Main.BOARD_ROWS; k++) {
    							if (AssetLoader.TILE[j][k].contains(e.getX(), e.getY())) {
    								endingColumn = j;
    								endingRow = k;
    
    								System.out.println("loop2");
    
    								if ((startingColumn + startingRow) - (endingColumn + endingRow) <= Card.cardSelected.mvt && (startingColumn + startingRow) - (endingColumn + endingRow) >= -Card.cardSelected.mvt) {
    									Card.cardSelected.x = (int) AssetLoader.TILE[j][k].getX();
    									Card.cardSelected.y = (int) AssetLoader.TILE[j][k].getY();
    									Card.cardSelected = null;
    									;
    
    									Main.frame.repaint();
    
    									// System.out.println("Card selected: " +
    									// c.cardSelected + " Selection: " +
    									// Card.classCardSelected);
    									System.out.println("Card x / y: " + c.x + " / " + c.y);
    									System.out.println("Click x / y: " + e.getX() + " / " + e.getY());
    									System.out.println("Unit moving from " + startingColumn + startingRow + " to " + endingColumn + endingRow);
    
    									break mainloop;
    								} else {
    									Card.cardSelected = null;
    
    									Main.frame.repaint();
    
    									System.out.println("Unit cannot move so far.");
    
    									break toloop;
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    Last edited by supremegrandruler; 05-16-2014 at 10:54 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    If the number of moves is 4, would it be allowed to end up where the thing started? (e.g. move North, East, South, West). Or is every position where the thing has been before (during the move) tabu?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Quote Originally Posted by JosAH View Post
    If the number of moves is 4, would it be allowed to end up where the thing started?

    kind regards,

    Jos
    Technically, yes . However, in the future I plan to make it so that if the card is clicked again after being selected, it will deselect it. Also there was a small error in another part of the code so I just updated it.
    Last edited by supremegrandruler; 05-15-2014 at 05:05 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    ... and if the number of moves is 2, would it be allowed to end up where the thing started? (e.g. move North, South).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Quote Originally Posted by JosAH View Post
    ... and if the number of moves is 2, would it be allowed to end up where the thing started? (e.g. move North, South).

    kind regards,

    Jos
    Technically yes. A unit can move 1 tile horizontally or vertically. This process can be repeated an amount of times equal the the unit's movement value.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Ok, what is the problem then? Enumerate all possible directions for 'n' moves and see where your 'thing' ends ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Did you read the OP? That's what I tried to do but to no avail.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Oh dear, enumerating the possible moves is easy: use two bits for a move, i.e. 00= North, 01= East, 10= South, 11= West; if you are allowed to make four moves, e.g. 01011110 == East, East, West, South. All possible combinations of four moves is the sequence 00000000 ... 11111111 or the numbers 0 ... 255. So a simple loop can generate all possible sequences of four moves. Keep a set of (int, int) pairs ready and stick in the coordinates where you end up for each sequence of moves; at the end the Set contains all coordinates where you can end up after the moves have been made. If the result is 20 lines of code or more, I'd be surprised ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    I'll look into your suggestion but what I don't understand is why my code isn't working.

    Take a look at this grid: (yes, i know java is 0,0 in the top left).

    Calculating movement help! (Advance Wars / FF Tactics like movement).-4_coord_what1.gif

    Now, if my unit is say at 4,4 and wants to move to 3,3, the way I do it right now is like this:
    - take the unit's starting column (4) and subtract the column it wants to go to from it (3). 4-3 = 1. (if the result is negative, multiply by -1.)
    - take the unit's starting row (4) and subtract the row it wants to go to from it (3). 4-3 = 1. (if the result is negative, multiply by -1.)
    - Add the result of both operations together. 1 + 1 = 2
    - Compare the final result (2) to the unit's movement. If the final result is equal to or less than the unit's movement, then it is a valid move. If not, it's invalid.

    More examples (assuming movement of 2):
    U(5,1) -> (6,6):
    a)5-1 = -1; -1 * -1 = 1
    b) 1-6 = -5; -5 * -1 = 5
    c) 1+5 = 6; 6 <= 2(unit's movement) invalid move

    U(2,4) -> (5,5) (assuming movement of 4):
    a) 2 - 5 = -3; -3 * -1 = 3
    b) 4-5 = -1; -1 * -1 = 1
    c) 3+1 = 4; 4 <= 4 (unit's movement) valid move

    U(2,4) -> (3,3) (assuming movement of 4):
    a) 2 - 3 = -1; -1 * -1 = 1
    b) 4 - 3 = 1
    c) 1 + 1 = 2; 2 <= 4 (unit's movement) valid move
    Last edited by supremegrandruler; 05-16-2014 at 09:26 AM.

  10. #10
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Figured the answer out. It's because the the 'to' column is only subtracted from the 'from' column if the result is less than 0. Otherwise, no operation takes place. Same with rows.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Are you going to stick to your solution? I find it a bit lenghtly and tiresome ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  12. #12
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    TBH, I understand what you're suggesting but I'm not sure how to go about doing it. Also, movement could be anywhere from 0+.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    I did it for you because I can't stand inefficient solutions; because this is spoonfeeding, no further comments. The method returns a Set of all reachable points, starting from a point xorig, yorig. The method makes 'hops' moves. Do with it what you want:

    Java Code:
    	// directions North, East, South and West
    	private static final int[] DX= { 0, 1, 0, -1 };
    	private static final int[] DY= { 1, 0, -1, 0 };
    	
    	// size of the grid
    	private static final int GRIDX= 10;
    	private static final int GRIDY= 10;
    	
    	// return all destination positions
    	private static Set<Point> getPositions(int xorig, int yorig, int hops) {
    		
    		Set<Point> dest= new HashSet<Point>();
    		
    		// generate all positions
    		offgrid:
    		for (int moves= 1 << 2*hops; moves-- > 0;) { // for all possible moves, 'hops' times
    			
    			int x= xorig, y= yorig;
    			for (int h= 0, m= moves; h < hops; h++, m>>= 2) { // do all moves
    				x+= DX[m&0x03];
    				y+= DY[m&0x03];
    				if (x < 0 || y < 0 || x >= GRIDX || y >= GRIDY) continue offgrid;
    			}
    			dest.add(new Point(x, y));
    		}
    		return dest;
    	}
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  14. #14
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Thanks. I never knew you could do stuff like that.

    What's this though?

    x+= DX[m&0x03];
    y+= DY[m&0x03];

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Quote Originally Posted by supremegrandruler View Post
    Thanks. I never knew you could do stuff like that.

    What's this though?

    x+= DX[m&0x03];
    y+= DY[m&0x03];
    That is a single move; you figure it out ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Never seen a mod with so much attitude before. Guess there's a first time for everything, eh?
    Btw, if people don't want to explain the question but they still want to be helpful, they post a link to a tutorial or something.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Quote Originally Posted by supremegrandruler View Post
    Never seen a mod with so much attitude before. Guess there's a first time for everything, eh?
    Btw, if people don't want to explain the question but they still want to be helpful, they post a link to a tutorial or something.
    It's not my attitude, it's your laziness; those statements you don't seem to understand only involve simple array access and the bitwise-and operator; you could've looked them up if you really wanted to understand them but you wanted to be spoonfed; I gave you the method as well as an implementation; what more do you want?

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  18. #18
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Quote Originally Posted by JosAH View Post
    It's not my attitude, it's your laziness; those statements you don't seem to understand only involve simple array access and the bitwise-and operator; you could've looked them up if you really wanted to understand them but you wanted to be spoonfed; I gave you the method as well as an implementation; what more do you want?

    Jos
    Eh actually when you described the solution, I understood what you meant but had no idea how to go about implementing it, which is why I'm posting in the new to java forum.
    Then, you nicely :) coded the answer and honestly, before you posted it, I had no idea you could do those things with loops (like continue and stuff), and the arraylist for points didn't even cross my mind (because I'm new to Java and can't think of everything, even if it's basic stuff, unfortunately).

    What I don't understand now, is only the &0x03. That's the only thing. I was looking into it when I thought "why not ask?". I guess I'll look into it myself.

    Thanks for the help. You answered my OP and showed me some cool stuff I didn't know before.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Quote Originally Posted by supremegrandruler View Post
    Eh actually when you described the solution, I understood what you meant but had no idea how to go about implementing it, which is why I'm posting in the new to java forum.
    Then, you nicely :) coded the answer and honestly, before you posted it, I had no idea you could do those things with loops (like continue and stuff), and the arraylist for points didn't even cross my mind (because I'm new to Java and can't think of everything, even if it's basic stuff, unfortunately).

    What I don't understand now, is only the &0x03. That's the only thing. I was looking into it when I thought "why not ask?". I guess I'll look into it myself.

    Thanks for the help. You answered my OP and showed me some cool stuff I didn't know before.
    You were using labeled breaks yourself (e.g. see line #25 in your original code); if you can use labeled breaks, you can use labeled continues ... Looking up (and unerstanding) what a bit-wise and does, would've shown that the expression takes a single move from a sequence of moves represented as a series of bits (a simple int).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  20. #20
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

    Actually, I read about 3-4 tutorials on Java basics (loops, ifs, etc. inc. Java docs) and 3-4 game making tutorials and none of them mentioned named loops or named breaks or continues. (I'm sure the Java docs covers it at some point, but I haven't reach that point yet.) The reason I know about labelled breaks is because I googled something like "how break outerloop from innerloop java". That search led me to a forum post with some guy saying you can do labeled breaks.

Page 1 of 2 12 LastLast

Similar Threads

  1. Movement not working...
    By RiokuTheSlayer in forum New To Java
    Replies: 7
    Last Post: 05-27-2013, 12:47 AM
  2. Movement
    By ChaoticXknight in forum New To Java
    Replies: 2
    Last Post: 03-06-2013, 08:38 PM
  3. Replies: 2
    Last Post: 03-10-2012, 04:26 AM
  4. Movement
    By elamre in forum Java Applets
    Replies: 24
    Last Post: 01-18-2012, 08:44 PM
  5. Checkers Movement
    By Hollowsoul in forum Advanced Java
    Replies: 14
    Last Post: 08-28-2011, 08:12 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
  •