Skip to content

Commit

Permalink
Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
InCogNiTo124 committed Apr 25, 2021
1 parent 22e7e89 commit 1b979cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
34 changes: 24 additions & 10 deletions belabot/engine/belot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
from functools import reduce
from typing import List, Tuple, Sequence, Dict
import more_itertools as mit

random_gen = random.SystemRandom()

Expand All @@ -19,11 +20,15 @@

class Belot:
def __init__(self, players: Sequence[Player]):
assert len(set(players)) == len(
players
), "The players must all have different names"
self.players = players
self.deck = range(32)
self.cards_played: List[Card] = []
self.mi = slice(0, None, 2)
self.vi = slice(1, None, 2)

for player, right, teammate, left in mit.circular_shifts(self.players):
player.team_setup(teammate, left, right)
return

def play(self) -> None:
Expand All @@ -43,8 +48,11 @@ def round(self, dealer_index: int) -> Tuple[int, int]:
### BIDDING PHASE
first_6, talons = self.shuffle()
self.deal_cards(first_6)
adut, mi_bid = self.get_adut(dealer_index)
log.debug(f'{"MI" if mi_bid else "VI"} have bid {repr(adut)} for adut')
adut, adut_caller_index = self.get_adut(dealer_index)
mi_bid = (adut_caller_index % 2) == 0
log.info(
f'{self.players[adut_caller_index].name} ({"MI" if mi_bid else "VI"}) have bid {repr(adut)} for adut'
)
self.deal_cards(talons)
all_declarations = self.compute_declarations(
[player.cards for player in self.players], dealer_index
Expand All @@ -56,6 +64,8 @@ def round(self, dealer_index: int) -> Tuple[int, int]:
total_points = 162 + sum(t.value() for t in mi_declarations + vi_declarations)
log.debug("Total points: {}".format(total_points))

self.notify_pregame(all_declarations, adut, adut_caller_index)

### MAIN PHASE
# mi_points = random_gen.randint(0, 162)
mi_points, vi_points = 0, 0
Expand All @@ -71,9 +81,6 @@ def round(self, dealer_index: int) -> Tuple[int, int]:
log.debug(f"\t{player.name} plays {repr(card)}")
turn_cards.append(card)
self.notify_played(player, card)
# assert len(turn_cards[self.mi]) == 2
# assert len(turn_cards[self.vi]) == 2
assert turn_cards[self.mi] != turn_cards[self.vi]
turn_winner = get_winner(turn_cards, adut)
log.debug(f"{turn_winner} wins the turn.")
if (start_player_index + turn_winner) % 2 == 0:
Expand Down Expand Up @@ -123,6 +130,13 @@ def notify_turn_points(self, mi_points: int, vi_points: int) -> None:
player.notify_turn_points(mi_points if i % 2 == 0 else vi_points)
return

def notify_pregame(
self, declarations: Dict[int, List[Declaration]], adut: Suit, adut_caller: int
) -> None:
for player in self.players:
player.notify_pregame(declarations, adut, self.players[adut_caller])
return

def shuffle(self) -> Tuple[List[List[int]], List[List[int]]]:
# Usually, cards in Bela are dealed in a particular order
# this kinda makes sense in a real world where not all
Expand All @@ -143,14 +157,14 @@ def shuffle(self) -> Tuple[List[List[int]], List[List[int]]]:
talons.append(deck[talon_start:player_cards_end])
return adut_cards, talons

def get_adut(self, dealer_index: int) -> Tuple[Suit, bool]:
def get_adut(self, dealer_index: int) -> Tuple[Suit, int]:
for i in range(1, 1 + len(self.players)): # the dealer calls last
player_index = (dealer_index + i) % len(self.players)
log.debug(f"Player {player_index} bids...")
player = self.players[player_index]
adut = player.get_adut(is_muss=(i == 4))
if adut != Adut.NEXT:
to_return = (Suit(adut.value), player_index % 2 == 0)
to_return = (Suit(adut.value), player_index)
break
return to_return

Expand All @@ -163,7 +177,7 @@ def compute_declarations(
2: [],
3: [],
} # absolute indices
for i in range(1, 1+len(self.players)):
for i in range(1, 1 + len(self.players)):
player_index = (dealer_index + i) % len(self.players)
player = self.players[player_index]
cards = player_cards[player_index]
Expand Down
24 changes: 23 additions & 1 deletion belabot/engine/player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .card import Card, Adut, Suit
from .declarations import Declaration
from .util import get_valid_moves
from typing import List, Optional
from typing import List, Optional, Dict
import abc
import random
import sys
Expand All @@ -19,8 +20,12 @@ def __init__(self, name: Optional[str]) -> None:
self.cards: List[Card] = []
self.played: List[Card] = []
self.points: List[int] = []
self.turn_declarations: Dict[int, List[Declaration]] = dict()
return

def __hash__(self) -> int:
return hash(self.name)

def add_cards(self, cards: List[int]) -> None:
self.cards.extend([Card.from_int(t) for t in cards])
return
Expand All @@ -34,6 +39,17 @@ def notify_played(self, card: Card) -> None:
self.card_played(card)
return

def notify_pregame(
self,
declarations: Dict[int, List[Declaration]],
adut: Suit,
adut_caller: "Player",
) -> None:
self.turn_declarations = declarations
self.turn_adut = adut
self.turn_adut_called = adut_caller
return

def card_accepted(self, card: Card) -> None:
self.cards.remove(card)
return
Expand All @@ -45,6 +61,12 @@ def notify_turn_points(self, points: int) -> None:
self.points.append(points)
return

def team_setup(self, teammate: "Player", left: "Player", right: "Player") -> None:
self.teammte = teammate
self.left = left
self.rigth = right
return

@abc.abstractmethod
def get_adut(self, is_muss: bool) -> Adut:
pass
Expand Down

0 comments on commit 1b979cf

Please sign in to comment.