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

Skip to content

Fixed small errors in EightPuzzle class #987

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

Merged
merged 1 commit into from
Nov 30, 2018
Merged
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
8 changes: 4 additions & 4 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ def astar_search(problem, h=None):
class EightPuzzle(Problem):

""" The problem of sliding tiles numbered from 1 to 8 on a 3x3 board,
where one of the squares is a blank. A state is represented as a 3x3 list,
where element at index i,j represents the tile number (0 if it's an empty square) """
where one of the squares is a blank. A state is represented as a tuple of length 9,
where element at index i represents the tile number at index i (0 if it's an empty square) """

def __init__(self, initial, goal=(1, 2, 3, 4, 5, 6, 7, 8, 0)):
""" Define goal state and initialize a problem """
Expand Down Expand Up @@ -472,8 +472,8 @@ def check_solvability(self, state):

inversion = 0
for i in range(len(state)):
for j in range(i, len(state)):
if state[i] > state[j] != 0:
for j in range(i+1, len(state)):
if (state[i] > state[j]) and state[i] != 0 and state[j]!= 0:
inversion += 1

return inversion % 2 == 0
Expand Down