0

i have tried to look for a solution before i posted this and i cant get 1, please help me. https://pastebin.com/PsnMGsGx

/**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/

void init(void)
{
// TODO
int i,j;
int k=d*d-1;
// blank space
blank_row=d-1;
blank_column=d-1;
for(i=0;i<d;i++)
{
 for(j=0;j<d;j++)
 {
  board[i][j]=k--;
 }
 }
 if(d%2!=0)
  {
   int temp=board[d-1][d-3];
   board[d-1][d-3]=board[d-1][d-2];
   board[d-1][d-2]=temp;
  }
 board[blank_row][blank_column]=0;
}

/**
* Prints the board in its current state.
*/
void draw(void)
  {
printf("\n");
// TODO
 for(int i=0;i<d;i++)
    {
 for(int j=0;j<d;j++)
 {
     if(board[i][j]==0)
     {
        printf("_\t");
     }
     else
    printf("%2i\t",board[i][j]);
  }
 printf("\n \n");
 }
  }


  bool move(int tile)
  {//int row n column to track the position of blank space

 int tile_row;
 int tile_column;
 for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if(board[i][j]==0)
{
blank_row=i;
blank_column=j;
}
if(board[i][j]==tile&&tile>0&&tile<d*d)
{
tile_row=i;
tile_column=j;
}
}
}
 if((board[blank_row-1][blank_column]==tile) || (board[blank_row+1]
 [blank_column]==tile)  || (board[blank_row][blank_column-1]==tile) || 
 (board[blank_row][blank_column+1]==tile))
  {
 metrytoexchange(&board[blank_row][blank_column],&board[tile_row]
[tile_column]);
return true;
}
return false;
}

/** * Returns true if game is won (i.e., board is in winning configuration), * else false. */

// TODO
 bool won(void)
 {
 // TODO
 int checker=1;
  for(int i=0;i<d;i++)
 {
 for(int j=0;j<d;j++)
 {
 if (board[i][j]!=checker++)
    {
        return false;
    }

 }

  }

    return true;
  }


 void metrytoexchange(int *a,int *b)
   {
    int temp= *a;
    *a = *b;
    *b = temp;
    }

but i get this sad faces from check50

:( 3x3 board: move up-up-left-down-down-left-up-up-right-down-down-right
did not find output "4-1-7|8-2-6|5-3..."
:( 3x3 board: make sure none of 2, 4, 5, 6, 7, 8 move tile
did not find output "Illegal move."
:( 3x3 board: move blank left (tile 1) then up (tile 4), then try to move 
tiles 1, 2, 6, 8
did not find output "Illegal move."
:( 3x3 board: make sure game is winnable
did not find output "1-2-3|4-5-6|7-8..."
:( 4x4 board: make sure game is winnable
did not find output "1-2-3-4|5-6-7-8..."
See https://cs50.me/checks/dda85031d55230ee0ed5ce7626816452c88cdc3b for more 
detail.
0

In init, I think swapping 1 and 2 is done for even board sizes, so those with d % 2 == 0, not d % 2 != 0.

board[blank_row][blank_column]=0; makes little sense, as that one should already be 0 at that moment.

In move, you don't have to find the empty tile if you correctly keep track of it.

Do not do something like board[blank_row-1][blank_column] unless you have first tested whether blank_row-1 is in range 0..d-1. Otherwise, if blank_row were 0, you might end up accessing memory outside the array. Same for other computed indices used in the array. You can use &&'s property of short-circuit, if the left side is false, it would not even evaluate the right side.

If you already have tile_row and tile_column, you can avoid this whole thing completely, and just compare tile_row and empty_row, same for the column. If one of the two pairs is equal, and the other differs by one (absolute value), the two tiles are neighbours.

won needs to account for the empty tile. This could be done by for example

if (board[i][j] != counter++ % (d * d))

or

if (board[i][j] != counter++ && board[i][j] != 0)

(important to have the two sides of the && in this order, as otherwise the side-effect of increment operator won't be triggered on empty tile)

I don't like the ++ operator used in the condition. Sure, it saves you another line, but makes the code slightly more fragile.

  • thank you @Blauef for the corrections, you were true.but why does this code on won refuse to work?if (checker % (d*d) !=board[i][j]) { checker++; return false; } – TabithaWamucii Sep 11 '17 at 19:04
  • You need to increment checker on each iteration of the inner loop. That way, you'd do it just before returning. – Blauelf Sep 12 '17 at 7:31

You must log in to answer this question.

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