Wow. It looks like a couple of posts just disappeared. I thought you posted yesterday afternoon and I responded later in the afternoon, but those posts seem to have vaporized. Oh well.
By the way, when you post code, please click the Format button that looks like "<>" and paste your code between the tags. This will make it easier to read and comment on your code.
Let's start back at the beginning. Make sure you are doing things step by step. Be as clear as possible.
1. Determine the X distance between the current location and the clicked location (absolute value of the difference of the X coordinates.
2. Determine the Y distance |
1 2
|
int colDelta = abs(click_col - z_col);
int rowDelta = abs(click_row - z_row);
|
3. Whichever is greater, that is the direction you will be moving. |
bool colMovement = (colDelta > rowDelta);
After you determine which direction you are moving, calculate next location:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
int newCol, new Row; // new coordinates
if (colMovement == true)
{
// we are moving to a new column
newRow = z_row; // The row stays the same
if (click_col > z_col)
{
// click was to the right of current location, so move 1 column to the right
newCol = z_col + 1;
}
else
{
// click was to the left of current location, so move 1 column to the left
newCol = z_col - 1;
}
}
else
{
// we are moving to a new row
newCol = z_col; // The column stays the same
if (click_row > z_row)
{
// click was row greater than current row, so increase row
newRow = z_row + 1;
}
else
{
// click was row less than current row, so decrease row
newRow = z_row - 1;
}
}
|
When you do these steps you will now have the current location (z_col, z_row) and the next location (newCol, newRow). You can do whatever you want with these coordinates.
Note: This algorithm does not account for clicking on the current location. If you click on the current location, colMovement will be false and newRow will be decremented. You can figure out how to handle this situation.
You also need to validate the new location. If the new location is outside the boundaries of the grid, you must not do the move.