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;
}
  • format your code properly and i'll take a look! – Zach Arundel Jun 26 '17 at 15:04
0

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

| improve this answer | |
  • Thank you so much! That helped a lot, but i'm still getting; – A. Durk Jun 28 '17 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 Jun 28 '17 at 10:17
  • Any insight into why that might be happening? I really appreciate your help! – A. Durk Jun 28 '17 at 10:17
  • That sounds like your won function returns true when it shouldn't. – Blauelf Jun 28 '17 at 11:25
  • Gotcha, I added my won function above... is there something wrong with iterating through the matrix? – A. Durk Jun 28 '17 at 14:08

You must log in to answer this question.

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