Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.69 KB | None | 0 0
  1. bool move(int tile)
  2. {
  3.     int i = 0;
  4.     int j = 0;
  5.     int tilevalue;
  6.     //Start search for the tile the user selected
  7.     while (i < d)
  8.     {
  9.         while (j < d)
  10.         {
  11.             //Grab the value of the current index iteration
  12.             tilevalue = board[i][j];
  13.             //Check the value of the current index iteration against
  14.             //the user selected tile to see if we have a match
  15.             if (tilevalue == tile)
  16.             {
  17.                 //Checks to see if the move is valid by referencing the array index +/- 1 for
  18.                 //the value 0. Depending on the X and Y axis will determine which direction to
  19.                 //look for 0.
  20.                
  21.                 //This checks if the position is at the bottom of the X axis ([0][x] index)
  22.                 if (i == 0)
  23.                 {
  24.                     //This checks if the position is 0, 0, or the bottom left of the board.
  25.                     //We can only search for 0 above and to the right from this position.
  26.                     if (j == 0)
  27.                     {  
  28.                         //Check up
  29.                         if (board[i+1][j] == 0)
  30.                         {
  31.                             board[i+1][j] = 0;
  32.                             board[i][j] = tile;
  33.                             return true;
  34.                         }
  35.                         //Check right
  36.                         else if (board[i][j+1] == 0)
  37.                         {
  38.                             board[i][j] = 0;
  39.                             board[i][j+1] = tile;
  40.                             return true;
  41.                         }
  42.                     }
  43.                     //This checks if the position is 0, d, or the bottom right of the board.
  44.                     //We can only search for 0 above and to the left from this position.
  45.                     else if (j == d - 1)
  46.                     {
  47.                         //Check up
  48.                         if (board[i+1][j] == 0)
  49.                         {
  50.                             board[i][j] = 0;
  51.                             board[i+1][j] = tile;
  52.                             return true;
  53.                         }
  54.                         //Check left
  55.                         else if (board[i][j-1] == 0)
  56.                         {
  57.                             board[i][j] = 0;
  58.                             board[i][j-1] = tile;
  59.                             return true;
  60.                         }
  61.                     }
  62.                     //If we get here, we know that the tile is not on the bottom right or left corner,
  63.                     //so we need to check all positions around it to ensure we can find 0 in case
  64.                     //it does indeed exist in [0][x]. This ensures that we look up, left, and right
  65.                     //for any tiles that are not already at the edge of the index for the current row.
  66.                     else
  67.                     {
  68.                         //Check up
  69.                         if (board[i+1][j] == 0)
  70.                         {
  71.                             board[i][j] = 0;
  72.                             board[i+1][j] = tile;
  73.                             return true;
  74.                         }
  75.                         //Check left
  76.                         else if (board[i][j-1] == 0)
  77.                         {
  78.                             board[i][j] = 0;
  79.                             board[i][j-1] = tile;
  80.                             return true;
  81.                         }
  82.                         //Check right
  83.                         else if (board[i][j+1] == 0)
  84.                         {
  85.                             board[i][j] = 0;
  86.                             board[i][j+1] = tile;
  87.                             return true;
  88.                         }
  89.                         //If we get here, it means that we have checked every position of the bottom row for 0.
  90.                         //This means we are ready to check the positions of the very top row for 0.
  91.                     }
  92.                 }
  93.                 //This checks the position of [d-1][x] (the top of the board) for the tile
  94.                 else if (i == d - 1)
  95.                 {
  96.                     //This checks if the tile is at [d-1][0] (the top-left corner of the board) to ensure that we
  97.                     //only check for 0 in the direction of possible moves.
  98.                     if (j == 0)
  99.                     {
  100.                         //Check bottom
  101.                         if (board[i-1][j] == 0)
  102.                         {
  103.                             board[i][j] = 0;
  104.                             board[i-1][j] = tile;
  105.                             return true;
  106.                         }
  107.                         //Check right
  108.                         else if (board[i][j+1] == 0)
  109.                         {
  110.                             board[i][j] = 0;
  111.                             board[i][j+1] = tile;
  112.                         }
  113.                     }
  114.                     //Checks if the tile is at [d-1][d-1] (the top right corner of the board) to ensure that we
  115.                     //only check for 0 in the direction of possible moves.
  116.                     else if (j == d - 1)
  117.                     {
  118.                         //Check bottom
  119.                         if (board[i-1][j] == 0)
  120.                         {
  121.                             board[i][j] = 0;
  122.                             board[i-1][j] = tile;
  123.                             return true;
  124.                         }
  125.                         //Check left
  126.                         else if (board[i][j-1] == 0)
  127.                         {
  128.                             board[i][j] = 0;
  129.                             board[i][j-1] = tile;
  130.                             return true;
  131.                         }
  132.                     }
  133.                     //We now know that j is possibly somewhere in the middle of the top row of the board. We now search
  134.                     //down, right, and left to see if we can find 0.
  135.                     else
  136.                     {
  137.                         //Check down
  138.                         if (board[i-1][j] == 0)
  139.                         {
  140.                             board[i][j] = 0;
  141.                             board[i-1][j] = tile;
  142.                             return true;
  143.                         }
  144.                         //Check left
  145.                         else if (board[i][j-1] == 0)
  146.                         {
  147.                             board[i][j] = 0;
  148.                             board[i][j-1] = tile;
  149.                             return true;
  150.                         }
  151.                         //Check right
  152.                         else if (board[i][j+1] == 0)
  153.                         {
  154.                             board[i][j] = 0;
  155.                             board[i][j+1] = tile;
  156.                             return true;
  157.                         }  
  158.                     }
  159.                     //If we get this far, we know that the tile doesn't exist at the top or at the bottom of the array,
  160.                     //but somewhere in the middle. Now is a good time to check if the tile resides at the first or last
  161.                     //index of the Y axis. This ensures we are never looking out of scope of the array's index when
  162.                     //checking for 0. If we can confirm that it does not reside on the outside of the board, we can test
  163.                     //all directions safely
  164.                 }
  165.                 //Start of the Y axis checking for the tile
  166.                
  167.                 //Check index [x][0] to see if the tile is there. Then we can check for 0 accordingly.
  168.                 if (j == 0)
  169.                 {
  170.                     //Check up
  171.                     if (board[i+1][j] == 0)
  172.                         {
  173.                             board[i][j] = 0;
  174.                             board[i+1][j] = tile;
  175.                             return true;
  176.                         }
  177.                     //Check down
  178.                     if (board[i-1][j] == 0)
  179.                     {
  180.                         board[i][j] = 0;
  181.                         board[i-1][j] = tile;
  182.                         return true;
  183.                     }
  184.                     //Check right
  185.                     else if (board[i][j+1] == 0)
  186.                     {
  187.                         board[i][j] = 0;
  188.                         board[i][j+1] = tile;
  189.                         return true;
  190.                     }
  191.                 }
  192.                 else if (j == d - 1)  
  193.                 {
  194.                     //Check up
  195.                     if (board[i+1][j] == 0)
  196.                     {  
  197.                         board[i][j] = 0;
  198.                         board[i+1][j] = tile;
  199.                         return true;
  200.                     }
  201.                     //Check down
  202.                     if (board[i-1][j] == 0)
  203.                     {
  204.                         board[i][j] = 0;
  205.                         board[i-1][j] = tile;
  206.                         return true;
  207.                     }
  208.                     //Check left
  209.                     else if (board[i][j-1] == 0)
  210.                     {
  211.                         board[i][j] = 0;
  212.                         board[i][j-1] = tile;
  213.                         return true;
  214.                     }
  215.                 }
  216.                 //If we get here, we now know that the tile is somewhere in the middle with the possibility to
  217.                 //have zero to the left, right, up, or down
  218.                 else
  219.                 {
  220.                     //Check up
  221.                     if (board[i+1][j] == 0)
  222.                     {  
  223.                         board[i][j] = 0;
  224.                         board[i+1][j] = tile;
  225.                         return true;
  226.                     }
  227.                     //Check down
  228.                     else if (board[i-1][j] == 0)
  229.                     {
  230.                         board[i][j] = 0;
  231.                         board[i-1][j] = tile;
  232.                         return true;
  233.                     }
  234.                     //Check left
  235.                     else if (board[i][j-1] == 0)
  236.                     {
  237.                         board[i][j] = 0;
  238.                         board[i][j-1] = tile;
  239.                         return true;
  240.                     }
  241.                     //Check right
  242.                     else if (board[i][j+1] == 0)
  243.                     {
  244.                         board[i][j] = 0;
  245.                         board[i][j+1] = tile;
  246.                         return true;
  247.                     }
  248.                     //If we get here, we now know that the move was invalid
  249.                     else
  250.                     {
  251.                         return false;
  252.                     }
  253.                 }
  254.             }
  255.             j++;
  256.         }
  257.         j = 0;
  258.         i++;
  259.     }
  260.     return false;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement