I would really appreciate any input into what I must definitely be doing wrong. I am trying to test the code out by myself before entering it into the ide. Instead of writing the tile number in the new position, the tile is replaced with '0', and it wont move the first element. I am not sure why this is happening:
./my_fifteen 3
8 7 6
5 4 3
2 1 0
Which tile to move?3
found at board[1][2]
[p_row][p_col] = [1][2]
3
8 7 6
5 4 0
2 1 0
Which tile to move?6
found at board[0][2]
[p_row][p_col] = [0][2]
6
8 7 0
5 4 0
2 1 0
Which tile to move?7
found at board[0][1]
[p_row][p_col] = [0][1]
7
8 0 0
5 4 0
2 1 0
Which tile to move?8
found at board[0][0]
[p_row][p_col] = [0][0]
8
8 0 0
5 4 0
2 1 0
Which tile to move?^C
My code is:
void draw(void)
{
//swapping the position of the n - 3 and n - 2 elements,
//and making sure n - 1 is null or something..
if (!(array & 1)) //if the value array is NOT odd;
{
tmp = board[row - 1][column - 3];
board[row - 1][column - 3] = board[row - 1][column - 2];
board[row - 1][column - 2] = tmp;
}
//board[row - 1][column - 1] = '\0';
//generating 2d array:
for (row = 0; row < array; row++)
{
for (column = 0; column < array; column++)
{
printf("%d\t", board[row][column]);
}
printf("\n");
}
}
void clear(void)
{
//clearing the screen with ANSI escape sequences;
printf("\033[2J");
printf("\033[%d;%dH", 0, 0);
}
int move(int tile)
{
//How to find the selected tile position?
bool found = false;
for (row = 0; row < array; row++)
{
for (column = 0; column < array; column++)
{
if (board[row][column] == tile)
{
found == true;
printf("found at board[%i][%i]\n", row, column);
//store the position
p_row = row;
p_col = column;
printf("[p_row][p_col] = [%i][%i]\n", p_row, p_col);
printf("%i\n", board[p_row][p_col]);
break;
}
}
}
//upward
if (p_row > 0) //dont go above the array
{
//if tile above tile[p_row][p_col] holds zero
if (board[row - 1][column] == 0)
{
int holdspace = board[p_row][p_col]; //store tile value in holdspace,
board[p_row][p_col] = board[row - 1][column]; //store uptile value in tile,
board[row - 1][column] = holdspace; //store holdspace value in uptile
return true;
}
}
//downward
if (p_row < array - 1) //dont go below the array
{
if (board[row + 1][column] == 0)
{
int holdspace = board[p_row][p_col]; //store tile value in holdspace,
board[p_row][p_col] = board[row + 1][column]; //store downtile value in tile,
board[row + 1][column] = holdspace; //store holdspace value in downtile
return true;
}
}
//rightward
if (p_col < array - 1) //dont go to the right of the array
{
if (board[row][column + 1] == 0)
{
int holdspace = board[p_row][p_col]; //store tile value in holdspace,
board[p_row][p_col] = board[row][column + 1]; //store righttile value in tile,
board[row][column + 1] = holdspace; //store holdspace value in righttile
return true;
}
}
//leftward
if (p_col > 0) //dont go to the left of the array
{
if (board[row][column - 1] == 0)
{
int holdspace = board[p_row][p_col]; //store tile value in holdspace,
board[p_row][p_col] = board[row][column - 1]; //store lefttile value in tile,
board[row][column - 1] = holdspace; //store holdspace value in lefttile
return true;
}
}
}