Question
I need help writing the following java method for a sliding tile game inputs:Two integer arrays. The first array contains the tile location coordinates (row
I need help writing the following java method for a sliding tile game
inputs:Two integer arrays. The first array contains the tile location coordinates (row and col) and the second array contains the empty spot coordinates (row and col).
return:true if the chosen tile and the empty spot are neighbors. Returns false if they are not.
functionality: This method switches the chosen tile number (lets say tile number 7and the empty spot (0) in the 2D board array.
this is the code I have and I need help debugging
private int getUserSelectedTile() { System.out.println("Enter the tile number"); Scanner k = new Scanner(System.in); while (true) { try { int userTile = k.nextInt(); if(userTile<=15 && userTile>=0){ return userTile; } else{ System.out.println("Not a valid number"); } } catch (Exception e) { System.out.println("Please enter a valid number"); k.nextLine(); } } }
public static int[] getTileLocation(int userTile,int [] [] gameBoard){ for (int row=0;row public static boolean canTileBeMoved(int[] tileLocation, int[] emptyLocationOnBoard){ if((emptyLocationOnBoard[0]==tileLocation[0]-1)&&(emptyLocationOnBoard[1]==tileLocation[1])){ return true; }if((emptyLocationOnBoard[0]==tileLocation[0]+1)&&(emptyLocationOnBoard[1]==tileLocation[1])){ return true; }if((emptyLocationOnBoard[0]==tileLocation[0])&&(emptyLocationOnBoard[1]==tileLocation[1]+1)){ return true; }if((emptyLocationOnBoard[0]==tileLocation[0])&&(emptyLocationOnBoard[1]==tileLocation[1]-1)){ return true; }else{ return false; } } public static boolean moveTile(int[] tileLocation, int[] emptyLocationOnBoard, int[][] gameBoard){ if(canTileBeMoved(int[] tileLocation, int[] emptyLocationOnBoard)){ gameBoard[emptyLocationOnBoard[0]][emptyLocationOnBoard[1]]=gameBoard[tileLocation[0]][tileLocation[1]]; gameBoard[tileLocation[0]][tileLocation[1]]=0; return true; } else{ return false; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started