def ready_for_next_game(self): self.hand = Hand() self.cards_won = [] self.game_points = 0 self.opponent_hand = [] self.opponent_cards_won = [] self.opponent_game_points = 0 self._trump = None
def test_cards_of_same_suit_all_same_suit(self): from Game.Hand import Hand from Game.Card import Card hand = Hand() hand.append(Card(Card.Diamonds, Card.Jack)) hand.append(Card(Card.Diamonds, Card.Queen)) hand.append(Card(Card.Diamonds, Card.King)) self.assertEqual(len(hand.cards_of_same_suit(Card.Diamonds)), 3)
def __init__(self, name, automated=True, requires_model_load=False): self.name = name self.automated = automated self.requires_model_load = requires_model_load self.hand = Hand() self.cards_won = [] self.match_points = 0 self.game_points = 0 self.opponent_hand = [] self.opponent_cards_won = [] self.opponent_match_points = 0 self.opponent_game_points = 0 self.game = None # Assigned by game controller self._trump = None self.legal_actions = []
def test_cards_of_same_suit_1_greater_than(self): from Game.Hand import Hand from Game.Card import Card hand = Hand() hand.append(Card(Card.Diamonds, Card.Jack)) hand.append(Card(Card.Diamonds, Card.Queen)) card = Card(Card.Diamonds, Card.Ace) hand.append(card) self.assertEqual( hand.cards_of_same_suit(Card.Diamonds, Card.King)[0], card)
def test_available_marriages_out_of_order(self): from Game.Deck import Card from Game.Hand import Hand hand = Hand() hand.append(Card(Card.Diamonds, Card.Jack)) hand.append(Card(Card.Diamonds, Card.King)) hand.append(Card(Card.Diamonds, Card.Ace)) hand.append(Card(Card.Diamonds, Card.Ten)) hand.append(Card(Card.Diamonds, Card.Queen)) expected_marriage = [Card.Diamonds] result, marriage = hand.available_marriages() self.assertEqual(result, True) self.assertEqual(expected_marriage, marriage)
class Player: "the baseClass player" def __init__(self, name, hand, foot): self.name = name self.hand = Hand(hand); self.foot = Hand(foot); self.inFoot = False def draw(self, draw): for d in draw: if self.inFoot: self.foot.addCard(d) else: self.hand.addCard(d) def laydown(self, piles, safe, discard): return []; def discard(self): if self.inFoot: return self.foot.pop() else: return self.hand.pop() def isItSafe(self): return true
def test_cards_of_same_suit_no_mathces(self): from Game.Hand import Hand from Game.Card import Card hand = Hand() hand.append(Card(Card.Diamonds, Card.Jack)) hand.append(Card(Card.Diamonds, Card.Queen)) hand.append(Card(Card.Diamonds, Card.Queen)) self.assertEqual( hand.cards_of_same_suit(Card.Clubs, Card.Ace), [])
def first_deal(self, card): """ Called when initially dealing to a player. Places all cards in a single (first hand) Args: :Card card: Card object added to the player's hand. """ # This is the first card delt to the player if len(self.hands) == 0: hand = Hand(self.initial_bet) self.hands.append(hand) else: hand = self.hands[0] self.deal_card(card, hand) self.print_hands()
def __init__(self): self._hands = [] for i in range(5): self._hands.append(Hand()) self._available_hands = [1, 2, 3, 4, 5]
player1 = Player(name='John', health=30, energy_pool=5) player1.addDeckToPlayer(deck1) player1.selectDeck(deck1) player2 = Player(name='George', health=30, energy_pool=5) player2.addDeckToPlayer(deck2) player2.selectDeck(deck2) # print('Player 1 Deck') print('Player 1 Deck Card Count:', len(player1.deck.deck)) # player1.deck.printDeck() # print('Player 2 Deck') print('Player 2 Deck Card Count:', len(player2.deck.deck)) # player2.deck.printDeck() allHands = Hand(5, 5) player1Hand = Hand(5, 5) player2Hand = Hand(5, 5) for x in range(0, allHands.hand_size): player1Hand.addCardToHand( player1.deck.drawCardFromDeck(DrawCard.DrawFromTopOfDeck)) player2Hand.addCardToHand( player2.deck.drawCardFromDeck(DrawCard.DrawFromTopOfDeck)) player1.hand = player1Hand player2.hand = player2Hand # print('Player 1 Hand') # player1.hand.printHand() # print('Player 2 Hand') # player2.hand.printHand()
def manage_split(self, game_deck, h_idx): """ Manage splitting a hand at the start of the hand's plathrough. Args: :Deck game_deck: Deck used in the blackjack game used to draw additional cards. :Int h_idx: Index of the hand to play through. Return: Bool - True if hand was split """ hand = self.hands[h_idx] # Each hand starts with two cards. If they are equal, the player can split them and double their bet. if hand.get_bet() <= self.money: if hand.get_first().int_value() == hand.get_second().int_value(): print( 'Both cards in this hand have equal value, would you like to split?' ) print( 'You must place your original bet amount on the second hand.' ) # Prompt to split their hand. split_str = '' while split_str.lower() != 'y' and split_str.lower() != 'n': split_str = input('Press Y to split and N to continue: ') # Confirm the split. if split_str.lower() == 'y': confirm_str = '' while confirm_str.lower() != 'y' and confirm_str.lower( ) != 'n': confirm_str = input( 'Are you sure you want to split (Y to split, N to cancel): ' ) # Split the hand if confirm_str.lower() == 'y': self.money -= hand.get_bet() print(self.name, 'bet another $', hand.get_bet(), 'and split their hand!\n') # Create a new hand from the first card first_card = hand.get_first() first_new = Hand(hand.get_bet()) first_new.add_card(first_card) first_new.add_card(game_deck.draw()) self.hands[h_idx] = first_new # Create a second hand from the second card second_card = hand.get_second() second_new = Hand(hand.get_bet()) second_new.add_card(second_card) second_new.add_card(game_deck.draw()) self.hands.insert(h_idx + 1, second_new) # Print the new hands self.print_hands() return True return False
def test_available_marriages_no_marriages(self): from Game.Deck import Card from Game.Hand import Hand hand = Hand() hand.append(Card(Card.Diamonds, Card.Ten)) hand.append(Card(Card.Diamonds, Card.King)) hand.append(Card(Card.Clubs, Card.Ace)) hand.append(Card(Card.Hearts, Card.King)) hand.append(Card(Card.Clubs, Card.King)) expected_marriage = [] result, marriage = hand.available_marriages() self.assertEqual(result, False) self.assertEqual(expected_marriage, marriage)
class SimplePlayer: def __init__(self, name, automated=True, requires_model_load=False): self.name = name self.automated = automated self.requires_model_load = requires_model_load self.hand = Hand() self.cards_won = [] self.match_points = 0 self.game_points = 0 self.opponent_hand = [] self.opponent_cards_won = [] self.opponent_match_points = 0 self.opponent_game_points = 0 self.game = None # Assigned by game controller self._trump = None self.legal_actions = [] def ready_for_next_match(self): self.match_points = 0 self.opponent_match_points = 0 self.ready_for_next_game() def ready_for_next_game(self): self.hand = Hand() self.cards_won = [] self.game_points = 0 self.opponent_hand = [] self.opponent_cards_won = [] self.opponent_game_points = 0 self._trump = None def receive_card(self, card): self.hand.append(card) def notify_game_points_won(self, player, points, cards=None): if player is self: self.game_points += points self._check_and_declare_win() if cards is not None: for card in cards: self.cards_won.append(card) else: self.opponent_game_points += points if cards is not None: for card in cards: self.opponent_cards_won.append(card) def notify_match_points_won(self, player, points): if player is self: self.match_points += points else: self.opponent_match_points += points def _check_and_declare_win(self): if self._enough_points(): self.game.declare_game_win(self) def _enough_points(self): return self.game_points >= self.game.game_point_limit def has_cards_left(self): return len(self.hand) != 0 # All cards are valid def evaluate_legal_actions(self, am_leader, opponents_card): legal_actions = [] for card in self.hand: legal_actions.append(Action(card=card)) self.legal_actions = legal_actions def _print_str_name(self): return self.name def __repr__(self): return self._print_str_name() def __str__(self): return self._print_str_name() def select_action(self): return random.choice(self.legal_actions) def select_action_better(self): selected_action = None if self.game.leading_player is self: # Play highest card if leader highest_card_value = 0 if selected_action is None: for action in self.legal_actions: if action.card is not None: if action.card.value > highest_card_value: highest_card_value = action.card.value selected_action = action else: # Can I win hand? Why wouldn't I want to? # Play lowest card if follower lowest_card_value = 100 if selected_action is None: for action in self.legal_actions: if action.card is not None: if action.card.value < lowest_card_value: lowest_card_value = action.card.value selected_action = action if selected_action is None: raise Exception('Pick an action f**k-wit') return selected_action
class Player: def __init__(self, name, automated=True, requires_model_load=False): self.name = name self.automated = automated self.requires_model_load = requires_model_load self.hand = Hand() self.cards_won = [] self.match_points = 0 self.game_points = 0 self.opponent_hand = [ ] # This is for keeping track of cards we know the opponent definitely has self.opponent_cards_won = [] self.opponent_match_points = 0 self.opponent_game_points = 0 self.game = None # Assigned by game controller self._trump = None self.legal_actions = [] # Child player class must implement this method and return an action # The player or some other controller should probably first ask for the # legal actions to evaluated (but an illegal action is handled by the game controller) def select_action(self): raise Exception('Must be implemented by child') def ready_for_next_match(self): self.match_points = 0 self.opponent_match_points = 0 self.ready_for_next_game() def ready_for_next_game(self): self.hand = Hand() self.cards_won = [] self.game_points = 0 self.opponent_hand = [] self.opponent_cards_won = [] self.opponent_game_points = 0 self._trump = None def receive_card(self, card): self.hand.append(card) def notify_trump(self, card): self._trump = card def notify_game_points_won(self, player, points, cards=None): if player is self: self.game_points += points self._check_and_declare_win() if cards is not None: for card in cards: self.cards_won.append(card) else: self.opponent_game_points += points if cards is not None: for card in cards: self.opponent_cards_won.append(card) def notify_match_points_won(self, player, points): if player is self: self.match_points += points else: self.opponent_match_points += points def _check_and_declare_win(self): if self._enough_points(): self.game.declare_game_win(self) def _enough_points(self): return self.game_points >= self.game.game_point_limit def has_cards_left(self): return len(self.hand) != 0 # This ugly AF... But it works def evaluate_legal_actions(self, opponents_card): legal_actions = [] if opponents_card is None: if self.hand.has_card(self._trump.suit, 2) and not self.game.deck_closed: legal_actions.append(Action(swap_trump=True)) if not self.game.deck_closed: legal_actions.append(Action(close_deck=True)) have_marriages, marriages = self.hand.available_marriages(self) for marriage in marriages: legal_actions.append( Action(card=marriage.queen, marriage=marriage)) legal_actions.append( Action(card=marriage.king, marriage=marriage)) for card in self.hand: legal_actions.append(Action(card=card)) else: if not self.game.deck_closed: for card in self.hand: legal_actions.append(Action(card=card)) else: more_legal_actions = True for card in self.hand.cards_of_same_suit( opponents_card.suit, opponents_card.value): legal_actions.append(Action(card=card)) more_legal_actions = False if more_legal_actions: for card in self.hand.cards_of_same_suit( opponents_card.suit): legal_actions.append(Action(card=card)) more_legal_actions = False if more_legal_actions: for card in self.hand.cards_of_same_suit(self._trump.suit): legal_actions.append(Action(card=card)) more_legal_actions = False if more_legal_actions: for card in self.hand: legal_actions.append(Action(card=card)) self.legal_actions = legal_actions def _print_str_name(self): return self.name def __repr__(self): return self._print_str_name() def __str__(self): return self._print_str_name()
def __init__(self, player_number): self.player_number = player_number self.player_score = 0 self.hand = Hand()
def __init__(self, name, hand, foot): self.name = name self.hand = Hand(hand); self.foot = Hand(foot); self.inFoot = False