I have been working on the game of fifteen problem and seem to be having an issue. I keep getting an "Illegal Move" when it is a legal move. I think I have found part of the issue and I would just like some guidance on why this is happening.
Here is my code to get the tile location:
//Find tile location
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] == tile)
{
x = i;
y = j;
}
}
}
and my board would look something like this
8 7 6
5 4 3
2 1 _
I when the tile = 3 I would expect the x,y to be 2,1 correct? But when I use debug50 to check it out, it gives me 1,2 every time.
However, when I use this to get my blank space:
//Find blank location
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] == blankval)
{
blankx = i;
blanky = j;
}
}
}
And it gives me the correct coordinates of 2,2.
Do I have this backwards and can't tell because the blank coordinates would be 2,2 either way?
Any help is appreciated, thank you!
EDIT: Init()
void init()
{
//get max tile value
int value = (d*d) - 1;
//check if board is even
if((d % 2) == 0)
{
//create board
//for each row
for(int i = 0; i < d; i++)
{
//for each column
for(int j = 0; j < d; j++)
{
//if value is 2 and board is even, then the 2 and 1 values need swapped
if(value == 2)
{
board[i][j] = 1;
value--;
}
else if(value == 1)
{
board[i][j] = 2;
value--;
}
//else fill board normally
else
{
//set value for tile
board[i][j] = value;
value--;
}
}
}
}
else //board is odd
{
//create board
//for each row
for(int i = 0; i < d; i++)
{
//for each column
for(int j = 0; j < d; j++)
{
//set value for tile
board[i][j] = value;
value--;
}
}
}
board[d - 1][d - 1] = d*d; //set blank value
}