Question: FIX THE FOLLOWING CODE , make sure its working by testing the below inputs. A = 1 2 3 4 5 6 7 8 0

FIX THE FOLLOWING CODE , make sure its working by testing the below inputs.
A =1234567809000(Does reach goal state)
B=0234567891000(Doesnt reach goal state)
import java.io.*;
import java.util.*;
class State implements Comparable {
private int hValue;
private int[] board;
public State(int[] board, int hValue){
this.hValue = hValue;
this.board = board;
}
@Override
public int compareTo(State o){
return Integer.compare(this.hValue, o.hValue);
}
public int getHValue(){
return hValue;
}
List generateChildren(){
List children = new ArrayList<>();
for (int i =0; i < board.length; i++){
if (board[i]!=0){
// Check the left adjacent cell
if (i -1>=0 && board[i -1]==0){
int[] newBoard = Arrays.copyOf(board, board.length);
newBoard[i -1]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
// Check the right adjacent cell
if (i +1< board.length && board[i +1]==0){
int[] newBoard = Arrays.copyOf(board, board.length);
newBoard[i +1]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
// Move between positions 4 & 11
if ((i ==3 && board[10]==0)||(i ==10 && board[3]==0)){
int[] newBoard = Arrays.copyOf(board, board.length);
int newPosition =(i ==3)?10 : 3;
newBoard[newPosition]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
// Move between positions 6 & 12
if ((i ==5 && board[11]==0)||(i ==11 && board[5]==0)){
int[] newBoard = Arrays.copyOf(board, board.length);
int newPosition =(i ==5)?11 : 5;
newBoard[newPosition]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
// Move between positions 8 & 13
if ((i ==7 && board[12]==0)||(i ==12 && board[7]==0)){
int[] newBoard = Arrays.copyOf(board, board.length);
int newPosition =(i ==7)?12 : 7;
newBoard[newPosition]= newBoard[i];
newBoard[i]=0;
children.add(new State(newBoard, calcHeuristic(newBoard)));
}
}
}
return children;
}
private int calcHeuristic(int[] board){
int disp =0;
for (int i =0; i < board.length; i++){
if (board[i]!= i)
disp++;
}
return disp;
}
public void displayState(FileWriter output) throws IOException {
output.write("h ="+ hValue +"
");
for (int i =0; i < board.length; i++)
output.write(board[i]+"");
output.write("
");
}
public class TEST {
public static void main(String[] args){
try {
File file = new File("inputC.txt");
Scanner sc = new Scanner(file);
int[] iniState = new int[14];
for (int i =0; i <10; i++)
iniState[i]= sc.nextInt();
State initialState = new State(iniState, calcHeuristic(iniState));
BFS(initialState);
sc.close();
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
// BFS Algorithm
private static void BFS(State initialState){
PriorityQueue frontier = new PriorityQueue<>(Comparator.comparingInt(State::getHValue));
frontier.add(initialState);
Set visited = new HashSet<>();
int moves =0;
try {
FileWriter output = new FileWriter("output.txt");
while (!frontier.isEmpty()){
State currState = frontier.poll();
visited.add(currState);
currState.displayState(output);
if (currState.getHValue()==0){
output.write("Goal state reached.
Number of moves: "+ moves);
output.close();
return;
}
for (State child : currState.generateChildren()){
if (!visited.contains(child) && !frontier.contains(child))
frontier.add(child);
}
if (moves >=700){
output.write("
Max no of moves reached")
return;}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!