 # Game of Fifteen move() Function

Oct 25th, 2016
200
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. bool move(int tile)
2. {
3.     int empty_tile = 0;
4.     for (int i = 0; i < d; i++)
5.     {
6.         for (int j = 0; j < d; j++)
7.         {
8.             // if the current element is a tile the user wants to move and the one above it is an empty space
9.             if (board[i][j] == tile && board[i + 1][j] == empty_tile)
10.             {
11.                 swap(&board[i][j], &board[i + 1][j]);
12.                 return true;
13.             }
14.             // else, if the one below the tile the user wants to move is an empty space
15.             else if (board[i][j] == tile && board[i - 1][j] == empty_tile)
16.             {
17.                 swap(&board[i][j], &board[i - 1][j]);
18.                 return true;
19.             }
20.             // else, if the one to the left of the tile the user wants to move is an empty space
21.             else if (board[i][j] == tile && board[i][j - 1] == empty_tile)
22.             {
23.                 swap(&board[i][j], &board[i][j - 1]);
24.                 return true;
25.             }
26.             // else, if the one to the right of the tile the user wants to move is an empty space
27.             else if (board[i][j] == tile && board[i][j + 1] == empty_tile)
28.             {
29.                 swap(&board[i][j], &board[i][j + 1]);
30.                 return true;
31.             }
32.             // else, if the one above the tile the user wants to move is not an empty space and if the
33.             // the said move is going over an edge of the board
34.             else if (board[i][j] == tile && board[i + 1][j] == board[d][d] && board[i + 1][j] != empty_tile)
35.             {
36.                 return false;
37.             }
38.             // else, if the one below the tile the user wants to move is not an empty space and if the
39.             // the said move is going over an edge of the board
40.             else if (board[i][j] == tile && board[i - 1][j] == board[d][d] && board[i - 1][j] != empty_tile)
41.             {
42.                 return false;
43.             }
44.             // else, if the one to the left of the tile the user wants to move is not an empty space and if the
45.             // the said move is going over an edge of the board
46.             else if (board[i][j] == tile && board[i][j - 1] == board[d][d] && board[i][j - 1] != empty_tile)
47.             {
48.                 return false;
49.             }
50.             // else, if the one to the right of the tile the user wants to move is not an empty space and if the
51.             // the said move is going over an edge of the board
52.             else if (board[i][j] == tile && board[i][j + 1] == board[d][d] && board[i][j + 1] != empty_tile)
53.             {
54.                 return false;
55.             }
56.         }
57.     }
58.     return false;
59. }
60.
61. void swap(int *a, int *b)
62. {
63.     int *temp = a;
64.     *a = *b;
65.     *b = *temp;
66. }
RAW Paste Data