Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. bool move(int tile)
  2. {
  3.   //  int temp;
  4.    
  5.     int blankrow;
  6.     int blankcol;
  7.    
  8.     int tilerow;
  9.     int tilecol;
  10.    
  11.     for (int row = 0; row < d; row++){
  12.         for (int col = 0; col < d; col++){
  13.            
  14.             // determine pos of blank tile
  15.             if (board[row][col] == 0){
  16.                 blankrow = row;
  17.                 blankcol = col;
  18.                
  19.             // determine if blank tile is out of bounds
  20.             if (blankrow + 1 > d-1 || blankcol + 1 > d-1 || blankrow -1 > d-1 || blankcol - 1 > d-1){
  21.                 return false;
  22.                 }
  23.             }
  24.            
  25.             // determine pos of user tile
  26.             if (board[row][col] == tile) {
  27.                 tilerow = row;
  28.                 tilecol = col;
  29.             // determine if tile move is out of bounds
  30.                 if (tilerow + 1 > d-1 || tilecol + 1 > d-1 || tilerow - 1 > d-1 || tilecol - 1 > d-1){
  31.                     return false;
  32.                 }
  33.                
  34.                 else if ((tilerow == blankrow) && (tilecol == blankcol +1 || tilecol == blankcol-1)) || ((tilecol == blankcol) && (tilerow == blankcol +1 || tilerow == blankcol-1)){
  35.                 // legal move is permitted...blankrow and col must be updated once swap takes places
  36.                 temp = board[row][col];
  37.                 board[row][col] = tile;
  38.                 tile = temp;
  39.                     return true;
  40.                 }
  41.                
  42.             }
  43.                
  44.             }
  45.            
  46.        
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement