Exemplo n.º 1
0
class PlayCards():
    def __init__(self):
        self.pot_size = 0
        self.bet_size = 0
        self.correct = 0
        self.success_ratio = 0

    def setup_deck(self):
        self.dealer_deck = Deck()
        self.cards_dealt = Deck()
        self.dealer_deck.fresh_deck(suits, ranks)
        self.dealer_deck.shuffle()

    def set_pot_size(self):
        self.pot_size = int(input('Please select your starting pot size: '))
        return self.pot_size

    def get_pot_size(self):
        return self.pot_size

    def set_bet_size(self, bet_style, whole):

        if (whole) and self.pot_size == 1:
            return 1

        if bet_style == 'quarter':
            bet_size = self.pot_size / 4
        elif bet_style == 'half':
            bet_size = self.pot_size / 2
        elif bet_style == 'max':
            bet_size = self.pot_size
        else:
            bet_size = input('Please select your bet amount: ')
            if int(float(bet_size)) > self.pot_size:
                print("Bet too large. Setting bet to max.")
                bet_size = self.pot_size

        if whole:
            return int(float(bet_size))
        else:
            return bet_size

    def colour_guess(self, colour_selector, auto_fin):

        colours = self.dealer_deck.count_colours()

        if (auto_fin) and (0 in colours):
            self.bet_style = 'max'

        if colour_selector == 'random':
            return choice(('r', 'b'))
        elif colour_selector == 'smart':
            if colours[0] > colours[1]:
                return 'r'
            elif colours[1] > colours[0]:
                return 'b'
            else:
                return choice(['r', 'b'])
        else:
            colour = input("Please choose a card colour ('r' or 'b'): ")
            if colour not in ('r', 'b'):
                print("Invalid colour selection.")
                self.colour_guess(colour_selector, auto_fin)

    def make_bet(self, colour, bet_size):

        drawn_card = self.dealer_deck.get_top_card()

        if colour == 'r':
            colour_str = 'red'
        else:
            colour_str = 'black'

        print("---Drawing a card---")
        print("You bet {} that the card will be {}".format(
            bet_size, colour_str))
        print("The card is: {}".format(drawn_card))

        if drawn_card.colour == colour:
            self.pot_size += bet_size
            print("Correct! You win {}.".format(bet_size))

        else:
            self.pot_size -= bet_size
            print("Incorrect.. You lose {}.".format(bet_size))

        self.cards_dealt.add_card(self.dealer_deck.remove_card())

    def play(self,
             colour_selector=None,
             bet_style=None,
             auto_fin=False,
             slow=True,
             whole=True):

        self.setup_deck()
        self.bet_style = bet_style
        self.pot_size = self.set_pot_size()

        playing = True

        while playing:
            if len(self.dealer_deck) > 0 and (self.pot_size > 0):

                colour = self.colour_guess(colour_selector, auto_fin)
                bet_size = self.set_bet_size(self.bet_style, whole)

                self.make_bet(colour, bet_size)

            else:
                playing = False
                break

            ##TODO display stats ##
            print("Your current pot size is: {}.".format(self.pot_size))
            self.dealer_deck.show_colours()

            if slow:
                q = input("----------------------------------------------")
            else:
                print("----------------------------------------------")

        print("You finished with a pot size of: {}".format(self.pot_size))
Exemplo n.º 2
0
while num_players < 2:
	num_players = int(input())
	if num_players == -1:
		quit()
	if num_players < 2:
		print("You need at least 2 players to play s******d! Try again...")
        
for i in range(num_players):
    print("Enter Name for Player",i+1,":")
    name = input()
    players.append(ShitheadPlayer(name))
		
num_decks = 1+num_players//3 

playing_deck = Deck(num_decks)
playing_deck.shuffle()

discard_pile = []

# Deal Cards

for place in ['fd','fu','hand']:
    for i in range(3):
        for j in range(num_players):
            players[j].add_card(playing_deck.pop(),place)
 
# Game 
player_ind = 0
direction = 1

