Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool move(int tile)
- {
- int i = 0;
- int j = 0;
- int tilevalue;
- //Start search for the tile the user selected
- while (i < d)
- {
- while (j < d)
- {
- //Grab the value of the current index iteration
- tilevalue = board[i][j];
- //Check the value of the current index iteration against
- //the user selected tile to see if we have a match
- if (tilevalue == tile)
- {
- //Checks to see if the move is valid by referencing the array index +/- 1 for
- //the value 0. Depending on the X and Y axis will determine which direction to
- //look for 0.
- //This checks if the position is at the bottom of the X axis ([0][x] index)
- if (i == 0)
- {
- //This checks if the position is 0, 0, or the bottom left of the board.
- //We can only search for 0 above and to the right from this position.
- if (j == 0)
- {
- //Check up
- if (board[i+1][j] == 0)
- {
- board[i+1][j] = 0;
- board[i][j] = tile;
- return true;
- }
- //Check right
- else if (board[i][j+1] == 0)
- {
- board[i][j] = 0;
- board[i][j+1] = tile;
- return true;
- }
- }
- //This checks if the position is 0, d, or the bottom right of the board.
- //We can only search for 0 above and to the left from this position.
- else if (j == d - 1)
- {
- //Check up
- if (board[i+1][j] == 0)
- {
- board[i][j] = 0;
- board[i+1][j] = tile;
- return true;
- }
- //Check left
- else if (board[i][j-1] == 0)
- {
- board[i][j] = 0;
- board[i][j-1] = tile;
- return true;
- }
- }
- //If we get here, we know that the tile is not on the bottom right or left corner,
- //so we need to check all positions around it to ensure we can find 0 in case
- //it does indeed exist in [0][x]. This ensures that we look up, left, and right
- //for any tiles that are not already at the edge of the index for the current row.
- else
- {
- //Check up
- if (board[i+1][j] == 0)
- {
- board[i][j] = 0;
- board[i+1][j] = tile;
- return true;
- }
- //Check left
- else if (board[i][j-1] == 0)
- {
- board[i][j] = 0;
- board[i][j-1] = tile;
- return true;
- }
- //Check right
- else if (board[i][j+1] == 0)
- {
- board[i][j] = 0;
- board[i][j+1] = tile;
- return true;
- }
- //If we get here, it means that we have checked every position of the bottom row for 0.
- //This means we are ready to check the positions of the very top row for 0.
- }
- }
- //This checks the position of [d-1][x] (the top of the board) for the tile
- else if (i == d - 1)
- {
- //This checks if the tile is at [d-1][0] (the top-left corner of the board) to ensure that we
- //only check for 0 in the direction of possible moves.
- if (j == 0)
- {
- //Check bottom
- if (board[i-1][j] == 0)
- {
- board[i][j] = 0;
- board[i-1][j] = tile;
- return true;
- }
- //Check right
- else if (board[i][j+1] == 0)
- {
- board[i][j] = 0;
- board[i][j+1] = tile;
- }
- }
- //Checks if the tile is at [d-1][d-1] (the top right corner of the board) to ensure that we
- //only check for 0 in the direction of possible moves.
- else if (j == d - 1)
- {
- //Check bottom
- if (board[i-1][j] == 0)
- {
- board[i][j] = 0;
- board[i-1][j] = tile;
- return true;
- }
- //Check left
- else if (board[i][j-1] == 0)
- {
- board[i][j] = 0;
- board[i][j-1] = tile;
- return true;
- }
- }
- //We now know that j is possibly somewhere in the middle of the top row of the board. We now search
- //down, right, and left to see if we can find 0.
- else
- {
- //Check down
- if (board[i-1][j] == 0)
- {
- board[i][j] = 0;
- board[i-1][j] = tile;
- return true;
- }
- //Check left
- else if (board[i][j-1] == 0)
- {
- board[i][j] = 0;
- board[i][j-1] = tile;
- return true;
- }
- //Check right
- else if (board[i][j+1] == 0)
- {
- board[i][j] = 0;
- board[i][j+1] = tile;
- return true;
- }
- }
- //If we get this far, we know that the tile doesn't exist at the top or at the bottom of the array,
- //but somewhere in the middle. Now is a good time to check if the tile resides at the first or last
- //index of the Y axis. This ensures we are never looking out of scope of the array's index when
- //checking for 0. If we can confirm that it does not reside on the outside of the board, we can test
- //all directions safely
- }
- //Start of the Y axis checking for the tile
- //Check index [x][0] to see if the tile is there. Then we can check for 0 accordingly.
- if (j == 0)
- {
- //Check up
- if (board[i+1][j] == 0)
- {
- board[i][j] = 0;
- board[i+1][j] = tile;
- return true;
- }
- //Check down
- if (board[i-1][j] == 0)
- {
- board[i][j] = 0;
- board[i-1][j] = tile;
- return true;
- }
- //Check right
- else if (board[i][j+1] == 0)
- {
- board[i][j] = 0;
- board[i][j+1] = tile;
- return true;
- }
- }
- else if (j == d - 1)
- {
- //Check up
- if (board[i+1][j] == 0)
- {
- board[i][j] = 0;
- board[i+1][j] = tile;
- return true;
- }
- //Check down
- if (board[i-1][j] == 0)
- {
- board[i][j] = 0;
- board[i-1][j] = tile;
- return true;
- }
- //Check left
- else if (board[i][j-1] == 0)
- {
- board[i][j] = 0;
- board[i][j-1] = tile;
- return true;
- }
- }
- //If we get here, we now know that the tile is somewhere in the middle with the possibility to
- //have zero to the left, right, up, or down
- else
- {
- //Check up
- if (board[i+1][j] == 0)
- {
- board[i][j] = 0;
- board[i+1][j] = tile;
- return true;
- }
- //Check down
- else if (board[i-1][j] == 0)
- {
- board[i][j] = 0;
- board[i-1][j] = tile;
- return true;
- }
- //Check left
- else if (board[i][j-1] == 0)
- {
- board[i][j] = 0;
- board[i][j-1] = tile;
- return true;
- }
- //Check right
- else if (board[i][j+1] == 0)
- {
- board[i][j] = 0;
- board[i][j+1] = tile;
- return true;
- }
- //If we get here, we now know that the move was invalid
- else
- {
- return false;
- }
- }
- }
- j++;
- }
- j = 0;
- i++;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement