Guest User

Untitled

a guest
Nov 7th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 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.             // find zero tile position
  15.             if (board[row][col] == 0){
  16.                 blankrow = row;
  17.                 blankcol = col;
  18.                 }
  19.                
  20.            
  21.             // find target tile position
  22.                 if (board[row][col] == tile) {
  23.                 tilerow = row;
  24.                 tilecol = col;
  25.                 }
  26.         }
  27.                
  28.                 // determine if tile move is out of bounds
  29.                 if (tilerow > d-1 || tilecol > d-1 || tilerow > d-1 || tilecol > d-1){
  30.                     return false;
  31.                 }
  32.                
  33.                 else{
  34.                     return true;
  35.                 }
  36.                
  37.        
  38.                
  39.                
  40.                
  41.                 if (((tilecol == blankcol) && (tilerow == blankrow +1 || tilerow == blankrow -1)) || ((tilerow == blankrow) && (tilecol == blankcol + 1 || tilecol == blankcol - 1))){
  42.                 // legal move is permitted...blankrow and col must be updated once swap takes places
  43.                 temp = board[blankrow][blankcol];
  44.                 board[blankrow][blankcol] = tile;
  45.                 tile = temp;
  46.                 blankrow = tilerow;
  47.                 blankcol = tilecol;
  48.                 printf("Legal move");
  49.                     return true;
  50.                    
  51.                 }
  52.                
  53.                 else{
  54.                     return false;
  55.                 }
  56.                
  57.             }
  58.                
  59.            
  60.            
  61.         return true;
  62.     }
Add Comment
Please, Sign In to add comment