Calculate Manhattan from 8-Puzzle games in Python

I am trying to write a simple A* solver in Python for a simple 8-Puzzle game.
I used this way to represent the goal of the game:

goal = [[1, 2, 3],
[8, 0, 4],
[7, 6, 5]]

My problem is that I don’t know how to write a simple Manhattan distance heuristic for my goal. I know it should be defined as the sum of the distances between the general state and my goal state. I think I should write similar code:

def manhattan_distance(state):
distance = 0
for x in xrange(3):
for y in xrange(3):
value = state[x][y]
x_value = x
y_value = y
x_goal = ...?
y_goal = ...?
distance += abs(x_value-x_goal) + abs(y_value-y_goal)
return distance

My problem is that I did not clearly indicate the target status The coordinates of the various parts in the board, so I don’t know how to define’x_goal’ and’y_goal’ for the “value” part of the board. I tried to use division and module operations to accomplish it, but it was difficult.

Can you give me some hints to define my’x_goal’ and’y_goal’ variables?

Thank you

You can find most pythonic implementations.

< /p>

Suppose,

0 1 2

3 4 5

6 7 8 < /p>

Is the target country…
and,

1 5 3

4 2 6

7 8 9

is the final state.

initial_state = [1,5,3,4 ,2,6,7,8,0]
goal_state = [0,1,2,3,4,5,6,7,8]
def calculateManhattan(initial_state):
initial_config = initial_state
manDict = 0
for i,item in enumerate(initial_config):
prev_row,prev_col = int(i/ 3), i% 3
goal_row,goal_col = int(item /3),item% 3
manDict += abs(prev_row-goal_row) + abs(prev_col-goal_col)
return manDict

I don’t know how to explain this. It just works. Please enjoy! :D

I am trying to write a simple A* solver in Python for a simple 8-Puzzle game.
I used this way to represent The goal of the game:

goal = [[1, 2, 3],
[8, 0, 4],
[7 , 6, 5]]

My problem is that I don’t know how to write a simple Manhattan distance heuristic algorithm for my goal. I know it should be defined as a general state and my goal state. The sum of the distances between them. I think I should write similar code:

def manhattan_distance(state):
distance = 0
for x in xrange (3):
for y in xrange(3):
value = state[x][y]
x_value = x
y_value = y
x_goal =. ..?
y_goal = ...?
distance += abs(x_value-x_goal) + abs(y_value-y_goal)
return distance

My question is I didn’t clearly indicate the coordinates of the various parts in the goal state, so I don’t know how to define’x_goal’ and’y_goal’ for the “value” part of the chessboard. I tried to use division and module operations to accomplish it, but it was difficult. /p>

Can you give me some hints to define my’x_goal’ and’y_goal’ variables?

Thank you

You can find most pythonic implementations.

If you say,

0 1 2

3 4 5

6 7 8

is the target Country…
and,

1 5 3

4 2 6

7 8 9

is the final state.

initial_state = [1,5,3,4,2,6,7,8,0]< br />goal_state = [0,1,2,3,4,5,6,7,8]
def calculateManhattan(initial_state):
initial_config = initial_state
manDict = 0
for i,item in enumerate(initial_config):
prev_row,prev_col = int(i/ 3), i% 3
goal_row,goal_col = int(item /3),item% 3
manDict += abs(prev_row-goal_row) + abs(prev_col-goal_col)
return manDict

I don’t know how to explain this. It just works. Please enjoy! : D

Leave a Comment

Your email address will not be published.