Skip to content

Commit

Permalink
Fixes bugs in last_moves_dist and relaxed_adjacency_dist
Browse files Browse the repository at this point in the history
  • Loading branch information
entangledloops committed Jan 21, 2024
1 parent 2366668 commit 09c62cf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/slidingpuzzle/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def last_moves_distance(board: Board, relaxed: bool = True) -> int:
corner_adj1 = board[-1, -2]
corner_adj2 = board[-2, -1]
# if one of the adjacents is already in its dest, can't add any more
if {corner_adj1, corner_adj2} & {adj3, adj3, adj5}:
if {corner_adj1, corner_adj2} & {adj3, adj4, adj5}:
return dist

if not relaxed:
Expand Down Expand Up @@ -502,6 +502,7 @@ def swap_first_misplaced(blank_yx: tuple[int, int]) -> None:
for x in range(w):
if get_goal_yx(h, w, board[y][x]) != (y, x):
swap_tiles(board, (y, x), blank_yx)
return

while not is_solved(board):
blank_yx = find_blank(board)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ def test_relaxed_adjacency_distance():
swap_tiles(b, (h - 1, w - 1), (0, 0))
assert relaxed_adjacency_distance(b) == 1

h, w = 3, 3
b = new_board(h, w)
swap_tiles(b, (0, 0), (0, 1))
swap_tiles(b, (1, 0), (1, 1))
assert relaxed_adjacency_distance(b) == 6

b = new_board(h, w)
swap_tiles(b, (0, 0), (0, 1))
swap_tiles(b, (0, 0), (h - 1, w - 1))
Expand Down

0 comments on commit 09c62cf

Please sign in to comment.