0

I was trying to solve this Pset, and I believe there is something wrong in my move function, the if condition maybe?? I used debug50 and found out that the if loop takes part even when the move is illegal! this almost blew my mind up!! :D so, some help would be very appreciated.

// board
int board[DIM_MAX][DIM_MAX]; //board is a local variable, so all the 
functions can see it

// dimensions
int d;

// prototypes
 void clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);



int tile;
int main(int argc, string argv[])
{

if (argc != 2)
{
    printf("Usage: fifteen d\n");
    return 1;
}

// ensure valid dimensions
d = atoi(argv[1]);
if (d < DIM_MIN || d > DIM_MAX)
{
    printf("Board must be between %i x %i and %i x %i, inclusive.\n",
        DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
    return 2;
}

// open log
FILE* file = fopen("log.txt", "w");
if (file == NULL)
{
    return 3;
}

// greet user with instructions
greet();

// initialize the board
init();

// accept moves until game is won
while (true)
{
    // clear the screen
    clear();

    // draw the current state of the board
    draw();

    // log the current state of the board (for testing)
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            fprintf(file, "%i", board[i][j]);
            if (j < d - 1)
            {
                fprintf(file, "|");
            }
        }
        fprintf(file, "\n");
    }
    fflush(file); 

     // check for win
    if (won())
    {
        printf("ftw!\n");
        break;
    }

    do{
     // prompt for move
    printf("tile To Move?");
    tile=get_int();

     // quit if user inputs 0 (for testing)
    if (tile == 0)
    {
        break;
    }


    // log move (for testing)
    fprintf(file, "%i\n", tile);
    fflush(file);


    // move if possible, else report illegality
    if (!move(tile))
    {
        printf("\nIllegal move.\n");
        usleep(500000);
    }
    draw();

    }while(tile>0);
    // sleep thread for animation's sake
    usleep(500000);

}
// close log
fclose(file);

// success

return 0;
}


void clear(void)
{
printf("\033[2J");
printf("\033[%d;%dH", 0, 0);
}


void greet(void)
{
clear();
printf("WELCOME TO GAME OF FIFTEEN\n");
usleep(2000000);
}


 void init(void)
{

int i,j,start=0,temp=0;

start=d*d-1;
for (i=0; i<d;i++){    //the descending order appears only for the first rows
    for (j=0; j<d;j++){
        board[i][j]= start;
        start-=1;
    }


int a=d-1;
for(i=(d-1);i<=(d-1);i++){
    for(j=0;j<d;j++){
        board[i][j]=a;
        a-=1;
    }
}
}
  if(d%2==0){
start=d*d-1;
int b=d-1;
for(i=(d-1);i<=(d-1);i++){
    for(j=0;j<d;j++){
        board[i][j]=b;
        b-=1;
    }
}
temp=board[d-1][d-3] ;//switch 1 & 2, if d is even
board[d-1][d-3]=board[d-1][d-2];
board[d-1][d-2]=temp;
}
}
void draw(void)
{

int i,j;

for (i=0; i<d;i++){    //the descending order appears only for the first rows
    for (j=0; j<d;j++){

        if (board[i][j] == 0) //checking if value is 0 to print a blank tile
               printf(" _");
        else
            printf("%2i\t",board[i][j]);
    }
   printf("\n");
}
}


bool move(int tile)
{


int tile_i=0,tile_j=0;      //a variable to store the wanted tile number to 
move
int blank_i,blank_j,tempp; //to keep track of the blank location for i&j


   int i,j;

    for (i=0; i<d;i++){
    for (j=0; j<d;j++){
            if(board[i][j]==tile){  //keep track of the tile location
                tile_i=i;
                tile_j=j;
            }
            if(board[i][j]==0){    //keep track of the blank location
                blank_i=i;
                blank_j=j;
            }
}}
        if(board[tile_i+1][tile_j]==0 || board[tile_i-1][tile_j]==0){ 
//check for legal move,, if blank is next to the tile

            tempp=board[tile_i][tile_j];         //move
            board[tile_i][tile_j]=board[blank_i][blank_j];
            board[blank_i][blank_j]=tempp;
            return true;
        }
       else if(board[tile_i][tile_j+1]==0 || board[tile_i][tile_j-1]==0){ //or if blank is above/below the tile
            tempp=board[tile_i][tile_j];         //move
            board[tile_i][tile_j]=board[blank_i][blank_j];
            board[blank_i][blank_j]=tempp;
            return true;
        }

 else
 return false;

 }


bool won(void)
{
int i,j,count=1;
for (i=0; i<d;i++){    
    for (j=0; j<d;j++){
        count+=1;       
    }
   printf("\n");
}
if(count==d*d-1)      //if the last element in the array is d*d, you won!
   return true;
else
    return false;
}
0

When checking for legal moves, the code checks whether the i values of the two tiles are adjacent or whether the two j values are adjacent. However, it does NOT verify that the other value is the same. For example, it might find that board[tile_i + 1] == 0, but it doesn't check that board[tile_j] == 0. This means that, in this example, if the blank tile is any tile in the row below the tile to move, they will swap.

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

| improve this answer | |
  • Ahaaa! I changed the code and it seems to work, thank you – Raw Jul 11 '17 at 8:32

You must log in to answer this question.

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