0

i can't figure out why i get the sad faces on move and won.Help will be appreciated.i am an able to post the code right in this forum. https://pastebin.com/RXwnUqaz

  */
bool move(int tile)
{//to do
int blank_space=0;
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
 if(board[i][j]==blank_space)
 {
 //iterate to check the position of of blank tile;left to right up and down 
if not return false
 if( (i-1)>=0 && board[i-1][j]==tile)
 {
  int temp=board[i-1][j];
  board[i-1][j]=board[i][j];
  board[i][j]=temp;
    return true;
}
  if((j-1)>=0 && board[i][j-1]==tile)
{
  int temp=board[i][j];
  board[i][j]=board[i][j-1];
  board[i][j-1]=temp;
  return true;
}
 if((i+1)<=(d-1) && board[i+1][j]==tile)
{
  int temp=board[i][j];
  board[i][j]=board[i+1][j];
  board[i+1][j]=temp;
  return true;
}
if((j+1)<=(d-1) && board[i][j+1]==tile)
{
   int temp=board[i][j];
   board[i][j]=board[i][j+1];
   board[i][j+1]=temp;
   return true;
}
}
}
}
return false;
}

  /**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.
*/
bool won(void)
{
// TODO
int checker=1;
for(int i=0;i<=d;i++)
{
for(int j=0;j<=d;j++)
{
    if (checker!=board[i][j] && board[d-1][d-1]==0)
    {
        return false;
    }
    checker++;
    }
 }
 return true;
 }
1
  • In the post editor, there's a {} button to format code. It just adds four spaces in front of the selected text (or removes them if all the selected lines start with four spaces). However, it won't fix your badly indented code, just turn it into a code block with fixed-width font, preserved whitespace and some syntax highlighting. – Blauelf Sep 4 '17 at 11:23
0

&& tile<d*d && tile>0 doesn't add value. Won't hurt either.

(i-1)>=0 should be placed before board[i-1][j]==blank_space. The && operator tests the left side first, and if it is zero, won't evaluate the right side (short-circuit evaluation). So by placing (i-1)>=0 first, you don't risk a segfault.

The move function is not the most efficient one, but looks valid otherwise.

won, however, is wrong in two ways.

  1. if (checker!=board[i][j] ) does not account for the empty tile, like for example if (checker % (d * d) != board[i][j]) would, using the remainder operator for the wrap-around.
  2. With board[i][j]=checker++;, you change the board's content. Based on the code around, you would see an effect only after fixing 1. and winning the game, as this would turn the empty tile from value 0 to d*d then. Just use checker++; without an assignment.
4
  • i am not sure if i got you right but i stlll have sad faces pastebin.com/5jvsXwPJ – TabithaWamucii Sep 4 '17 at 19:37
  • bool won(void) { // TODO int checker=1; for(int i=0;i<d;i++) { for(int j=0;j<d;j++) { if (checker!=board[i][j] && checker<d*d) { return false; } checker++; } } return true; } – TabithaWamucii Sep 4 '17 at 19:43
  • i still get sad face – TabithaWamucii Sep 4 '17 at 19:44
  • @ Blauelf i dont think i understand what you mean, please try to explain in simpler way. thank you in advance – TabithaWamucii Sep 4 '17 at 19:47

You must log in to answer this question.

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