parser für die Descriptions, die Tatham's version generiert

This commit is contained in:
Alfred Baumann
2026-05-15 21:27:13 +02:00
parent a7fa8ca9c3
commit d49493b778
3 changed files with 161 additions and 92 deletions

View File

@@ -2,101 +2,49 @@
Kernlogik des Spiels
"""
from enum import IntEnum, auto
type Coordinate = tuple[int, int]
from src.types import Piece, Direction
from src.interface import parse_description
class PieceType(IntEnum):
T_JUNCTION = auto() # Direction is the piece at the "stem" of the T
STRAIGHT = auto()
CORNER = auto() # Direction is the more counter-clockwise connection
NODE = auto()
# 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),
# ],
# ]
class Direction(IntEnum):
UP = 0
RIGHT = auto()
DOWN = auto()
LEFT = auto()
@staticmethod
def from_offset(dx: int, dy: int) -> Direction:
if (dx != 0 and dy != 0) or abs(dx + dy) != 1:
raise ValueError(f"({dx}, {dy}) is not a valid direction offset")
if dx != 0:
return Direction(dx % 4)
else:
return Direction(dy + 1)
class Piece:
type: PieceType
direction: Direction = Direction.UP
locked: bool = False
def __init__(self, type: PieceType) -> None:
self.type = type
def connected_directions(self) -> list[Direction]:
match self.type:
case PieceType.NODE:
return [self.direction]
case PieceType.CORNER:
return [self.direction, Direction((self.direction + 1) % 4)]
case PieceType.STRAIGHT:
return [self.direction, Direction((self.direction + 2) % 4)]
case PieceType.T_JUNCTION:
return [
self.direction,
Direction((self.direction + 1) % 4),
Direction((self.direction - 1) % 4),
]
def turn_cw(self) -> None:
self.direction = Direction((self.direction + 1) % 4)
def turn_ccw(self) -> None:
self.direction = Direction((self.direction - 1) % 4)
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),
],
]
DEMO_FIELD = parse_description("5x5:c7634887c213e5b8db3e69282")
class Grid: