Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 23rd International Conference Dexa 2012 Vienna Austria September 2012 Proceedings Part 1 Lncs 7446

Authors: Stephen W. Liddle ,Klaus-Dieter Schewe ,A Min Tjoa ,Xiaofang Zhou

2012th Edition

3642325998, 978-3642325991

More Books

Students also viewed these Databases questions