0

I would really appreciate any input into what I must definitely be doing wrong. I am trying to test the code out by myself before entering it into the ide. Instead of writing the tile number in the new position, the tile is replaced with '0', and it wont move the first element. I am not sure why this is happening:

./my_fifteen 3

8   7   6   
5   4   3   
2   1   0   
Which tile to move?3
found at board[1][2]
[p_row][p_col] = [1][2]
3

8   7   6   
5   4   0   
2   1   0   
Which tile to move?6
found at board[0][2]
[p_row][p_col] = [0][2]
6

8   7   0   
5   4   0   
2   1   0   
Which tile to move?7
found at board[0][1]
[p_row][p_col] = [0][1]
7

8   0   0   
5   4   0   
2   1   0   
Which tile to move?8
found at board[0][0]
[p_row][p_col] = [0][0]
8

8   0   0   
5   4   0   
2   1   0   
Which tile to move?^C

My code is:

void draw(void)
{
     //swapping the position of the n - 3 and n - 2 elements,
    //and making sure  n - 1 is null or something..
    if (!(array & 1))  //if the value array is NOT odd;
    {
        tmp = board[row - 1][column - 3];
        board[row - 1][column - 3] = board[row - 1][column - 2];
        board[row - 1][column - 2] = tmp;     
    }    
    //board[row - 1][column - 1] = '\0';

    //generating 2d array:
    for (row = 0; row < array; row++)
    {
        for (column = 0; column < array; column++)
        {
            printf("%d\t", board[row][column]);
        }
        printf("\n");

    }
}

void clear(void)
{
     //clearing the screen with ANSI escape sequences;
     printf("\033[2J");
    printf("\033[%d;%dH", 0, 0);
}

int move(int tile)
{


    //How to find the selected tile position?
    bool found = false;
    for (row = 0; row < array; row++)
    {    
            for (column = 0; column < array; column++)
            {
                if (board[row][column] == tile)
                {
                    found == true;
                    printf("found at board[%i][%i]\n", row, column);
                    //store the position
                    p_row = row;
                    p_col = column;
                    printf("[p_row][p_col] = [%i][%i]\n", p_row, p_col);
                    printf("%i\n", board[p_row][p_col]);
                    break;
                }
            }    
    }   


    //upward
    if (p_row > 0)    //dont go above the array
    {
        //if tile above tile[p_row][p_col] holds zero
        if (board[row - 1][column] == 0)
        {
            int holdspace = board[p_row][p_col];      //store tile value in holdspace,
            board[p_row][p_col] = board[row - 1][column]; //store uptile value in tile,
            board[row - 1][column] = holdspace;         //store holdspace value in uptile
            return true;
        }
    }

    //downward
    if (p_row < array - 1)    //dont go below the array
    {
        if (board[row + 1][column] == 0)
        {
            int holdspace = board[p_row][p_col];      //store tile value in holdspace,
            board[p_row][p_col] = board[row + 1][column]; //store downtile value in tile,
            board[row + 1][column] = holdspace;         //store holdspace value in downtile
            return true;            
        }
    }

    //rightward
    if (p_col < array - 1)     //dont go to the right of the array
    {
        if (board[row][column + 1] == 0)
        {
            int holdspace = board[p_row][p_col];      //store tile value in holdspace,
            board[p_row][p_col] = board[row][column + 1]; //store righttile value in tile,
            board[row][column + 1] = holdspace;         //store holdspace value in righttile
            return true;            
        }
    }

    //leftward
    if (p_col > 0) //dont go to the left of the array
    {
        if (board[row][column - 1] == 0)
        {
            int holdspace = board[p_row][p_col];      //store tile value in holdspace,
            board[p_row][p_col] = board[row][column - 1]; //store lefttile value in tile,
            board[row][column - 1] = holdspace;         //store holdspace value in lefttile
            return true;            
        }
    }

}

2 Answers 2

0

your draw function:

per the comments in fifteen.c, your draw function is responsible of drawing the current state of the board and only that. you shouldn't be performing any swapping inside this function.

you are instructed to swap the tiles 2 and 1 initially in case the board contains an odd number of tiles. so this should be done inside init.

your move function:

