Thanks to visit codestin.com
Credit goes to github.com

Skip to content

This Fixes #824 #825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,44 +434,51 @@ def actions(self, state):

possible_actions = list()
index_blank_square = self.find_blank_square(state)
(row, col) = index_blank_square

if index_blank_square(0) == 0:
if row == 0:
possible_actions += ['DOWN']
elif index_blank_square(0) == 1:
elif row == 1:
possible_actions += ['UP', 'DOWN']
elif index_blank_square(0) == 2:
elif row == 2:
possible_actions += ['UP']

if index_blank_square(1) == 0:
if col == 0:
possible_actions += ['RIGHT']
elif index_blank_square(1) == 1:
elif col == 1:
possible_actions += ['LEFT', 'RIGHT']
elif index_blank_square(1) == 2:
elif col == 2:
possible_actions += ['LEFT']

return possible_actions

def result(self, state, action):
"""Given state and action, return a new state that is the result of the action.
Action is assumed to be a valid action in the state."""

blank_square = self.find_blank_square(state)

new_state = [row[:] for row in state]
index_blank_square = self.find_blank_square(state)
(row, col) = index_blank_square

if action == 'UP':
new_state[row][col] = new_state[row - 1][col]
new_state[row - 1][col] = 0

elif action == 'LEFT':
new_state[row][col] = new_state[row][col - 1]
new_state[row][col - 1] = 0

elif action == 'DOWN':
new_state[row][col] = new_state[row + 1][col]
new_state[row + 1][col] = 0

elif action == 'RIGHT':
new_state[row][col] = new_state[row][col + 1]
new_state[row][col + 1] = 0

if action=='UP':
new_state[blank_square(0)][blank_square(1)] = new_state[blank_square(0)-1][blank_square(1)]
new_state[blank_square(0)-1][blank_square(1)] = 0
elif action=='LEFT':
new_state[blank_square(0)][blank_square(1)] = new_state[blank_square(0)][blank_square(1)-1]
new_state[blank_square(0)][blank_square(1)-1] = 0
elif action=='DOWN':
new_state[blank_square(0)][blank_square(1)] = new_state[blank_square(0)+1][blank_square(1)]
new_state[blank_square(0)+1][blank_square(1)] = 0
elif action=='RIGHT':
new_state[blank_square(0)][blank_square(1)] = new_state[blank_square(0)][blank_square(1)+1]
new_state[blank_square(0)][blank_square(1)+1] = 0
else:
print("Invalid Action!")

return new_state

def goal_test(self, state):
Expand Down