0

guys! I am a beginner and not good enough at programming yet. On the game of fifteen I am trying to understand how this block of code works for a long time and still cant get the idea!:(

bool legalmove(void) { // check if on top row. If so check up for 0 if (tile_i > 0 && board[tile_i - 1][tile_j] == 0) return true; // bottom if (tile_i < d-1 && board[tile_i + 1][tile_j] == 0) return true; // left if (tile_j > 0 && board[tile_i][tile_j - 1] == 0) return true; // right if (tile_j < d-1 && board[tile_i][tile_j + 1] == 0) return true; else return false; }

I understand it in general but I can`t get the details...

1 Answer 1

0

You don't really need to know whether the tile is to the right, left, top or bottom exactly. if you have a 2-dimensional array you don't need to check all of the tiles. You can construct a nested for loop and a condition in order to check the neighbours only. The following is not C but rather pseudocode, so don't take it literally.

for (outer loop)
    for (inner loop)
        if (counter of outer loops don't match and current cell is empty)
             make a move;

You must log in to answer this question.

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