def test_blackjack_player_wins(self): player = Player('cory','password') game = Game('Blackjack') hand = Hand() player.bank = 100 player.hands.append(hand) game.hands.append(hand) game.players.append(player) bank_before_bet = player.bank # cards_app.session.commit() cards_app.session.flush() bet(hand, 50) cards = [Card(sequence=1), Card(sequence=10)] hand.cards.extend(cards) count_blackjack(hand) evaluate_hit(hand) # player wins with nautral evaluate_hit assert player.bank == bank_before_bet - 50 # player stands on 18, dealer stands on 17 hand.cards = [Card(sequence=10), Card(sequence=8)] bet(hand, 50) game.dealer = Player('cory','password') dealer = game.dealer dealer.cards = [Card(sequence=10), Card(sequence=17)] dealer_hand = Hand() dealer_hand.cards = dealer.cards
def split(hand): player = hand.player game = hand.game new_hand = Hand() new_hand.cards = [hand.cards.pop()] game.hands.append(new_hand) player.hands.append(new_hand) new_hand.cards.append(game.deck.cards.pop()) hand.cards.append(game.deck.cards.pop())
def setup_dealer_blackjack(self): player = Player() player.bet = Decimal(1.00) player.money = Decimal(1.00) card1 = Card('Hearts', 'Jack') card2 = Card('Hearts', 'Ace') dealer_hand = Hand() player_hand = Hand() dealer_hand.add_card(card1) dealer_hand.add_card(card2) return player, player_hand, dealer_hand
def setup_lose_scenario(self): player = Player() player.bet = Decimal(1.00) player.money = Decimal(1.00) card1 = Card('Hearts', 'Jack') card2 = Card('Hearts', 'King') dealer_hand = Hand() player_hand = Hand() dealer_hand.add_card(card1) dealer_hand.add_card(card2) return player, player_hand, dealer_hand
def split(hand_id): hand = session.query(Hand).filter(Hand.id == hand_id).all()[0] player = hand.player game = hand.game new_hand = Hand() count_blackjack(hand) session.add(new_hand) new_hand.cards = [hand.cards.pop()] game.hands.append(new_hand) player.hands.append(new_hand) new_hand.cards.append(game.deck.cards.pop()) hand.cards.append(game.deck.cards.pop()) session.commit() return jsonify({'status': 'success'})
def find_best_hand(cardlist): combos = itertools.combinations(cardlist, 5) best_hand = None for c in combos: if best_hand is None: best_hand = Hand(c) else: compare = best_hand.compare_to_hand(Hand(c)) if compare == 1: continue elif compare == -1: best_hand = Hand(c) elif compare == 0: continue return best_hand
def find_best_plo_hand(player_cards, shared_cards): combos = select_combinations([ (player_cards, 2), (shared_cards, 3), ]) best_hand = None for c in combos: if best_hand is None: best_hand = Hand(list(c)) else: compare = best_hand.compare_to_hand(Hand(list(c))) if compare == 1: continue elif compare == -1: best_hand = Hand(list(c)) elif compare == 0: continue return best_hand
def start_game(request): context = {} if request.POST: return HttpResponseBadRequest("Please do not post") else: context = get_dealer(request, context) hand = Hand() hand.add_card(C.generate_card()) hand.add_card(C.generate_card()) hand.save() context['player'].hand = hand context['player'].save() dealhand = Hand() dealhand.add_card(C.generate_card()) dealhand.save() context['dealer'].hand = dealhand context['dealer'].save() print context['dealer'].hand return HttpResponseRedirect('/game/playing')
def test_blackjack_player_loses(self): game = Game('Blackjack') cards = [Card(sequence=10), Card(sequence=10), Card(sequence=10)] player = Player('cory','password') cards_app.session.add(player) hand = Hand() player.hands.append(hand) game.players.append(player) game.hands.append(hand) cards_app.session.flush() hand.cards.extend(cards) bank_after_bet = player.bank count_blackjack(hand) evaluate_hit(hand) # player loses if he breaks assert player.bank == bank_after_bet # start of next loss, player stands on 15, dealer gets high hand hand.cards = [Card(sequence=10), Card(sequence=5)] bet(hand, 50) bank_after_bet = player.bank count_blackjack(hand) evaluate_hit(hand) assert player.bank == bank_after_bet
# docstrings for more information. import random import time from functions import shuffle_deck, get_rank, war, win, finish, crd_id from models import Player, Hand, deck while True: # Instantiating Player class shuffle_deck(deck) print('--War card game--\n') name1 = input('Player 1 name: ') player1 = Player(name1) player1.hand = Hand(deck[:26]) computer = Player('Computer') computer.hand = Hand(deck[26:]) game_won = False while not game_won: action = input(f'\n{player1.name}, type anything to play a card: ') if action: # Playing and comparing cards by rank card1 = player1.hand.play() rank1 = get_rank(card1) print(f'\n{player1.name} played [{card1}] rank: {rank1}')
def calculate_best_hand(cards): # takes a list of cards and returns a Hand Object that represents the best hand # maybe optimize? #calc if flush suit_counts = [ len(filter(lambda card: card.suit == s, cards)) for s in range(4) ] is_flush = max(suit_counts) >= 5 suit = CardSuit(suit_counts.index(max(suit_counts))) flush_val = max([ lambda card: card.value for suited_card in filter( lambda card: card.suit == suit, cards) ]) #calc if straight sorted_cards = sorted(cards) stack = [] is_straight = False for c in sorted_cards: if stack == [] or c.value - 1 == stack[-1]: stack.append(c.value) else: stack = [] if (len(stack >= 5)): is_straight = True straight_val = stack[-1] #calc groupings of cards groups = collections.Counter(arr) ones = [ card_value for card_value in groups if groups[card_value] == 1 ] twos = [ card_value for card_value in groups if groups[card_value] == 2 ] threes = [ card_value for card_value in groups if groups[card_value] == 3 ] fours = [ card_value for card_value in groups if groups[card_value] == 4 ] if (is_straight and is_flush): return Hand(HandType.STRAIGHTFLUSH, flush_val) elif (len(fours) != 0): return Hand(HandType.QUADS, max(fours)) elif (len(threes) > 0 and len(twos) > 1): return Hand( HandType.FULLHOUSE, max(threes) * 13 + max(filter(twos, lambda x: x != max(threes)))) elif (is_flush): return Hand(HandType.FLUSH, flush_val) elif (is_straight): return Hand(HandType.STRAIGHT, straight_val) elif (len(threes) > 0): return Hand(HandType.TRIPS, max(threes)) elif (len(twos) > 0): return Hand(HandType.PAIR, max(twos)) else: return Hand(HandType.HIGHCARD, max(ones))
def deal_hands(self): for player in self.player_names: self.scores[player] = 0 dealt_hand = self.red_deck.deal_hand() new_hand = Hand(player.lower(), dealt_hand) self.hands[player] = new_hand
def test_blackjack_payout(self): player = Player('cory','password') game = Game('Blackjack') hand = Hand() game.players = [] player.bank = 100 player.hands = [hand] hand.bet = 0 game.deck = Hand() game.dealer = Player('cory','password') game.dealer.hands.append(Hand()) game.hands.append(game.dealer.hands[0]) player.hands.append(hand) game.hands.append(hand) game.deck.cards = piece_maker(suits, card_values, 1) game.players.append(player) cards_app.session.flush() # testing player AND dealer get blackjack hand.cards = [Card(sequence=1), Card(sequence=10)] game.dealer.hands[0].cards = [Card(sequence=1), Card(sequence=10)] bet(hand, 50) count_blackjack(hand) blackjack_dealer(game) assert player.bank == 100 # Player gets 15 and dealer gets 15, dealer hits and breaks because deck is unshuffled and king on top player.bank = 100 hand.cards = [Card(sequence=10), Card(sequence=5)] game.dealer.hands[0].cards = [Card(sequence=10), Card(sequence=5)] bet(hand, 50) hand.is_expired = False count_blackjack(hand) blackjack_dealer(game) assert player.bank == 150 # player gets blackjack, dealer doesn't hand.is_expired = False hand.bet = 0 player.bank = 100 hand.cards = [Card(sequence=10), Card(sequence=1)] game.dealer.hands[0].cards = [Card(sequence=10), Card(sequence=10)] bet(hand, 50) count_blackjack(hand) blackjack_dealer(game) assert player.bank == 175 # player broke, loses bet hand.is_expired = False hand.bet = 0 player.bank = 100 hand.cards = [Card(sequence=10), Card(sequence=10), Card(sequence=10)] game.dealer.hands[0].cards = [Card(sequence=10), Card(sequence=10)] bet(hand, 50) count_blackjack(hand) blackjack_dealer(game) blackjack_payout(game) assert player.bank == 50
deck = Deck(packs) deck.shuffle() # Setting the play variable and the game counter play_another = 'y' while play_another != 'n': # clear screen between rounds clearscreen() if (deck.cards_left() < 10): print( "There were only {} cards left in the deck, so the dealer reshuffled.".format( deck.cards_left())) deck = Deck(packs) deck.shuffle() player_hand = Hand() dealer_hand = Hand() # listing the players's stats print("{}'s current chipcount: ${} - {}'s starting amount: ${}.".format(player.name, round(player.money, 2), player.name, round(player.start_money, 2))) print("{} Hands played. {} Hands won. Win percentage: {}%".format( player.games_played, player.wins, player.win_percentage)) if player.money < 1: break else: # player.bet() = int(input("{}, make your wager: # $".format(player.name))) bet = get_bet(player) if player.bet > player.money: print("You don't have that much, so you promise your partner a payment.") print("Luckily (maybe) for you, the dealer accepts.")
def test_show_cards_returns_string(self): hand = Hand() hand.add_card(Card('Heart','4')) hand.add_card(Card('Diamond','Ace')) self.assertTrue(hand.show_cards, 'a string, any string')
def test_hand_scoring_with_multiple_aces(self): hand = Hand() hand.add_card(Card('Heart','Ace')) hand.add_card(Card('Diamond','Ace')) self.assertTrue(hand.score, '12')
def test_hand_scoring(self): hand = Hand() hand.add_card(Card('Heart','4')) hand.add_card(Card('Diamond','7')) self.assertTrue(hand.score, '11')