I've almost finished "fifteen". It works: outputs correct board from 3x3 to 9x9 with 'init'
void init(void)
{
int k = (d * d) - 1;
for (int j = 0; j < d; j++)
{
for (int i = 0; i < d; i++)
{
board[i][j] = k;
k--;
}
}
if (d % 2 == 0)
{
board[d - 2][d - 1] = 2;
board[d - 3][d - 1] = 1;
}
} & 'draw' functions:
void draw(void)
{
for (int j = 0; j < d; j++)
{
for (int i = 0; i < d; i++)
{
printf("%3i ", board[i][j]);
}
printf("\n\n");
}
}
It checks won with 3x3 & 4x4 tests,
bool won(void)
{
int res = 1;
for (int j = 0; j < d; j++)
{
for (int i = 0; i < d; i++)
{
if(board[i][j] == res)
{
res++;
}
else if (res == d * d && board[i][j] == 0)
{
return true;
}
}
}
return false;
}
& swaps moving
bool move(int tile)
{
for (int j = 0; j < d; j++)
{
for (int i = 0; i < d; i++)
{
if (board[i][j] == 0 && board[i - 1][j] == tile)
{
board[i - 1][j] = 0;
board[i][j] = tile;
return true;
}
else if (board[i][j] == 0 && board[i + 1][j] == tile)
{
board[i + 1][j] = 0;
board[i][j] = tile;
return true;
}
else if (board[i][j] == 0 && board[i][j - 1] == tile)
{
board[i][j - 1] = 0;
board[i][j] = tile;
return true;
}
else if (board[i][j] == 0 && board[i][j + 1] == tile)
{
board[i][j + 1] = 0;
board[i][j] = tile;
return true;
}
}
}
return false;
}
but still cannot pass through check50 with mistakes below
I suspect a logical error in one of the loops, but I can not catch it in any way. Will you poke my nose where to look for?