0

I think some of the move code I've written is working correctly in that once the "Tile to move:" message appears and I enter a logical tile to move, I do not see the "illegal move" message, but when I do choose a number that does not border the blank space, I do get the "illegal move" message. However, the board does not redraw and the "Tile to move:" message remains after a correctly chosen tile to move.

Any hints on mistakes I've made or additions I need are very much appreciated!

    void init(void)
{
    // TODO
     int grid [d][d];
     int i = 0;
     int j = 0;
     grid [i][j] = (d * d) - 1;
     int n = (d * d) - 1;
     for (j = 0;j < d;j++)
    {
        board [i][j] = n;
        n--;
    }

    while (i < d)
        {
            i++;

            for (j = 0;j < d;j++)
                {
                    board[i][j] = n;
                    n--;

                }
    if (i == d-1)    
    {   if (d == 4 || d == 6 || d == 8)
        {   //swap 1 and 2

        int temp = board[i][j-2];
        board[i][j - 2] = board[i][j - 3];
        board[i][j - 3] = temp;
        }    

    }
}
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    // TODO
    int i;
    int j;

    for (i = 0;i < d;i++)
    {
        for (j = 0;j < d;j++)
        {   if (board[i][j] == 0)
                printf("%2c  ",'_');
            else    
            printf("%2d  ",board[i][j]);

        }

        printf("\n");
    }

}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    // TODO
    // SEARCH for tile
    int i = 0;
    int j = 0;

    {
        for (i = 0;i < d;i++)
        {
            for (j = 0; j < d;j++)


            {   if (board[i][j] == tile) 

                   {

                    if (board[i][j + 1] == 0 || board[i][j - 1] == 0
                        || board[i + 1][j] == 0 || board[i - 1][j] == 0)
                    {   // move tile
                        int blank = 0;
                   int temp = tile;
                   board[i][j] = blank;
                   blank = temp;

                    return true;
                    }
                   }
            }               
    }
    }
    return false;
}

/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{
    // TODO
    return false;
}
0

The code does a nice job of swapping values around between tile, blank and temp, but does nothing with the actual values in the board[][] array. The actual board remains unchanged.

While the values in tile and blank may be useful to locate the tile to move and the blank tile, you have to swap the actual array elements to execute the move.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • I made some edits in the move-swap area and am now creating '' for each tile I move, but I'm still not able to "swap" tiles. I think I'm just creating a bunch of 0 s where the tiles that I choose are locatedt hat then turn into '' because of my above draw function? I just can't figure out how to make this work. Any thoughts? – Karen Safrit Oct 7 '16 at 3:04
  • Same problem. tile contains the value of the tile to move, but the board[][] element that contains that value has to be swapped with the blank or 0 element in the board array. You're still swapping the value of tile with blank, which really does nothing useful, but in the middle, you're inserting the blank value, or 0, into the array. Also, you're testing to see IF one of the adjoining tiles is blank, not WHICH is blank, and then trying to swap if that's true. How do you know which of the 4 is the actual blank? You'll need 4 separate tests and code blocks for this. – Cliff B Oct 7 '16 at 22:43
  • Thank you, Cliff. I knew that part of my problem was the "if blank" vs "which blank," so I've already expanded the tests/code blocks. I have some odd stuff going on now, though some swapping is working, so I'm in the midst of investigating that further. As always, your help is phenomenal. – Karen Safrit Oct 7 '16 at 22:55

You must log in to answer this question.

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