Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool move(int tile)
- {
- int empty_tile = 0;
- for (int i = 0; i < d; i++)
- {
- for (int j = 0; j < d; j++)
- {
- // if the current element is a tile the user wants to move and the one above it is an empty space
- if (board[i][j] == tile && board[i + 1][j] == empty_tile)
- {
- swap(&board[i][j], &board[i + 1][j]);
- return true;
- }
- // else, if the one below the tile the user wants to move is an empty space
- else if (board[i][j] == tile && board[i - 1][j] == empty_tile)
- {
- swap(&board[i][j], &board[i - 1][j]);
- return true;
- }
- // else, if the one to the left of the tile the user wants to move is an empty space
- else if (board[i][j] == tile && board[i][j - 1] == empty_tile)
- {
- swap(&board[i][j], &board[i][j - 1]);
- return true;
- }
- // else, if the one to the right of the tile the user wants to move is an empty space
- else if (board[i][j] == tile && board[i][j + 1] == empty_tile)
- {
- swap(&board[i][j], &board[i][j + 1]);
- return true;
- }
- // else, if the one above the tile the user wants to move is not an empty space and if the
- // the said move is going over an edge of the board
- else if (board[i][j] == tile && board[i + 1][j] == board[d][d] && board[i + 1][j] != empty_tile)
- {
- return false;
- }
- // else, if the one below the tile the user wants to move is not an empty space and if the
- // the said move is going over an edge of the board
- else if (board[i][j] == tile && board[i - 1][j] == board[d][d] && board[i - 1][j] != empty_tile)
- {
- return false;
- }
- // else, if the one to the left of the tile the user wants to move is not an empty space and if the
- // the said move is going over an edge of the board
- else if (board[i][j] == tile && board[i][j - 1] == board[d][d] && board[i][j - 1] != empty_tile)
- {
- return false;
- }
- // else, if the one to the right of the tile the user wants to move is not an empty space and if the
- // the said move is going over an edge of the board
- else if (board[i][j] == tile && board[i][j + 1] == board[d][d] && board[i][j + 1] != empty_tile)
- {
- return false;
- }
- }
- }
- return false;
- }
- void swap(int *a, int *b)
- {
- int *temp = a;
- *a = *b;
- *b = *temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement