Question: Please complete this A-star framework (Python) for a 8-puzzle game: Class State{ int value[]; //9 number array int g_cost; //g() int h_cost; //h() State p;

Please complete this A-star framework (Python) for a 8-puzzle game:

Class State{

int value[]; //9 number array

int g_cost; //g() int h_cost; //h() State p; //parent

State(v[])

{

Values = v;

}

}

List mem_list; //The list of states that are reached but not expanded yet

List expanded_list; //Avoid repeating, this list store all the expanded states

aStarSearch(nums[])

{

State initial_state = State(nums)

mem_list.add(initial_state) //Add the initial state into list

State expanded_goal_state; //Hold the first expanded goal state

//Keep expanding states from the mem_list

While (mem_list != Empty)

{

Mem_list.remove(minimum_state) If(minimum_state == goal_state){

expanded_goal_state = minimum_state

break;}

Expanded_list.add(minimum_state) //If minimum_state.value = 214785630

List children = getChildren(minimum_state) //Children: 284715630,

//214875630, 214758630, 214735680

for child in children{

if(child is NOT in expanded_list){

child.parent = minimum_state

child.g_cost = minimum_state.g_cost + 1 child.h_cost = calcuateBy8and0Position() expanded_list.add(child)

}

}//End of For-loop of children

} //End of While-loop for expanding

State parent = expanded_goal_state.parent

List path=[]

Path.add(expanded_goal_state.get8pos())

While(parent != initial_state) //Trace back from goal state to initial state to get the path

{

Path.add(parent.get8pos())

}

retrun path.reverse()

}

import queue def swapPositions(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list def pos0268(pos, puz, parent_dict, q): child_1 = puz.copy() child_2 = puz.copy() if pos == 0: swapPositions(child_1, 0, 1) swapPositions(child_2, 0, 3) elif pos == 2: swapPositions(child_1, 2, 1) swapPositions(child_2, 2, 5) elif pos == 6: swapPositions(child_1, 6, 3) swapPositions(child_2, 6, 7) elif pos == 8: swapPositions(child_1, 8, 5) swapPositions(child_2, 8, 7) cur_key = ''.join(str(i) for i in puz) child_1_key = ''.join(str(i) for i in child_1) child_2_key = ''.join(str(i) for i in child_2) #add child_1 if child_1_key not in parent_dict: q.put(child_1) #push the new child into queue parent_dict.update({child_1_key: cur_key}) #map the child-parent relationship #add child_2 if child_2_key not in parent_dict: q.put(child_2) #push the new child into queue parent_dict.update({child_2_key: cur_key}) #map the child-parent relationship def pos1357(pos, puz, parent_dict, q): child_1 = puz.copy() child_2 = puz.copy() child_3 = puz.copy() if pos == 1: swapPositions(child_1, 1, 0) swapPositions(child_2, 1, 2) swapPositions(child_3, 1, 4) elif pos == 3: swapPositions(child_1, 3, 0) swapPositions(child_2, 3, 6) swapPositions(child_3, 3, 4) elif pos == 5: swapPositions(child_1, 5, 2) swapPositions(child_2, 5, 8) swapPositions(child_3, 5, 4) elif pos == 7: swapPositions(child_1, 7, 6) swapPositions(child_2, 7, 8) swapPositions(child_3, 7, 4) cur_key = ''.join(str(i) for i in puz) child_1_key = ''.join(str(i) for i in child_1) child_2_key = ''.join(str(i) for i in child_2) child_3_key = ''.join(str(i) for i in child_3) # add child_1 if child_1_key not in parent_dict: q.put(child_1) # push the new child into queue parent_dict.update({child_1_key: cur_key}) # map the child-parent relationship # add child_2 if child_2_key not in parent_dict: q.put(child_2) # push the new child into queue parent_dict.update({child_2_key: cur_key}) # map the child-parent relationship # add child_3 if child_3_key not in parent_dict: q.put(child_3) # push the new child into queue parent_dict.update({child_3_key: cur_key}) # map the child-parent relationship def pos4(pos, puz, parent_dict, q): child_1 = puz.copy() child_2 = puz.copy() child_3 = puz.copy() child_4 = puz.copy() swapPositions(child_1, 4, 1) swapPositions(child_2, 4, 3) swapPositions(child_3, 4, 5) swapPositions(child_4, 4, 7) cur_key = ''.join(str(i) for i in puz) child_1_key = ''.join(str(i) for i in child_1) child_2_key = ''.join(str(i) for i in child_2) child_3_key = ''.join(str(i) for i in child_3) child_4_key = ''.join(str(i) for i in child_4) # add child_1 if child_1_key not in parent_dict: q.put(child_1) # push the new child into queue parent_dict.update({child_1_key: cur_key}) # map the child-parent relationship # add child_2 if child_2_key not in parent_dict: q.put(child_2) # push the new child into queue parent_dict.update({child_2_key: cur_key}) # map the child-parent relationship # add child_3 if child_3_key not in parent_dict: q.put(child_3) # push the new child into queue parent_dict.update({child_3_key: cur_key}) # map the child-parent relationship # add child_4 if child_4_key not in parent_dict: q.put(child_4) # push the new child into queue parent_dict.update({child_4_key: cur_key}) # map the child-parent relationship #define a dictionary to map position to children potential_children = { 0: pos0268, 1: pos1357, 2: pos0268, 3: pos1357, 4: pos4, 5: pos1357, 6: pos0268, 7: pos1357, 8: pos0268 }

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!