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;