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')
# 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