MCPcopy Index your code
hub / github.com/geekcomputers/Python / neighbors

Method neighbors

8_puzzle.py:47–59  ·  view source on GitHub ↗

Generate all valid neighboring states by moving empty tile (0).

(self)

Source from the content-addressed store, hash-verified

45 return self.board == self.goal
46
47 def neighbors(self) -> List["PuzzleState"]:
48 """Generate all valid neighboring states by moving empty tile (0)."""
49 neighbors = []
50 x, y = next((i, j) for i in range(3) for j in range(3) if self.board[i][j] == 0)
51 for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
52 nx, ny = x + dx, y + dy
53 if 0 <= nx < 3 and 0 <= ny < 3:
54 new_board = [row[:] for row in self.board]
55 new_board[x][y], new_board[nx][ny] = new_board[nx][ny], new_board[x][y]
56 neighbors.append(
57 PuzzleState(new_board, self.goal, self.moves + 1, self)
58 )
59 return neighbors
60
61
62def solve_puzzle(

Callers 1

solve_puzzleFunction · 0.80

Calls 2

PuzzleStateClass · 0.85
appendMethod · 0.45

Tested by

no test coverage detected