Skip to content

Commit

Permalink
implemented hand and trick play logic, added team class, placeholder …
Browse files Browse the repository at this point in the history
…player methods, rule_util with implemented trick_winner function, todos
  • Loading branch information
boldandbrad committed Jul 2, 2020
1 parent a0e629e commit 9bae74b
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 24 deletions.
3 changes: 3 additions & 0 deletions euchrecli/card_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

class Suit():

def __init__(self, name: str, color: str):
self.name = name
self.color = color
Expand All @@ -11,6 +12,7 @@ def __str__(self) -> str:
return f"{self.name}"

class Face():

def __init__(self, name: str, value: int):
self.name = name
self.value = value
Expand All @@ -22,6 +24,7 @@ def __str__(self) -> str:
return f"{self.name}"

class Card():

def __init__(self, suit: Suit, face: Face):
self.suit = suit
self.face = face
Expand Down
106 changes: 85 additions & 21 deletions euchrecli/game.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from euchrecli.card_util import Card
from euchrecli.card_util import Card, Suit
from euchrecli.deck_util import create_deck, deal_hand
from euchrecli.player_util import Player
from euchrecli.player_util import Team, Player
from euchrecli.rule_util import valid_play, trick_winner, score_hand


def setup():
"""
game setup
"""
players = [Player('Mitch'), Player('Lena'), Player('Bradley'), Player('Morgan')]
# TODO: implement setup from user input

# TODO: implement random team assignment and order
team_1 = Team('Team 1')
team_2 = Team('Team 2')
players = [Player('Mitch', team_1), Player('Lena', team_2), Player('Bradley', team_1), Player('Morgan', team_2)]
players[3].is_dealer = True

game(players)
Expand All @@ -21,42 +27,100 @@ def game(players: [Player]):

hand_number = 1
while(not team_won):
hand(players)
team_called = hand(players)
score_hand(team_called)
team_won = True
hand_number += 1
# TODO: implement dealer rotation


def hand(players: [Player]):
def hand(players: [Player]) -> int:
"""
deal and play a hand
"""
print('play hand')
trump_suit = None
team_called = None

deck = create_deck()
deal_hand(players, deck)
while not trump_suit:
deck = create_deck()
deal_hand(players, deck)

# dealer flips card
face_up_card = deck[0]
deck.pop(0)
for player in players:
print(player.name, player.hand)
print('deck', deck)

trump_suit = None

for player in players:
print(player.hand)
print(deck)
print('face up', face_up_card)
trump_suit, team_called = set_trump_suit(players, deck)

set_trump_suit()
print('trump is', trump_suit)

for _ in range(5):
trick()
played_cards = trick(players, trump_suit)
winner = trick_winner(players, played_cards, trump_suit)
print('trick winner', winner.name)
winner.team.won_trick()
# TODO: adjust play order based on winner of previous trick

return team_called # and potentially number of tricks won for each team?


def set_trump_suit():
def set_trump_suit(players: [Player], deck: [Card]) -> (Suit, int):
"""
set trump suit for the current hand
"""
pass
print('set trump')
# dealer flips card
face_up_card = deck[0]
deck.pop(0)

face_up_suit = face_up_card.suit

print('face up is', face_up_card)

trump_suit = None
team_called = None

def trick():
# pickup round
for player in players:
if not trump_suit:
if player.call_pick_up(face_up_card):
print('pick it up')
# capture trump suit and team that called for it
trump_suit = face_up_card.suit
team_called = player.team

# dealer picks up face up card
replaced_card = players[3].pick_up_card(face_up_card)
deck.append(replaced_card)
else:
print('pass')

# call round
if not trump_suit:
print(f'trump suit cannot be {face_up_suit}')
for player in players:
if not trump_suit:
candidate_suit = player.call_trump_suit(face_up_suit)
print('candidate', candidate_suit)
if candidate_suit != face_up_suit:
trump_suit = candidate_suit
team_called = player.team
else:
print('pass')

return trump_suit, team_called


def trick(players: [Player], trump_suit: Suit) -> [Card]:
print('play trick')
played_cards = []
for player in players:
card_to_play = player.play_card(played_cards, trump_suit)
while not valid_play(card_to_play, played_cards, trump_suit):
card_to_play = player.play_card(played_cards, trump_suit)

played_cards.append(card_to_play)
player.remove_card(card_to_play)

print(played_cards)
return(played_cards)
44 changes: 41 additions & 3 deletions euchrecli/player_util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@

from euchrecli.card_util import Card, Suit

class Team():

def __init__(self, name: str):
self.name = name
self.game_score = 0
self.trick_score = 0

def won_hand(self, points: int):
self.game_score += points

def won_trick(self):
self.trick_score += 1

class Player():

def __init__(self, name: str):
def __init__(self, name: str, team: Team):
self.name = name
self.hand = []
self.team = team
self.is_dealer = False
self.hand = []

def call_pick_up(self, face_up_card: Card) -> bool:
# TODO: implement
return False

def call_trump_suit(self, unsuitable: Suit) -> Suit:
# TODO: implement
return list(filter(lambda card: card.suit != unsuitable , self.hand))[0].suit

def pick_up_card(self, card: Card) -> Card:
# TODO: implement
replaced_card = self.hand[0]
self.hand[0] = card
return replaced_card

def play_card(self, played_cards: [Card], trump_suit: Suit) -> Card:
# TODO: implement
return self.hand[0]

def remove_card(self, card: Card):
# TODO: implement
self.hand.pop(self.hand.index(card))

def __repr__(self) -> str:
return f"Player({self.name}, {self.is_dealer})"
return f"Player({self.name}, {self.team}, {self.is_dealer})"

def __str__(self) -> str:
return f"{self.name}"
Expand Down
27 changes: 27 additions & 0 deletions euchrecli/rule_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

from euchrecli.card_util import Card, Suit
from euchrecli.player_util import Player

def valid_play(card_to_play: Card, played_cards: [Card], trump_suit: Suit) -> bool:
# TODO: implement
return True

def trick_winner(players: [Player], played_cards: [Card], trump_suit: Suit):
"""
determine winner of trick by trump and lead weighted card values
"""
lead_suit = played_cards[0].suit

high_value = 0
winning_player = None
for idx, card in enumerate(played_cards):
weighted_value = card.weighted_value(trump_suit, lead_suit)
if weighted_value > high_value:
high_value = weighted_value
winning_player = players[idx]

return winning_player

def score_hand(team_called: int):
# TODO: implement
pass

0 comments on commit 9bae74b

Please sign in to comment.