# Game of fifteen is is somewhat randomly printing extra lines of numbers and deeming moves illegal

Game of fifteen is 1) printing an extra line each time a move is labeled illegal and 2) somewhat randomly labeling moves illegal (e.g. it will swap to the left once but not twice, and up/down etc. sometimes but not all of the time). Init and draw seem to be working fine since the board prints alright.

``````bool move(int tile)
{
int r;
int c;
for(r = 0; r < d; r++)
{
for(c = 0; c < d; c++)
{
if(tile == 0)
{
return false;
}
// from github
if(tile == board[r][c])
{
if((r - 1) >= 0 && board[r-1][c] == 0)
{
int tmp = board[r][c];
board[r][c] = board[r-1][c];
board[r-1][c] = tmp;
return true;
}
else if((r + 1) <= (d - 1) && board[r + 1][c] == 0)
{
int tmp = board[r][c];
board[r][c] = board[r + 1][c];
board[r + 1][c] = tmp;
return true;
}
else if ((c - 1) >= 0 && (board[r][c - 1] == 0))
{
int tmp = board[r][c];
board[r][c] = board[r][c - 1];
board[r][c - 1] = tmp;
return true;
}
else if((c + 1) >= (d - 1) && (board[r][c + 1] == 0))
{
int tmp = board[r][c];
board[r][c] = board[r][c + 1];
board[r][c + 1] = tmp;
return true;
}
}
}
}
return false;
}

bool won(void)
{
for (int r = 0; r < d; r++)
{
for (int c = 0; c < d; d++)
{
if(board[r][c] > board[r][c+1])
{
return false;
}
if(board[r][c] > board[r+1][c])
{
return false;
}
}
}
return true;
}
``````
• format your code properly and i'll take a look! – Zach Arundel Jun 26 '17 at 15:04

`(c + 1) >= (d - 1)` should be `(c + 1) <= (d - 1)`
• That sounds like your `won` function returns `true` when it shouldn't. – Blauelf Jun 28 '17 at 11:25