Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool move(int tile)
- {
- int temp;
- int blankrow;
- int blankcol;
- int tilerow;
- int tilecol;
- for (int row = 0; row < d; row++){
- for (int col = 0; col < d; col++){
- // find zero tile position
- if (board[row][col] == 0){
- blankrow = row;
- blankcol = col;
- }
- // find target tile position
- if (board[row][col] == tile) {
- tilerow = row;
- tilecol = col;
- }
- }
- // determine if tile move is out of bounds
- if (tilerow > d-1 || tilecol > d-1 || tilerow > d-1 || tilecol > d-1){
- return false;
- }
- else{
- return true;
- }
- if (((tilecol == blankcol) && (tilerow == blankrow +1 || tilerow == blankrow -1)) || ((tilerow == blankrow) && (tilecol == blankcol + 1 || tilecol == blankcol - 1))){
- // legal move is permitted...blankrow and col must be updated once swap takes places
- temp = board[blankrow][blankcol];
- board[blankrow][blankcol] = tile;
- tile = temp;
- blankrow = tilerow;
- blankcol = tilecol;
- printf("Legal move");
- return true;
- }
- else{
- return false;
- }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment