0

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;
}
1
  • format your code properly and i'll take a look! Commented Jun 26, 2017 at 15:04

1 Answer 1

0

(c + 1) >= (d - 1) should be (c + 1) <= (d - 1)

6
  • Thank you so much! That helped a lot, but i'm still getting;
    – A. Durk
    Commented Jun 28, 2017 at 10:17
  • Sorry— Still getting: :( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right \ expected prompt for input, not exit code of 0
    – A. Durk
    Commented Jun 28, 2017 at 10:17
  • Any insight into why that might be happening? I really appreciate your help!
    – A. Durk
    Commented Jun 28, 2017 at 10:17
  • That sounds like your won function returns true when it shouldn't.
    – Blauelf
    Commented Jun 28, 2017 at 11:25
  • Gotcha, I added my won function above... is there something wrong with iterating through the matrix?
    – A. Durk
    Commented Jun 28, 2017 at 14:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .