brute-force funktioniert, 3x3 felder damit man es überhaupt brauchen kann
This commit is contained in:
1000
descriptions/3x3.txt
Normal file
1000
descriptions/3x3.txt
Normal file
File diff suppressed because it is too large
Load Diff
28
src/GUI.py
28
src/GUI.py
@@ -77,7 +77,16 @@ class NetGUI:
|
|||||||
for y in range(height):
|
for y in range(height):
|
||||||
self.pieceSprites.add(PieceSprite(x, y))
|
self.pieceSprites.add(PieceSprite(x, y))
|
||||||
|
|
||||||
|
def update_display(self, events: list[pygame.event.Event]):
|
||||||
|
self.pieceSprites.update(self.game, events)
|
||||||
|
for sprite in self.pieceSprites:
|
||||||
|
self.window.blit(sprite.image, sprite.rect)
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
def run_game(self):
|
def run_game(self):
|
||||||
|
current_solver = None
|
||||||
|
display_solver = False
|
||||||
while not self.game.solved():
|
while not self.game.solved():
|
||||||
events = pygame.event.get()
|
events = pygame.event.get()
|
||||||
|
|
||||||
@@ -92,14 +101,17 @@ class NetGUI:
|
|||||||
self.game.lock(event.x, event.y)
|
self.game.lock(event.x, event.y)
|
||||||
elif event.type == pygame.KEYDOWN:
|
elif event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_b:
|
if event.key == pygame.K_b:
|
||||||
BruteForceSolver(self.game).solve()
|
current_solver = BruteForceSolver(self.game).solve()
|
||||||
|
if event.key == pygame.K_d:
|
||||||
self.pieceSprites.update(self.game, events)
|
display_solver = not display_solver
|
||||||
for sprite in self.pieceSprites:
|
if current_solver:
|
||||||
self.window.blit(sprite.image, sprite.rect)
|
try:
|
||||||
|
_ = next(current_solver)
|
||||||
pygame.display.flip()
|
except StopIteration:
|
||||||
|
current_solver = None
|
||||||
|
if (not current_solver) or display_solver:
|
||||||
|
self.update_display(events)
|
||||||
sleep(2)
|
sleep(2)
|
||||||
|
|
||||||
|
|
||||||
NetGUI(5, 5).run_game()
|
NetGUI(3, 3).run_game()
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from collections.abc import Generator
|
||||||
from itertools import chain, pairwise
|
from itertools import chain, pairwise
|
||||||
|
|
||||||
from src.net import NetGame
|
from src.net import NetGame
|
||||||
@@ -13,11 +14,17 @@ class BruteForceSolver:
|
|||||||
for y in range(game.width):
|
for y in range(game.width):
|
||||||
self.game.set_direction(x, y, Direction.UP)
|
self.game.set_direction(x, y, Direction.UP)
|
||||||
|
|
||||||
def solve(self) -> None:
|
def solve(self) -> Generator[None]:
|
||||||
attempts = 0
|
attempts = 0
|
||||||
|
required = 4 ** (self.game.width * self.game.height)
|
||||||
while not self.game.solved():
|
while not self.game.solved():
|
||||||
|
yield
|
||||||
attempts += 1
|
attempts += 1
|
||||||
print(f"{attempts}/{4 ** (self.game.width * self.game.height)}")
|
|
||||||
|
print(
|
||||||
|
f"{attempts:0>{len(str(required))}}/{required} ({attempts / required * 100}%) ",
|
||||||
|
end="\r",
|
||||||
|
)
|
||||||
self.game.turn_cw(0, 0)
|
self.game.turn_cw(0, 0)
|
||||||
for prev, curr in pairwise(
|
for prev, curr in pairwise(
|
||||||
chain(
|
chain(
|
||||||
@@ -26,3 +33,5 @@ class BruteForceSolver:
|
|||||||
):
|
):
|
||||||
if prev.direction == Direction.UP:
|
if prev.direction == Direction.UP:
|
||||||
curr.turn_cw()
|
curr.turn_cw()
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class Grid:
|
|||||||
def __init__(self, width: int, height: int) -> None:
|
def __init__(self, width: int, height: int) -> None:
|
||||||
self.height = height
|
self.height = height
|
||||||
self.width = width
|
self.width = width
|
||||||
if width != height or width not in [5, 7, 9, 11, 13]:
|
if width != height or width not in [3, 5, 7, 9, 11, 13]:
|
||||||
raise ValueError("Feldgrösse nicht erlaubt")
|
raise ValueError("Feldgrösse nicht erlaubt")
|
||||||
with open(f"descriptions/{width}x{height}.txt") as f:
|
with open(f"descriptions/{width}x{height}.txt") as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
|
|||||||
Reference in New Issue
Block a user