# Calculating movement help! (Advance Wars / FF Tactics like movemen

Page 1 of 1

## 1 Replies - 401 Views - Last Post: 16 May 2014 - 01:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=347016&amp;s=1a0fd770cc7eb0eac01b91e2efe1ce19&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 SupremeGrandRuler

Reputation: 0
• Posts: 32
• Joined: 10-May 14

# Calculating movement help! (Advance Wars / FF Tactics like movemen

Posted 15 May 2014 - 07:28 PM

Alright, so I'm having problems with lines 11 - 55.
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.

```	@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) {

System.out.println("Final: " + endingColumn + endingRow);
}
}
}

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;
}
}
}
}
}
}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Calculating movement help! (Advance Wars / FF Tactics like movemen

### #2 SupremeGrandRuler

Reputation: 0
• Posts: 32
• Joined: 10-May 14

## Re: Calculating movement help! (Advance Wars / FF Tactics like movemen

Posted 16 May 2014 - 01:55 AM

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