brute-force
This commit is contained in:
18
src/GUI.py
18
src/GUI.py
@@ -1,8 +1,10 @@
|
||||
import os
|
||||
from time import sleep
|
||||
from typing import override
|
||||
|
||||
import pygame
|
||||
|
||||
from src.algorithms.bruteforce import BruteForceSolver
|
||||
from src.net import NetGame
|
||||
|
||||
# pyright: reportUnusedCallResult=false, reportAny=false
|
||||
@@ -59,7 +61,9 @@ class PieceSprite(pygame.sprite.Sprite):
|
||||
class NetGUI:
|
||||
game: NetGame
|
||||
window: pygame.Surface
|
||||
pieceSprites: pygame.sprite.Group[PieceSprite] # pyright: ignore[reportInvalidTypeArguments]
|
||||
pieceSprites: pygame.sprite.Group[
|
||||
PieceSprite # pyright: ignore[reportInvalidTypeArguments]
|
||||
]
|
||||
|
||||
def __init__(self, width: int, height: int):
|
||||
self.game = NetGame(width, height)
|
||||
@@ -74,7 +78,7 @@ class NetGUI:
|
||||
self.pieceSprites.add(PieceSprite(x, y))
|
||||
|
||||
def run_game(self):
|
||||
while True:
|
||||
while not self.game.solved():
|
||||
events = pygame.event.get()
|
||||
|
||||
for event in events:
|
||||
@@ -82,20 +86,20 @@ class NetGUI:
|
||||
raise SystemExit
|
||||
elif event.type == LEFT_TURN:
|
||||
self.game.turn_ccw(event.x, event.y)
|
||||
if self.game.solved():
|
||||
raise SystemExit # TODO: Richtiger Spiel schluss, nicht einfach schliessen
|
||||
elif event.type == RIGHT_TURN:
|
||||
self.game.turn_cw(event.x, event.y)
|
||||
if self.game.solved():
|
||||
raise SystemExit
|
||||
elif event.type == LOCK:
|
||||
self.game.lock(event.x, event.y)
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_b:
|
||||
BruteForceSolver(self.game).solve()
|
||||
|
||||
self.pieceSprites.update(self.game, events)
|
||||
for sprite in self.pieceSprites:
|
||||
self.window.blit(sprite.image, sprite.rect)
|
||||
|
||||
pygame.display.flip()
|
||||
sleep(2)
|
||||
|
||||
|
||||
NetGUI(13, 13).run_game()
|
||||
NetGUI(5, 5).run_game()
|
||||
|
||||
28
src/algorithms/bruteforce.py
Normal file
28
src/algorithms/bruteforce.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from itertools import chain, pairwise
|
||||
|
||||
from src.net import NetGame
|
||||
from src.netTypes import Direction
|
||||
|
||||
|
||||
class BruteForceSolver:
|
||||
game: NetGame
|
||||
|
||||
def __init__(self, game: NetGame) -> None:
|
||||
self.game = game
|
||||
for x in range(game.height):
|
||||
for y in range(game.width):
|
||||
self.game.set_direction(x, y, Direction.UP)
|
||||
|
||||
def solve(self) -> None:
|
||||
attempts = 0
|
||||
while not self.game.solved():
|
||||
attempts += 1
|
||||
print(f"{attempts}/{4 ** (self.game.width * self.game.height)}")
|
||||
self.game.turn_cw(0, 0)
|
||||
for prev, curr in pairwise(
|
||||
chain(
|
||||
*self.game.get_field()
|
||||
) # 2d-liste zu 1d machen, damit man einfacher über alle zellen iterieren kann
|
||||
):
|
||||
if prev.direction == Direction.UP:
|
||||
curr.turn_cw()
|
||||
@@ -7,8 +7,6 @@ from random import choice
|
||||
from src.interface import parse_description
|
||||
from src.netTypes import Piece, Direction, Coordinate
|
||||
|
||||
DEMO_FIELD = parse_description("5x5:c7634887c213e5b8db3e69282")
|
||||
|
||||
|
||||
class Grid:
|
||||
pieces: list[list[Piece]]
|
||||
@@ -70,9 +68,13 @@ class Grid:
|
||||
|
||||
class NetGame:
|
||||
_grid: Grid
|
||||
width: int
|
||||
height: int
|
||||
|
||||
def __init__(self, width: int, height: int) -> None:
|
||||
self._grid = Grid(width, height)
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
def get_field(self) -> list[list[Piece]]:
|
||||
return self._grid.pieces
|
||||
@@ -89,5 +91,8 @@ class NetGame:
|
||||
def lock(self, x: int, y: int) -> None:
|
||||
self._grid.pieces[x][y].locked = not self._grid.pieces[x][y].locked
|
||||
|
||||
def set_direction(self, x: int, y: int, dir: Direction) -> None:
|
||||
self._grid.pieces[x][y].direction = dir
|
||||
|
||||
def solved(self):
|
||||
return self._grid.solved()
|
||||
|
||||
Reference in New Issue
Block a user