Answered step by step
Verified Expert Solution
Question
1 Approved Answer
why does my code keep looping infintely, i provided all my code below: import java.util.HashMap; import java.util.Random; public class SteepestHill { private int [ ]
why does my code keep looping infintely, i provided all my code below:
import java.util.HashMap;
import java.util.Random;
public class SteepestHill
private int test;
public OutputData solveBoard board
int current board.getBoard;
int searchCost ;
whiletrue
test getBestSuccessorcurrent; get the best successor from the current board
searchCost; increase search cost
ifgetFitnesscurrent getFitnesstestif amount of attacking queens in current board is lower than best successor, choose the current board to return
test current.clone;
Board b new Boardtest; create a board with the current board
return new OutputDatagetFitnesstest b searchCost; return output data with either truefalse solved value and search cost
current test; if the value of the successor is lower than the current puzzle, set the current puzzle as the successor
private int getBestSuccessorint puzzleget the best successor of the current node
int board puzzle.clone;
HashMap moves new HashMap; store possible moves that can be made on the board
for int col ; col board.length; col
HashMap move new HashMap; store row values that can be placed in the current column
for int row ; row board.length; rowiterate through rows of the array
if boardcol row
continue;
int boardCopy board.clone;
boardCopycol row; if current row is not value of the column, place row value in a clone of the current board
move.putrow getFitnessboardCopy; store row value and fitness of the clone
moves.putcol move; store all possible row moves
int hToBeat getFitnessboard; calculate fitness of original board to test other values
HashMap bestMoves new HashMap; used to store moves which contain best fitness values
for HashMap entry : moves.valuesiterate through each entry from all possible moves
for int row : entry.keySet
int hCost entry.getrow;
if hCost hToBeatif attacking queens of entry is lower than current board, set fitness value to beat as the current entry
hToBeat hCost;
for int col : moves.keySet
HashMap entry moves.getcol;
for int row : entry.keySet
int hCost entry.getrow; get fitness value of the current row
if hCost hToBeatif fitness value is equal to the fitness value to beat, add the columnrow values to the best possible moves
bestMoves.putcol row;
if bestMovessizeif best moves is not empty, pick a random move from best moves and return that board
Random rand new Random;
int pick rand.nextIntbestMovessize;
int col int bestMoves.keySettoArraypick;
int row bestMoves.getcol;
boardcol row;
return board;
private int getFitnessint boardcalculates how many queens are in the same rowcolumndiagonal
int fitness ;
forint i ; i board.length ; i
forint j i ; j board.length; j
ifboardi boardjsame row
fitness;
else ifj i Math.absboardj boardisame diagonal
fitness;
return fitness;
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