0

So I have reviewed similar questions on here but can't quit fix my move code. It will correctly move tiles from within one column to row below (say 3 in the initial position for a 3x3 board). But it misfires when attempting to move across columns within the same row (say moving the the "2" to over in the initial position. I did look to see if it access outside of the array and it doesn't seem like it should. Ill post both the outputted table when I try to move the 2 plus my move code. Thank you!

 8       7       6
 5       4       3
 1       _
 2
Tile to move: 

and now the code:

 void draw(void)
{

for(int i=0; i<d; i++)
 {   for(int j=0; j<d; j++)
if (board[i][j]==0)
 {  printf(" _");
     printf("\n");}


            else    
         {printf("%2d\t", board[i][j]);
            if (j==(d-1))
            {printf("\n");}
         }
         }
             }


bool move(int tile)
{
    for (int i=0; i<d; i++)
    {   
        for(int j=0; j<d; j++)
        {
            if (board[i][j]==tile)
            { 
                if (i>0 && board[i-1][j]==0) 
                {
                    int swap=board[i][j];
                    board[i][j]=0;
                    board[i-1][j]=swap;
                    return true;
                }
                else if  (j>0 && board[i][j-1]==0)
                {
                    int swap=board[i][j];
                    board[i][j]=0;
                    board [i][j-1]=swap;
                    return true;
                }
                else if (j<(d-1) && board[i][j+1]==0)
                {
                    int swap=board[i][j];
                    board[i][j]=0;
                    board [i][j+1]=swap;
                    return true;
                }
                else if (i<(d-1) && board[i+1][j]==0)
                {
                    int swap=board[i][j];
                    board[i][j]=0;
                    board[i+1][j]=swap;
                    return true;
                }    
            }
        }
    }
    return false;
}
1

The move() code seems to be working fine. In looking at your output (the raw form, not the version that Stack reformatted before I edited it to show as output), it looks to me that maybe you have a linefeed, \n printing out after your underscore for the blank tile. It wouldn't show up in the initial board layout, but will show after the first tile is moved. I suspect that if the first move is up to 3, there's an extra blank row between the second and third rows. I'm betting that the problem lies in the draw() function, not move().

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

6
  • Hm. Here is the draw code. Without some \n command it prints the tile prompt on the same line. – astro Oct 25 '16 at 2:12
  • void draw(void) { for (int i=0; i<d; i++) { for(int j=0; j<d; j++) if (board[i][j]==0) { printf("_"); printf("\n");} else {printf("%2d\t", board[i][j]); if (j==(d-1)) {printf("\n");} } } } – astro Oct 25 '16 at 2:12
  • Ok. Im having trouble formatting it here. – astro Oct 25 '16 at 2:14
  • Perhaps you could edit the question and add the code there? – Cliff B Oct 25 '16 at 2:23
  • 1
    It's exactly as I thought. Your draw() function has problems - an extra line feed as well as not handling the spacing around the underscore correctly. Give it some thought and you should be able to resolve it easily. If you haven't fixed it after a good effort, leave a comment. – Cliff B Oct 25 '16 at 2:32

You must log in to answer this question.

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