344 lines
13 KiB
Python
344 lines
13 KiB
Python
# -*- coding: utf-8 -*-
|
|
# games/party/watten/models/watten_engine.py
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
import random
|
|
|
|
from games.party.watten.models.watten_rules import (
|
|
WATTEN_RANKS_32,
|
|
evaluate_trick_winner,
|
|
get_legal_cards_for_trick,
|
|
is_trump_card,
|
|
)
|
|
from models.bavarian_deck import BavarianCard, create_bavarian_deck
|
|
from resources.code_res.game_constants import (
|
|
BAVARIAN_SUITS,
|
|
WT_GESPANNT_POINTS,
|
|
WT_HAND_SIZE,
|
|
WT_NUM_PLAYERS,
|
|
WT_WIN_POINTS,
|
|
)
|
|
|
|
# Phasen einer einzelnen Watten-Hand innerhalb des Satzes (bis 11 Punkte)
|
|
PHASE_SCHOENERE_ASK = "schoenere_ask" # Vorhand darf "Schönere?" fragen oder Schlag ansagen
|
|
PHASE_SCHOENERE_REPLY = "schoenere_reply" # Geber antwortet auf "Schönere?" (Ja = neu geben / Nein = Pflicht)
|
|
PHASE_ANSAGE_SCHLAG = "ansage_schlag" # Vorhand (Ansager) bestimmt den Schlag (7..A)
|
|
PHASE_ANSAGE_TRUMPF = "ansage_trumpf" # Geber bestimmt die Trumpffarbe (E/B/H/S)
|
|
PHASE_TRICK_PLAY = "trick_play" # Ausspielen der bis zu 5 Stiche (oder Ausschaffen!)
|
|
PHASE_AUSSCHAFFEN_REPLY = "ausschaffen_reply" # Gegner-Team entscheidet: Halten (+1) oder Gehen!
|
|
PHASE_HAND_DONE = "hand_done" # Einzelhand beendet (3 Stiche erreicht oder Aufgabe)
|
|
PHASE_MATCH_DONE = "match_done" # Gesamtsatz beendet (>= 11 Punkte erreicht)
|
|
|
|
|
|
@dataclass
|
|
class WattenSeat:
|
|
seat_id: int
|
|
name: str
|
|
team_id: int # Team 0 = Sitz 0 (Du) & Sitz 2 (Partner Nord); Team 1 = Sitz 1 (West) & Sitz 3 (Ost)
|
|
is_human: bool = False
|
|
ai_level: int = 2
|
|
hand: list[BavarianCard] = field(default_factory=list)
|
|
status_text: str = ""
|
|
|
|
|
|
class WattenEngine:
|
|
"""
|
|
Oberbayerisches Watten (32 Karten, 2v2 über Kreuz bis 11 Gewinnpunkte):
|
|
- Team 0: Sitz 0 (Spieler Süd) + Sitz 2 (Partner Nord)
|
|
- Team 1: Sitz 1 (Gegner West) + Sitz 3 (Gegner Ost)
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.seats: list[WattenSeat] = []
|
|
self.team_points: list[int] = [0, 0]
|
|
self.team_tricks: list[int] = [0, 0]
|
|
|
|
self.dealer_seat: int = -1
|
|
self.ansager_seat: int = 0
|
|
self.turn_seat: int = 0
|
|
|
|
self.phase: str = PHASE_ANSAGE_SCHLAG
|
|
self.schoenere_used: bool = False
|
|
self.schlag_rank: str | None = None
|
|
self.trump_suit: str | None = None
|
|
|
|
self.current_stake: int = 2
|
|
self.proposed_stake: int = 3
|
|
self.last_raising_team: int | None = None
|
|
self.responder_seat: int = 0
|
|
self._saved_turn_before_raise: int = 0
|
|
|
|
self.trick_index: int = 0
|
|
self.current_trick: list[tuple[int, BavarianCard]] = []
|
|
self.last_completed_trick: list[tuple[int, BavarianCard]] = []
|
|
self.last_trick_winner: int | None = None
|
|
|
|
self.hand_winner_team: int | None = None
|
|
self.hand_points_awarded: int = 0
|
|
self.match_winner_team: int | None = None
|
|
self.hands_played: int = 0
|
|
|
|
@staticmethod
|
|
def team_of(seat_id: int) -> int:
|
|
return seat_id % 2
|
|
|
|
@property
|
|
def is_match_over(self) -> bool:
|
|
return self.match_winner_team is not None
|
|
|
|
def is_team_gespannt(self, team_id: int) -> bool:
|
|
return self.team_points[team_id] >= WT_GESPANNT_POINTS
|
|
|
|
def configure_seats(
|
|
self, human_name: str, bot_configs: list[tuple[int, str]]
|
|
) -> None:
|
|
"""
|
|
Sitz 0: Spieler (Team 0)
|
|
Sitz 1: Gegner Links / West (Team 1) <- bot_configs[0]
|
|
Sitz 2: Partner Oben / Nord (Team 0) <- bot_configs[1]
|
|
Sitz 3: Gegner Rechts / Ost (Team 1) <- bot_configs[2]
|
|
"""
|
|
self.seats = [WattenSeat(seat_id=0, name=human_name, team_id=0, is_human=True)]
|
|
for seat_id in range(1, WT_NUM_PLAYERS):
|
|
cfg_idx = seat_id - 1
|
|
lvl, b_name = (
|
|
bot_configs[cfg_idx]
|
|
if cfg_idx < len(bot_configs)
|
|
else (2, f"Bot {seat_id}")
|
|
)
|
|
self.seats.append(
|
|
WattenSeat(
|
|
seat_id=seat_id,
|
|
name=b_name,
|
|
team_id=self.team_of(seat_id),
|
|
is_human=False,
|
|
ai_level=lvl,
|
|
)
|
|
)
|
|
|
|
def start_new_match(self, rng: random.Random | None = None) -> None:
|
|
self.team_points = [0, 0]
|
|
self.match_winner_team = None
|
|
self.hands_played = 0
|
|
self.dealer_seat = (rng or random).randrange(WT_NUM_PLAYERS)
|
|
self.start_new_hand(rotate_dealer=False, rng=rng)
|
|
|
|
def start_new_hand(
|
|
self, rotate_dealer: bool = True, rng: random.Random | None = None
|
|
) -> None:
|
|
if rotate_dealer or self.dealer_seat < 0:
|
|
self.dealer_seat = (self.dealer_seat + 1) % WT_NUM_PLAYERS
|
|
self.ansager_seat = (self.dealer_seat + 1) % WT_NUM_PLAYERS
|
|
self.schoenere_used = False
|
|
self._deal_cards_internal(rng)
|
|
|
|
def _deal_cards_internal(self, rng: random.Random | None = None) -> None:
|
|
r = rng or random
|
|
deck = create_bavarian_deck(include_six=False, shuffle=True, rng=r)
|
|
suit_order = {s: i for i, s in enumerate(BAVARIAN_SUITS)}
|
|
|
|
for idx, seat in enumerate(self.seats):
|
|
chunk = deck[idx * WT_HAND_SIZE : (idx + 1) * WT_HAND_SIZE]
|
|
chunk.sort(key=lambda c: (suit_order[c.suit], c.order_index))
|
|
seat.hand = chunk
|
|
seat.status_text = ""
|
|
|
|
self.team_tricks = [0, 0]
|
|
self.schlag_rank = None
|
|
self.trump_suit = None
|
|
self.trick_index = 0
|
|
self.current_trick = []
|
|
self.last_completed_trick = []
|
|
self.last_trick_winner = None
|
|
self.hand_winner_team = None
|
|
self.hand_points_awarded = 0
|
|
|
|
# Ist mindestens ein Team gespannt (>= 9), zählt jede Hand automatisch 3 Punkte!
|
|
either_gespannt = self.is_team_gespannt(0) or self.is_team_gespannt(1)
|
|
self.current_stake = 3 if either_gespannt else 2
|
|
self.proposed_stake = self.current_stake + 1
|
|
self.last_raising_team = None
|
|
|
|
self.turn_seat = self.ansager_seat
|
|
# "Schönere?" ist erlaubt, solange keines der Teams gespannt ist und noch nicht neu gegeben wurde
|
|
if not either_gespannt and not self.schoenere_used:
|
|
self.phase = PHASE_SCHOENERE_ASK
|
|
else:
|
|
self.phase = PHASE_ANSAGE_SCHLAG
|
|
|
|
def sort_hands_for_ansage(self) -> None:
|
|
"""Sortiert nach erfolgter Ansage alle Trümpfe übersichtlich nach links auf die Hand."""
|
|
if not self.schlag_rank or not self.trump_suit:
|
|
return
|
|
suit_order = {s: i for i, s in enumerate(BAVARIAN_SUITS)}
|
|
for seat in self.seats:
|
|
seat.hand.sort(
|
|
key=lambda c: (
|
|
0 if is_trump_card(c, self.schlag_rank, self.trump_suit) else 1,
|
|
suit_order[c.suit],
|
|
c.order_index,
|
|
)
|
|
)
|
|
|
|
# --- 1. Schönere? & Ansagen (Schlag + Trumpf) ---
|
|
def ask_schoenere(self, seat_id: int) -> bool:
|
|
if self.phase != PHASE_SCHOENERE_ASK or seat_id != self.ansager_seat:
|
|
return False
|
|
self.phase = PHASE_SCHOENERE_REPLY
|
|
self.turn_seat = self.dealer_seat
|
|
return True
|
|
|
|
def skip_schoenere(self, seat_id: int) -> bool:
|
|
if self.phase != PHASE_SCHOENERE_ASK or seat_id != self.ansager_seat:
|
|
return False
|
|
self.phase = PHASE_ANSAGE_SCHLAG
|
|
self.turn_seat = self.ansager_seat
|
|
return True
|
|
|
|
def reply_schoenere(
|
|
self, seat_id: int, accept_redeal: bool, rng: random.Random | None = None
|
|
) -> bool:
|
|
if self.phase != PHASE_SCHOENERE_REPLY or seat_id != self.dealer_seat:
|
|
return False
|
|
self.schoenere_used = True
|
|
if accept_redeal:
|
|
self._deal_cards_internal(rng)
|
|
else:
|
|
self.phase = PHASE_ANSAGE_SCHLAG
|
|
self.turn_seat = self.ansager_seat
|
|
return True
|
|
|
|
def declare_schlag(self, seat_id: int, rank: str) -> bool:
|
|
if self.phase not in (PHASE_SCHOENERE_ASK, PHASE_ANSAGE_SCHLAG):
|
|
return False
|
|
if seat_id != self.ansager_seat or rank not in WATTEN_RANKS_32:
|
|
return False
|
|
self.schlag_rank = rank
|
|
self.phase = PHASE_ANSAGE_TRUMPF
|
|
self.turn_seat = self.dealer_seat
|
|
return True
|
|
|
|
def declare_trumpf(self, seat_id: int, suit: str) -> bool:
|
|
if self.phase != PHASE_ANSAGE_TRUMPF or seat_id != self.dealer_seat:
|
|
return False
|
|
if suit not in BAVARIAN_SUITS:
|
|
return False
|
|
self.trump_suit = suit
|
|
self.sort_hands_for_ansage()
|
|
self.phase = PHASE_TRICK_PLAY
|
|
self.turn_seat = self.ansager_seat
|
|
return True
|
|
|
|
# --- 2. Ausschaffen (Erhöhen) & Halten / Gehen ---
|
|
def can_seat_ausschaffen(self, seat_id: int) -> bool:
|
|
if self.phase != PHASE_TRICK_PLAY or seat_id != self.turn_seat:
|
|
return False
|
|
my_team = self.team_of(seat_id)
|
|
if self.is_team_gespannt(my_team):
|
|
return False
|
|
if self.last_raising_team == my_team:
|
|
return False
|
|
# Sinnvolle Obergrenze: sobald aktueller Einsatz ohnehin zum Match-Sieg beider Teams reicht
|
|
opp_team = 1 - my_team
|
|
if (
|
|
self.team_points[my_team] + self.current_stake >= WT_WIN_POINTS
|
|
and self.team_points[opp_team] + self.current_stake >= WT_WIN_POINTS
|
|
):
|
|
return False
|
|
return True
|
|
|
|
def initiate_ausschaffen(self, seat_id: int) -> bool:
|
|
if not self.can_seat_ausschaffen(seat_id):
|
|
return False
|
|
my_team = self.team_of(seat_id)
|
|
opp_team = 1 - my_team
|
|
self._saved_turn_before_raise = self.turn_seat
|
|
self.proposed_stake = self.current_stake + 1
|
|
self.last_raising_team = my_team
|
|
|
|
# Wenn der menschliche Spieler im Gegner-Team sitzt (opp_team == 0), antwortet IMMER der Mensch!
|
|
if opp_team == 0:
|
|
self.responder_seat = 0
|
|
else:
|
|
# Sonst antwortet der nächste aktive Gegner im Uhrzeigersinn
|
|
cand = (seat_id + 1) % WT_NUM_PLAYERS
|
|
self.responder_seat = cand if self.team_of(cand) == opp_team else (cand + 2) % WT_NUM_PLAYERS
|
|
|
|
self.phase = PHASE_AUSSCHAFFEN_REPLY
|
|
self.turn_seat = self.responder_seat
|
|
return True
|
|
|
|
def respond_ausschaffen(self, seat_id: int, accept_hold: bool) -> bool:
|
|
if self.phase != PHASE_AUSSCHAFFEN_REPLY or seat_id != self.responder_seat:
|
|
return False
|
|
if accept_hold:
|
|
self.current_stake = self.proposed_stake
|
|
self.phase = PHASE_TRICK_PLAY
|
|
self.turn_seat = self._saved_turn_before_raise
|
|
return True
|
|
|
|
# Gegner geht ("Aufgabe der Hand") -> ausschaffendes Team erhält den bisherigen Einsatz!
|
|
winner_team = 1 - self.team_of(seat_id)
|
|
self._award_hand_to_team(winner_team, self.current_stake)
|
|
return True
|
|
|
|
# --- 3. Stich-Ausspiel ---
|
|
def get_playable_cards(self, seat_id: int) -> list[BavarianCard]:
|
|
if self.phase != PHASE_TRICK_PLAY or seat_id != self.turn_seat:
|
|
return []
|
|
assert self.schlag_rank is not None and self.trump_suit is not None
|
|
return get_legal_cards_for_trick(
|
|
self.seats[seat_id].hand,
|
|
self.current_trick,
|
|
self.trick_index,
|
|
self.ansager_seat,
|
|
self.schlag_rank,
|
|
self.trump_suit,
|
|
)
|
|
|
|
def play_card(self, seat_id: int, card: BavarianCard) -> str:
|
|
"""
|
|
Spielt eine Karte in den aktuellen Stich.
|
|
Rückgabe: 'invalid' | 'played' | 'trick_completed' | 'hand_completed' | 'match_completed'
|
|
"""
|
|
legal = self.get_playable_cards(seat_id)
|
|
if card not in legal:
|
|
return "invalid"
|
|
|
|
self.seats[seat_id].hand.remove(card)
|
|
self.current_trick.append((seat_id, card))
|
|
|
|
if len(self.current_trick) < WT_NUM_PLAYERS:
|
|
self.turn_seat = (self.turn_seat + 1) % WT_NUM_PLAYERS
|
|
return "played"
|
|
|
|
assert self.schlag_rank is not None and self.trump_suit is not None
|
|
win_seat = evaluate_trick_winner(
|
|
self.current_trick, self.schlag_rank, self.trump_suit
|
|
)
|
|
win_team = self.team_of(win_seat)
|
|
self.team_tricks[win_team] += 1
|
|
self.last_completed_trick = list(self.current_trick)
|
|
self.last_trick_winner = win_seat
|
|
self.current_trick = []
|
|
self.trick_index += 1
|
|
|
|
# Wer zuerst 3 Stiche hat, gewinnt die Hand sofort!
|
|
if self.team_tricks[win_team] >= 3:
|
|
self._award_hand_to_team(win_team, self.current_stake)
|
|
return "match_completed" if self.is_match_over else "hand_completed"
|
|
|
|
self.turn_seat = win_seat
|
|
return "trick_completed"
|
|
|
|
def _award_hand_to_team(self, winner_team: int, pts: int) -> None:
|
|
self.hand_winner_team = winner_team
|
|
self.hand_points_awarded = pts
|
|
self.team_points[winner_team] += pts
|
|
self.hands_played += 1
|
|
if self.team_points[winner_team] >= WT_WIN_POINTS:
|
|
self.match_winner_team = winner_team
|
|
self.phase = PHASE_MATCH_DONE
|
|
else:
|
|
self.phase = PHASE_HAND_DONE |