Descriptions benutzen und andere Feldgrössen als 5x5 erlauben
This commit is contained in:
61
src/net.py
61
src/net.py
@@ -2,47 +2,10 @@
|
||||
Kernlogik des Spiels
|
||||
"""
|
||||
|
||||
from src.types import Piece, Direction
|
||||
from random import choice
|
||||
|
||||
from src.interface import parse_description
|
||||
|
||||
|
||||
# DEMO_FIELD = [
|
||||
# [
|
||||
# Piece(PieceType.NODE),
|
||||
# Piece(PieceType.NODE),
|
||||
# Piece(PieceType.T_JUNCTION),
|
||||
# Piece(PieceType.NODE),
|
||||
# Piece(PieceType.NODE),
|
||||
# ],
|
||||
# [
|
||||
# Piece(PieceType.STRAIGHT),
|
||||
# Piece(PieceType.NODE),
|
||||
# Piece(PieceType.T_JUNCTION),
|
||||
# Piece(PieceType.STRAIGHT),
|
||||
# Piece(PieceType.CORNER),
|
||||
# ],
|
||||
# [
|
||||
# Piece(PieceType.T_JUNCTION),
|
||||
# Piece(PieceType.T_JUNCTION),
|
||||
# Piece(PieceType.T_JUNCTION),
|
||||
# Piece(PieceType.T_JUNCTION),
|
||||
# Piece(PieceType.NODE),
|
||||
# ],
|
||||
# [
|
||||
# Piece(PieceType.STRAIGHT),
|
||||
# Piece(PieceType.NODE),
|
||||
# Piece(PieceType.T_JUNCTION),
|
||||
# Piece(PieceType.T_JUNCTION),
|
||||
# Piece(PieceType.NODE),
|
||||
# ],
|
||||
# [
|
||||
# Piece(PieceType.NODE),
|
||||
# Piece(PieceType.NODE),
|
||||
# Piece(PieceType.CORNER),
|
||||
# Piece(PieceType.CORNER),
|
||||
# Piece(PieceType.CORNER),
|
||||
# ],
|
||||
# ]
|
||||
from src.netTypes import Piece, Direction, Coordinate
|
||||
|
||||
DEMO_FIELD = parse_description("5x5:c7634887c213e5b8db3e69282")
|
||||
|
||||
@@ -55,9 +18,12 @@ class Grid:
|
||||
def __init__(self, width: int, height: int) -> None:
|
||||
self.height = height
|
||||
self.width = width
|
||||
self.pieces = (
|
||||
DEMO_FIELD # TODO: Field generation or import from Tatham's version
|
||||
)
|
||||
if width != height or width not in [5, 7, 9, 11, 13]:
|
||||
raise ValueError("Feldgrösse nicht erlaubt")
|
||||
with open(f"descriptions/{width}x{height}.txt") as f:
|
||||
lines = f.readlines()
|
||||
selected = choice(lines).strip()
|
||||
self.pieces = parse_description(selected)
|
||||
|
||||
def neighbors(self, x: int, y: int) -> list[Coordinate]:
|
||||
neighbors: list[Coordinate] = []
|
||||
@@ -82,7 +48,7 @@ class Grid:
|
||||
) -> list[list[bool]]:
|
||||
connected[x][y] = True
|
||||
|
||||
connectedNeighbors: list[Coordinate] = []
|
||||
connected_neighbors: list[Coordinate] = []
|
||||
for nx, ny in self.neighbors(x, y):
|
||||
dx = x - nx
|
||||
dy = y - ny
|
||||
@@ -92,9 +58,9 @@ class Grid:
|
||||
and Direction.from_offset(-dx, -dy)
|
||||
in self.pieces[x][y].connected_directions()
|
||||
):
|
||||
connectedNeighbors.append((nx, ny))
|
||||
connected_neighbors.append((nx, ny))
|
||||
|
||||
for nx, ny in connectedNeighbors:
|
||||
for nx, ny in connected_neighbors:
|
||||
if connected[nx][ny]:
|
||||
continue
|
||||
connected = self._solve_floodfill(nx, ny, connected)
|
||||
@@ -119,6 +85,9 @@ class NetGame:
|
||||
|
||||
def turn_ccw(self, x: int, y: int) -> None:
|
||||
self._grid.pieces[x][y].turn_ccw()
|
||||
|
||||
def lock(self, x: int, y: int) -> None:
|
||||
self._grid.pieces[x][y].locked = not self._grid.pieces[x][y].locked
|
||||
|
||||
def solved(self):
|
||||
return self._grid.solved()
|
||||
|
||||
Reference in New Issue
Block a user