Descriptions benutzen und andere Feldgrössen als 5x5 erlauben

This commit is contained in:
Alfred Baumann
2026-06-02 17:50:24 +02:00
parent a903f5eb91
commit 7b7df0f7a8
6 changed files with 84 additions and 56 deletions

61
src/netTypes.py Normal file
View File

@@ -0,0 +1,61 @@
from enum import IntEnum, auto
type Coordinate = tuple[int, int]
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()
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
locked: bool = False
def __init__(self, type: PieceType, direction: Direction = Direction.UP) -> None:
self.type = type
self.direction = direction
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:
if self.locked:
return
self.direction = Direction((self.direction + 1) % 4)
def turn_ccw(self) -> None:
if self.locked:
return
self.direction = Direction((self.direction - 1) % 4)