Question: Use the following code in the file. # For solving 8-puzzle problem by using a* search. from simpleai.search import astar, SearchProblem # Class containing methods

Use the following code in the file.

# For solving 8-puzzle problem by using a* search.

from simpleai.search import astar, SearchProblem

# Class containing methods to solve the puzzle

class MyPuzzleSolver(SearchProblem):

# Action method to get the list of the possible

# numbers that can be moved in to the empty space

def actions(self, cur_state):

rows = string_to_list(cur_state)

row_empty, col_empty = get_location(rows, 'e')

actions = []

if row_empty > 0:

actions.append(rows[row_empty - 1][col_empty])

if row_empty < 2:

actions.append(rows[row_empty + 1][col_empty])

if col_empty > 0:

actions.append(rows[row_empty][col_empty - 1])

if col_empty < 2:

actions.append(rows[row_empty][col_empty + 1])

return actions

# Return the resulting state after moving a piece to the empty space

def result(self, state, action):

rows = string_to_list(state)

row_empty, col_empty = get_location(rows, 'e')

row_new, col_new = get_location(rows, action)

rows[row_empty][col_empty], rows[row_new][col_new] = \

rows[row_new][col_new], rows[row_empty][col_empty]

return list_to_string(rows)

# Returns true if a state is the goal state

def is_goal(self, state):

return state == GOAL

# Returns an estimate of the distance from a state to

# the goal using the manhattan distance

def heuristic(self, state):

rows = string_to_list(state)

distance = 0

for number in '12345678e':

row_new, col_new = get_location(rows, number)

row_new_goal, col_new_goal = goal_positions[number]

distance += abs(row_new - row_new_goal) + abs(col_new - col_new_goal)

return distance

# Convert list to string

def list_to_string(input_list):

return ' '.join(['-'.join(x) for x in input_list])

# Convert string to list

def string_to_list(input_string):

return [x.split('-') for x in input_string.split(' ')]

# Find the 2D location of the input element

def get_location(rows, input_element):

for i, row in enumerate(rows):

for j, item in enumerate(row):

if item == input_element:

return i, j

# Final result that we want to achieve

GOAL = '''1-2-3

4-5-6

7-8-e'''

# Starting point

INITIAL = '''1-e-2

6-3-4

7-5-8'''

if __name__=='__main__':

# Create a cache for the goal position of each piece

goal_positions = {}

rows_goal = string_to_list(GOAL)

print(rows_goal);

for number in '12345678e':

goal_positions[number] = get_location(rows_goal, number)

print(goal_positions);

# Create the solver object

result = astar(MyPuzzleSolver(INITIAL))

# Print the results

for i, (action, state) in enumerate(result.path()):

print()

if action == None:

print('Initial configuration')

elif i == len(result.path()) - 1:

print('After moving', action, 'into the empty space. Goal achieved!')

else:

print('After moving', action, 'into the empty space')

print(state)

Based on the python code above, design a game of 15-puzzle in python. It means that there are 1-15 and empty. Try to solve the 15-puzzle problem using A* search.

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!