your move function should return a bool, not an int. also, there are scenarios where it should return false.

if you want to set a variable x to the value true, you do

x = true;
// NOT
x == true;

mind you that the break statement inside your nested for loop breaks from the nested loop only. this causes a main problem. also are you sure you wanna use row and column as you look for the blank tile around the tile to be moved?

the value that represents the blank tile is constant (i.e., never changes). given that, no need to cache the number of the tile to be moved in order to swap it with the blank tile. instead, you could define the value that represents the blank tile as a constant atop your program and use it directly as follows:

define BLANK as 0

// some code

// moving a tile
set blank tile to moved tile
set moved tile to BLANK

do you need a variable named array? what about d?

I also see you're depending on many global variables and that's not a good practice actually.

7
  • Thanks Kareem, I did say I am trying to figure it outside of the IDE space and I made up some aspects, hence "array" instead of "d". I have tried to implement all you said, including stripping down all the variables to the lowest minimum(just array, tmp, k, tile, row and column now). I think I am having trouble understanding how to store the tile position after getting it from the user, and applying this to the move function. I am also not fully understanding how to move properly.
    – Unpossible
    Commented Jan 25, 2016 at 10:52
  • @Sina I wouldn't use 4 variables row, column, p_row, and p_col. I would use only two of them, make sure they have the correct scopes, and break out of both for loops immediately as I find the tile. I would also store the current coordinates of the blank tile globally and check whether it was adjacent to the tile I found, in which case I would swap them.
    – kzidane
    Commented Jan 25, 2016 at 11:04
  • Thank you very much.
    – Unpossible
    Commented Jan 25, 2016 at 11:09
  • Still very stumped.
    – Unpossible
    Commented Jan 25, 2016 at 12:42
  • @Sina what's the problem now? would you like to update your question with your code?
    – kzidane
    Commented Jan 25, 2016 at 12:46
0

Corrected code is this:

void draw(void)
{    
    //generating 2d array:
    for (row = 0; row < array; row++)
    {
        for (column = 0; column < array; column++)
        {
            printf("%d\t", board[row][column]);
        }
        printf("\n");

    }
}

void clear(void)
{
    //clearing the screen with ANSI escape sequences;
     printf("\033[2J");
     printf("\033[%d;%dH", 0, 0);
}

int move(int tile)
{ 

    //How to find the selected tile position?

    bool found = false;             //set this to false first so its true when found

    for (row = 0; row < array; row++)
    {    
            for (column = 0; column < array; column++)
            {
                if (board[row][column] == tile)
                {
                    found = true;
                    //printf("found at board[%i][%i]\n", row, column);
                    break;
                }
            }
            if (found)
                break;    
    }   


    if (board[row][column] == tile)
    {

    //upward
    if (row > 0)    //dont go above the array
    {
        //if tile above tile[row][column] holds zero
        if (board[row - 1][column] == BLANK)
        {
            board[row - 1][column] = board[row][column]; //store uptile value in tile,
            board[row][column] = BLANK;     //store BLANK value in uptile
            return true;
        }
    }

    //downward
    if (row < array - 1)    //dont go below the array
    {
        if (board[row + 1][column] == BLANK)
        {
            board[row + 1][column] = board[row][column]; //store downtile value in tile,
            board[row][column] = BLANK;         //store BLANK value in downtile
            return true;            
        }
    }

    //rightward
    if (column < array - 1)     //dont go to the right of the array
    {
        if (board[row][column + 1] == BLANK)
        {
            board[row][column + 1] = board[row][column]; //store righttile value in tile,
            board[row][column] = BLANK;         //store BLANK value in righttile
            return true;            
        }
    }

    //leftward
    if (column > 0) //dont go to the left of the array
    {
        if (board[row][column - 1] == BLANK)
        {
            board[row][column - 1] = board[row][column]; //store lefttile value in tile,
            board[row][column] = BLANK;         //store BLANK value in lefttile
            return true;            
        }
    }

    } 

    return false;       //if all the above fails.   
}
1
  • Thanks @Kareem. Your breakdowns are amazing. I wonder if you could tutor me a little? :-)
    – Unpossible
    Commented Jan 25, 2016 at 23:23

You must log in to answer this question.

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