-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreadth_first.py
More file actions
56 lines (45 loc) · 2.11 KB
/
breadth_first.py
File metadata and controls
56 lines (45 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from ..classes.board import Board
from typing import Deque
from collections import deque
import sys
class BreadthFirst:
"""
Looks at all possible boards at a the current depth before moving to the next depth.
This guarantees it will find the minimum number of steps necessary to solve the board.
"""
def __init__(self, startBoard: Board, display: bool = False) -> None:
self.startBoard = startBoard
self.visit = {startBoard}
self.q: Deque[Board] = deque([startBoard])
self.depth = 0
self.display_bfs = display
def availableMoves(self, currentBoard: Board) -> list[Board]:
"""Returns all available moves for the current board."""
return currentBoard.moves()
def handleNewBoard(self, newBoard: Board, currentBoard: Board) -> None:
"""Adds the new board to the visit set and the queue. Current board not used here."""
self.visit.add(newBoard)
self.q.append(newBoard)
def reset(self) -> None:
"""Not necessary here but is used when used multiple times (shortened path random)."""
pass
def runBF(self) -> list[Board]:
"""Tries every possible move at every board, does not look at the same board twice."""
while self.q:
for _ in range(len(self.q)):
currentBoard = self.q.popleft()
# When the game is solved return the winning path
if currentBoard.isSolved():
self.reset()
return currentBoard.get_path()
# Check all moves that could be made and add the new board to the queue
for newBoard in self.availableMoves(currentBoard):
# Check if you already have seen this board if so skip else add it to visit
if newBoard in self.visit:
continue
self.handleNewBoard(newBoard, currentBoard)
if self.display_bfs:
print(f"No solution found at depth {self.depth}.\n")
self.depth += 1
print("\nNo solution, try different parameters.")
sys.exit()