1. Member
Join Date
Mar 2014
Posts
72
Rep Power
0

[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++) {
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) {
}
}
}

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++) {
}
}

fromloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
for (int k = 0; k < Main.BOARD_ROWS; k++) {

System.out.println("loop1");

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++) {
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 = 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 11:54 AM.

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

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

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

Originally Posted by JosAH
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 06:05 PM.

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

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

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

Originally Posted by JosAH
... 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. 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

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

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

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

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

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 10:26 AM.

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

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

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

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. 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;
}
}
return dest;
}```
kind regards,

Jos

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

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. Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

Originally Posted by supremegrandruler
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

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

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. Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

Originally Posted by supremegrandruler
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

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

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

Originally Posted by JosAH
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. Re: Calculating movement help! (Advance Wars / FF Tactics like movement).

Originally Posted by supremegrandruler
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

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

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 Last

Posting Permissions

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