print("Press Enter to start game...")
Exemplo n.º 3
0
class MissStudHand:
    def __init__(self, WinningHands= '',NumberHoleCards=2, NumberCommunityCards=3):
        self.Deck = Deck()
        self.Deck.shuffle()
        self.HoleCards = [self.Deck.draw(), self.Deck.draw()]
        self.CommunityCards = []
        self.NumberHoleCards = NumberHoleCards
        self.NumberCommunityCards = NumberCommunityCards
        self.NumberCards = NumberHoleCards + NumberCommunityCards

    def Cards(self):
        return itertools.chain(self.HoleCards, self.CommunityCards)

    def CountsByRank(self):
        CountsDict = {}
        for card in self.Cards():
            if card.rank in CountsDict:
                CountsDict[card.rank] += 1
            else:
                CountsDict[card.rank] = 1        
#    CountsDict[card.rank] += 1 if card.rank in CountsDict else CountsDict[card.rank] = 1
        return CountsDict        

	
    def __repr__(self):
        return '\n'.join(str(card) for card in self.Cards())


    def MaxCardValue(self):
        return max([c.rank for c in self.Cards()])

    def MinCardValue(self):
        return min([c.aceLow() for c in self.Cards()])
        
    def MaxCardDistance(self):
        return self.MaxCardValue() - self.MinCardValue()

    def MaxCard(self):
        return max([card for card in self.Cards()])

    def MinCard(self):
        if self.MaxCard().rank == 14:
            return self.MaxCard()
        else:
            return min([card for card in self.Cards()])

    def SuitedCards(self, suit):
        return sum([1 for card in self.Cards() if card.suit == suit])

    def NumberVisibleCards(self):
        return len(self.HoleCards) + len(self.CommunityCards)

    def NumberUnseenCards(self):
        return self.NumberCards - self.NumberVisibleCards()

    def IsPair(self):
        return sum([1 for n in self.CountsByRank().values()  if n >=2]) >= 1

    def IsTwoPair(self):
        return sum([1 for n in self.CountsByRank().values() if n >=2]) >= 2

    def IsThreeOfaKind(self):
        return sum([1 for n in self.CountsByRank().values() if n >=3]) >= 1

    def IsFourOfaKind(self):
        return sum([1 for n in self.CountsByRank().values() if n >=4]) >= 1

    def IsFullHouse(self):
        Pair =  sum([1 for n in self.CountsByRank().values()  if n ==2]) == 1
        Trips = sum([1 for n in self.CountsByRank().values()  if n ==3]) == 1
        return Pair and Trips

    def CountMatches(self, count):
        d = self.CountsByRank()
        return sum([1 for v in d.values() if v == count])
 
    def IsFlush(self):
        suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
        return self.NumberVisibleCards()  == max([self.SuitedCards(s) for s in suits])

    def IsStraight(self):
        if self.MaxCard().rank == 14:
            return self.IsStraight_AceLow()
        else:
            return self.IsStraight_AceHigh()

    def IsStraight_AceLow(self):
        l = [c.rank for c in self.Cards()]
	l = [1 if rank == 14 else rank for rank in l]
        l.sort()
        IsStraight = True
        for i in range(len(l)-1):
            if l[i]+1 != l[i+1]:
                IsStraight = False
        return IsStraight or self.IsStraight_AceHigh()

    def IsStraight_AceHigh(self):
        l = [c.rank for c in self.Cards()]
        l.sort()
        IsStraight = True
        for i in range(len(l)-1):
            if l[i]+1 != l[i+1]:
                IsStraight = False
        return IsStraight

    def IsPossible_Pair(self):
        IsEnoughCardsLeft = self.NumberUnseenCards() >= 1
        return self.IsPair() or IsEnoughCardsLeft

    def IsPossible_TwoPair(self):
        IsEnoughCardsLeft = True if self.IsPair() and self.NumberUnseenCards() >= 1 else self.NumberUnseenCards() >= 2      
        return self.IsTwoPair() or IsEnoughCardsLeft

    def IsPossible_Trips(self):
        IsEnoughCardsLeft = True if self.IsPair() and self.NumberUnseenCards() >= 1 else self.NumberUnseenCards() >= 2
        return self.IsThreeOfaKind() or IsEnoughCardsLeft

    def IsPossible_FullHouse(self):
        if self.NumberUnseenCards() >= 3:
            return True
        elif self.NumberUnseenCards() == 2 and self.IsPair():
            return True
        elif self.NumberUnseenCards() == 1 and self.IsTwoPair() or self.IsThreeOfaKind():
            return True
        else:
            return False
     

    def IsPossible_FourOfaKind(self):
        if self.NumberUnseenCards() >= 3:
            return True
        elif self.NumberUnseenCards() == 2 and self.IsPair():
            return True
        elif self.NumberUnseenCards() == 1 and self.IsThreeOfaKind():
            return True
        elif self.IsFourOfaKind():
            return True
        else:
            False


    def IsPossible_Straight(self):
        if self.IsPair() and self.NumberCards == 5:
            return False
        else:
            return True

    def IsPossible_Flush(self):
        return self.IsFlush()

    def IsPossible_StraightFlush(self):
        return True

    def getHandRank(self):
        if self.IsFlush():
             return 'Flush'
        elif self.IsStraight():
             return 'Straight'
        elif self.IsFullHouse():
            return 'Full House'
        elif self.IsFourOfaKind():
            return 'Four Of A Kind'
        elif self.IsThreeOfaKind():
            return 'Three Of A Kind'
        elif self.IsTwoPair():
            return 'Two Pair'
        elif self.IsPair():
            return 'Pair'
        else:
            return 'You Suck'
Exemplo n.º 4
0
class Manager:
    def __init__(self, noplayers, initial_stake):
        self.players = []
        self.dealer = Player("Dealer", 10000)
        self.deck = Deck()
        self.deck.shuffle()
        cards = self.deck.deal(2)
        dealerHand = Hand()
        dealerHand.add_cards(cards)
        self.dealer.initialHand(dealerHand)

        for i in range(0, noplayers):
            player = Player("Player" + str(i + 1), initial_stake)
            cards = self.deck.deal(2)
            playerHand = Hand()
            playerHand.add_cards(cards)
            player.initialHand(playerHand)
            self.players.append(player)
        print_all_hands(self.dealer, self.players)
        self.play_rounds()

    def hit_or_hold(self):
        while determine_if_anyone_can_play(self.players):
            for player in self.players:
                if player.status == "play":
                    print("==============================================")
                    print(player.identifier)
                    inputString = input('Do you want to hit or hold?')
                    print("==============================================")
                    if inputString == "hold":
                        player.status = "hold"
                    elif inputString == "hit":
                        card = self.deck.deal(1)[0]
                        player.hand.add_card(card)
                        handvalue = player.hand.adjust_for_ace()
                        print("The value of the current hand is " +
                              str(handvalue))
                        print("==============================================")
                        if handvalue > 21:
                            player.status = 'bust'

    def finish_dealer_hand(self):
        print("Here are the dealer's cards")
        for card in self.dealer.hand.cards:
            print(card)
        print("+==================================================")
        dealervalue = self.dealer.hand.adjust_for_ace()
        print("Initial hand value for dealer: " + str(dealervalue))
        while dealervalue < 17:
            card = self.deck.deal(1)[0]
            print(card)
            self.dealer.hand.add_card(card)
            dealervalue = self.dealer.hand.adjust_for_ace()
            print("The value of dealer hand with card added is: " +
                  str(dealervalue))
            if dealervalue > 21:
                self.dealer.status = 'bust'

    def pay_bets(self):
        for player in self.players:
            if player.status == 'bust':
                player.lose_bet()
            elif self.dealer.status == 'bust':
                player.win_bet()
            elif player.hand.adjust_for_ace(
            ) > self.dealer.hand.adjust_for_ace():
                player.win_bet()
            else:
                player.lose_bet()
            print(
                '=================================================================='
            )
            print(player.identifier + " now has this total : " +
                  str(player.total))

    def play_rounds(self):
        want_to_play_game = True
        while want_to_play_game:
            place_bets(self.players)
            self.hit_or_hold()
            self.finish_dealer_hand()
            print_player_statuses(self.dealer, self.players)
            self.pay_bets()
            print(
                '=================================================================='
            )
            inputString = input('Do you want to play again: yes/no?')
            if inputString == 'no':
                want_to_play_game = False
            else:
                cards = self.deck.deal(2)
                dealerHand = Hand()
                dealerHand.add_cards(cards)
                self.dealer.initialHand(dealerHand)
                for player in self.players:
                    cards = self.deck.deal(2)
                    playerHand = Hand()
                    playerHand.add_cards(cards)
                    player.initialHand(playerHand)
                    player.status = 'play'
            print(
                '=================================================================='
            )