Need Help C++

closed account (Doj23TCk)
I need help with the array and trying to figure out this game. But with the code I need to figure out how to only make it move only up, down, left and right. Rather than moving it anywhere on the grid.

This is the part of the code where you change it and make it so it only moves up, down, left and right.

void takeMove(int grid[4][4], int click_row, int click_col)
{
int z_row, z_col;
findZero(grid, &z_row, &z_col);
//needs work, fix so only legal moves allowed
grid[z_row][z_col] = grid[click_row] [click_col];
grid[click_row][click_col] = 0;

}
You problem description is somewhat hard to understand. What it sounds like is you have a game where there is an icon on a grid. The player can click on the grid, and the icon moves. You want the icon to only move in one direction. Is my understanding correct?

What I would do:

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

3. Whichever is greater, that is the direction you will be moving.

Let's say, for the sake of discussion, that X is greater, so we will be moving right or left.

If you can move as many spaces as you want at a time, then keep the Y coordinate constant and change the X coordinate to the clicked X coordinate.

If you can only move 1 space at a time, then figure out if the clicked X is greater or less than the current X. If it is, then increment your X coordinate by 1. Otherwise, decrement your X coordinate by 1.

You also need to make sure that you don't go past your boundaries. When you calculate the new location, always make sure it is a legal location.
closed account (Doj23TCk)
Ya, that's correct. So then we should do like a if and else saying that if gridclick is = to something. Then z_row is moved +1?
Something like:

1
2
3
4
if (clickRow > z_row)
{
   z_row += 1;
}


This calculates what the new coordinates will be. You also need to figure out what happens at the old coordinates and what happens at the new coordinates. Things that happen at the old coordinates need to happen to grid[x_row][z_col] before you assign the new z_col value. If you need to have access to both the new and old coordinates at the same time, you will need to create some temporary values.
closed account (Doj23TCk)
I got it to this now but it doesn't seem like it wants to work or anything.

void takeMove(int grid[4][4], int click_row, int click_col)
{
int z_row, z_col;

findZero(grid, &z_row, &z_col);
//needs work, fix so only legal moves allowed


if(click_row = z_row + 1, click_col = z_col);
else if(click_row = z_row - 1, click_col = z_col){
}else if(click_row = z_row, click_col = z_col + 1){
}else if(click_row = z_row, click_col = z_col + 1){
}else(click_row = z_row, click_col = z_col);

grid[z_row][z_col] = grid[click_row] [click_col];
grid[click_row][click_col] = 0;

}


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.
Topic archived. No new replies allowed.