0

for pset3 my move function is the following:

int j = 0;
for(int i = 0; j < d; i++) {
    if(i == d) {
        i = 0;
        j++;
    }
    if(board[i][j] == tile) {
    printf("%i, %i", i, j);
    //swap left
        if(board[i][j] == board[xpos][ypos - 1]) {
            swap(xpos, ypos, i, j);
            board[i][j + 1] = tile;
            return true;
            printf("l");
        }

(same idea for moving right, up, or down). Here is the swap function:

void swap(int xpos, int ypos, int i, int j) {
xpos = i;
ypos = j;
board[i][j] = 0;
}

How do i make it so that the xpos and ypos are retained and arn't reset to d - 1 the next time the function runs?

2 Answers 2

1

You'll need to declare xpos and ypos as global variables, initialize them and not changing their values until you really need to.

Global variables are accessed from all the functions in your program. They never go out of scope until your program ends execution unlike local variables which might go out of scope after a code block, a loop, a condition or a function depending on where they are declared in your program.

To declare xpos and ypos as global variables, you'll need to write something like this

#include <stdio.h>

// prototypes
void foo(void);

// global variables
int xpos;
int ypos;

int main(void)
{
    // we may initialize them here
    xpos = 20;
    ypos = 10;
}

void foo(void)
{
    // and update their values here
    xpos = 10;
    ypos++;
}

Global variables can also be initialized when they're declared, but it's a good practice to declare and initialize variables only when needed.

Update: I don't really see any point of your swap() function. In fact, it doesn't do any swapping at all. I assume, you're trying to swap a tile at position (i, j) with the blank tile at position (xpos, ypos).

You don't really need to pass xpos and ypos as arguments to swap() in this case since they're already accessible from swap(). That is, you can refer to them with their names (use them) directly within the function.

To "really" swap a tile at (i, j) with the blank tile at (xpos, ypos), you may have something like this

// swap
board[xpos][ypos] = board[i][j];
board[i][j] = BLANK; // where BLANK is the value of the blank tile

// update the position of the blank tile
xpos = i;
ypos = j;
4
  • the values of xpos and ypos are still not changing
    – user1475
    Jun 26, 2014 at 3:57
  • @user1475 you may update your question and include some code! ;)
    – kzidane
    Jun 26, 2014 at 4:01
  • so the values of numbers touching 0 are able to be changed but they do not become board[xpos][ypos]
    – user1475
    Jun 26, 2014 at 4:12
  • 1
    @user1475 updated my answer! :)
    – kzidane
    Jun 26, 2014 at 4:21
0

One approach would be to make xpos and ypos global variables whose initial value is d-1. Then they will retain their values no matter how many times you call your function.

The other option would be to make the addresses of xpos and ypos parameters of your function (they would have to be initialized outside the function).

1
  • I want them to change but be retained after the function until they are actualy redefined in it again
    – user1475
    Jun 26, 2014 at 4:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .