class Player(object): def __init__(self, name): self.name = name self.hand = Hand([]) def add_card(self, card): self.hand.add(card) def print_hand(self): cards = self.hand.cards for card in cards: print " %s" % card value = self.hand.eval_hand() if len(value) == 1: print "Value of hand: %s" % value[0] else: print "Value of hand: %s or %s" % (value[0], value[1]) def has_blackjack(self): aces = [card.rank == "Ace" for card in self.hand.cards] tens = [card.val() == 10 for card in self.hand.cards] return any(aces) and any(tens) def bust(self): return self.hand.value > 21 def clear_hand(self): self.hand = Hand([])
class Player(object): """A labeled collection of cards that can be sorted""" #------------------------------------------------------------ def __init__(self, Name): """Creates player""" self.player = Name #------------------------------------------------------------ def getName(self): """ Add card to the hand """ return self.player def createHand(self): self.hand = Hand(self.player) def addCard(self, card): self.hand.add(card) def dumpCard(self): self.hand.dump()
class Player: def __init__(self): self.hand = Hand() self.trumpSuit = JOKER self.tNumber = 0 self.cardinal = 0 def giveCard(self, card): self.hand.add(card) self.hand.sort(self.trumpSuit, self.tNumber) def declaredTrump(self, suit, num): self.trumpSuit = suit self.cardinal = num print "Someone declared %d with %d cards" % (suit, num) def bury(self, n): i = n while i > 0: bc = input("Choose what card(s) to bury: ") for k in range(len(self.hand.cardList)): if self.hand.cardList[i].toString == bc: self.hand.cardList.pop(i) i = i - 1 break
def setup_game(): print("balance: " + str(balance)) # take the player's bet balance.make_bet() # set variables global current current = 0 global deck deck = Deck() global dealer_hand dealer_hand = Hand() global player_hand player_hand = Hand() # give initial cards player_hand.add(deck.cards[current]) current += 1 dealer_hand.add(deck.cards[current]) current += 1 player_hand.add(deck.cards[current]) current += 1 dealer_hand.add(deck.cards[current]) current += 1 print("dealer: ") dealer_hand.dealer_start() print("your hand: ") player_hand.player_start()
class Player: #initializes Player def __init__(self, name="Player"): self.playername = name self.money = 100 self.hand = Hand() #Prints information about player def __str__(self): print(f"Player: {self.playername}") print(f"Money: ${self.money}") print(self.hand) return "" def addcard(self, card): self.hand.add(card) def name(self): return self.playername def money(self): return self.money #mutator that adds to characters money def win(self, amount): self.money += amount print(f"\n{self.playername} wins {amount}!") print(f"New Balance: {self.money}\n") def lose(self, amount): self.money -= amount print(f"\n{self.playername} loses {amount}!") print(f"New Balance: {self.money}\n")
class War(object): def __init__(self): self.status = "not delt" self.deck = Deck() self.hands = [] self.playerUsedPile = Hand() self.computerUsedPile = Hand() self.warCounter = 0 self.rewardPile = [] def deal(self): self.status = "normal fight" self.deck.shuffle() self.hands = self.deck.dealHands(2) self.playerUsedPile = Hand() self.computerUsedPile = Hand() def flip(self): if self.status == "normal fight": playerCard = self.hands[0].pop() computerCard = self.hands[1].pop() if self.getCardValue(playerCard) > self.getCardValue(computerCard): self.playerUsedPile.add(playerCard) self.playerUsedPile.add(computerCard) elif self.getCardValue(playerCard) < self.getCardValue( computerCard): self.computerUsedPile.add(playerCard) self.computerUsedPile.add(computerCard) else: self.rewardPile.append(playerCard) self.rewardPile.append(computerCard) self.status = "war" self.warCounter = 3 elif self.status == "war": if self.warCounter > 0: self.rewardPile.append(self.hands[0].pop()) self.rewardPile.append(self.hands[1].pop()) self.warCounter -= 1 elif self.warCounter == 0: playerCard = self.hands[0].pop() computerCard = self.hands[1].pop() if self.getCardValue(playerCard) > self.getCardValue( computerCard): self.playerUsedPile.add(playerCard) self.playerUsedPile.add(computerCard) for card in self.rewardPile: self.playerUsedPile.add(card) self.rewardPile = [] self.status = "normal fight" self.warCounter -= 1 elif self.getCardValue(playerCard) < self.getCardValue( computerCard): self.computerUsedPile.add(playerCard) self.computerUsedPile.add(computerCard) for card in self.rewardPile: self.computerUsedPile.add(card) self.rewardPile = [] self.status = "normal fight" self.warCounter -= 1 else: self.rewardPile.append(playerCard) self.rewardPile.append(computerCard) self.status = "war" self.warCounter = 3 self.checkHandSizes() def checkHandSizes(self): if len(self.hands[0].cards) == 0: if len(self.playerUsedPile.cards) == 0: self.status = "gameover" else: self.playerUsedPile.shuffle() self.hands[0] = self.playerUsedPile self.playerUsedPile = Hand() if len(self.hands[1].cards) == 0: if len(self.computerUsedPile.cards) == 0: self.status = "gameover" else: self.computerUsedPile.shuffle() self.hands[1] = self.computerUsedPile self.computerUsedPile = Hand() def getCardValue(self, card): if ord(card.value[0]) < 50 or ord(card.value[0]) > 57: if ord(card.value[0]) == 49: return 10 elif ord(card.value[0]) == 74: return 11 elif ord(card.value[0]) == 81: return 12 elif ord(card.value[0]) == 75: return 13 elif ord(card.value[0]) == 65: return 14 else: return int(card.value)
''' Created on Apr 23, 2018 @author: jfernando ''' from Deck import Deck from Card import Card, Suit, Value from Set import Set from Hand import Hand test = Deck() print(str(test.len()) + Set.print(test)) test.add52() print(str(test.len()) + Set.print(test)) bop = Hand("Pablo") for x in range(0, 20): test.shuffle() bop.add(test.giveRand()) print( str(len(test.contents) + len(bop.contents)) + test.print() + "|" + bop.print()) print(str(len(test.contents)) + "|" + str(len(bop.contents))) bop.giveAll(test) print(str(len(test.contents)) + "|" + str(len(bop.contents)))
class ComputerAI: def __init__(self): self.hand = Hand() self.cardinal = 0 # How many cards were declared trump self.trumpSuit = JOKER self.tNumber = 0 self.trumps = 0 self.jokers = 0 self.spades = 0 self.hearts = 0 self.diamonds = 0 self.clubs = 0 def giveHand(self, h): self.hand = h # Reinitialize suit numbers self.trumps = 0 self.jokers = 0 self.spades = 0 self.hearts = 0 self.diamonds = 0 self.clubs = 0 for c in h: if c.suit == JOKER: self.jokers = self.jokers + 1 self.trumps = self.trumps + 1 elif c.number == self.tNumber: self.trumps = self.trumps + 1 elif c.suit == SPADES: self.spades = self.spades + 1 elif c.suit == HEARTS: self.hearts = self.hearts + 1 elif c.suit == DIAMONDS: self.diamonds = self.diamonds + 1 elif c.suit == CLUBS: self.clubs = self.clubs + 1 def declaredTrump(self, suit, num): # Sets trumps self.trumpSuit = suit self.cardinal = num def giveCard(self, c): # Adds the card into hand and then choose a card to declare, if appropriate self.hand.add(c) self.hand.sort(self.trumpSuit, self.tNumber) if c.suit == JOKER: self.jokers = self.jokers + 1 self.trumps = self.trumps + 1 elif c.number == self.tNumber: self.trumps = self.trumps + 1 elif c.suit == SPADES: self.spades = self.spades + 1 elif c.suit == HEARTS: self.hearts = self.hearts + 1 elif c.suit == DIAMONDS: self.diamonds = self.diamonds + 1 elif c.suit == CLUBS: self.clubs = self.clubs + 1 def avg(s): # returns a float that is the average value of the cards in a specific suit sum = 0 count = 0 for c in self.hand.cardList: if c.suit == s: sum = sum + c.number count = count + 1 if count > 0: return float(sum) / count else: return 0 def checkPairs(): # returns a float value that indicates how many pairs/triples in the hand count = 0 for c in self.hand.cardList: partial = 0.5 for d in self.hand.cardList: if c.equals(d): partial = partial + 0.5 if partial >= 1: count = count + partial return count # Logic for deciding card suit if self.trumps > self.cardinal: if self.cardinal == 0: temp = None count = 0 count_trump = 0 jpair = False for i in range(len(self.hand.cardList)): c = self.hand.cardList[i] if c.suit == JOKER: index = i how_many = 1 while index + 1 < len(self.hand.cardList) and self.hand.cardList[index + 1].equals(c): how_many = how_many + 1 index = index + 1 if how_many > 1 and (self.trumps > 4 or checkPairs() > 4): jpair = True temp = JOKER count = how_many count_trump = self.trumps elif c.number == self.tNumber: index = i how_many = 1 while index + 1 < len(self.hand.cardList) and self.hand.cardList[index + 1].equals(c): how_many = how_many + 1 index = index + 1 if len(self.hand.cardList) < 16: if c.suit == SPADES and self.spades > count_trump and self.spades > float(len(self.hand.cardList)) / 2: return SPADES + 5 * (how_many - 1) elif c.suit == HEARTS and self.hearts > count_trump and self.hearts > float(len(self.hand.cardList)) / 2: return HEARTS + 5 * (how_many - 1) elif c.suit == CLUBS and self.clubs > count_trump and self.clubs > float(len(self.hand.cardList)) / 2: return CLUBS + 5 * (how_many - 1) elif c.suit == DIAMONDS and self.diamonds > count_trump and self.diamonds > float(len(self.hand.cardList)) / 2: return DIAMONDS + 5 * (how_many - 1) else: return None else: if c.suit == SPADES and self.spades > count_trump and self.spades > float(len(self.hand.cardList)) / 4 and self.spades + self.trumps > float(len(self.hand.cardList)) / 3: temp = SPADES count = how_many count_trump = self.spades elif c.suit == HEARTS and self.hearts > count_trump and self.hearts > float(len(self.hand.cardList)) / 4 and self.hearts + self.trumps > float(len(self.hand.cardList)) / 3: temp = HEARTS count = how_many count_trump = self.hearts elif c.suit == CLUBS and self.clubs > count_trump and self.clubs > float(len(self.hand.cardList)) / 4 and self.clubs + self.trumps > float(len(self.hand.cardList)) / 3: temp = CLUBS count = how_many count_trump = self.clubs elif c.suit == DIAMONDS and self.diamonds > count_trump and self.diamonds > float(len(self.hand.cardList)) / 4 and self.diamonds + self.trumps > float(len(self.hand.cardList)) / 3: temp = DIAMONDS count = how_many count_trump = self.diamonds # end of for loop if jpair and temp == JOKER and count > 1: return temp + 5 * count elif count > 0 and count_trump > 0 and temp != JOKER: return temp + 5 * (count - 1) else: return None ############################# end of cardinal == 0 elif self.cardinal == 1: temp = None count = 0 count_trump = 0 jpair = False for i in range(len(self.hand.cardList)): c = self.hand.cardList[i] if c.suit == JOKER: index = i how_many = 1 while index + 1 < len(self.hand.cardList) and self.hand.cardList[index + 1].equals(c): how_many = how_many + 1 index = index + 1 if how_many > 1 and self.trumps > 4 and checkPairs() > 4 and avg(SPADES) + avg(HEARTS) + avg(DIAMONDS) + avg(CLUBS) > 34: jpair = True temp = JOKER count = how_many count_trump = self.trumps elif c.number == self.tNumber: index = i how_many = 1 while index + 1 < len(self.hand.cardList) and self.hand.cardList[index + 1].equals(c): how_many = how_many + 1 index = index + 1 if how_many > 1 and self.spades > count_trump: p = checkPairs() l = float(len(self.hand.cardList)) if c.suit == SPADES: if self.spades + self.trumps > l / 2.5 and p > 4: temp = SPADES count = how_many count_trump = self.spades elif self.spades + self.trumps > l / 2 and p > 2 and avg(HEARTS) + avg(DIAMONDS) + avg(CLUBS) > 27: temp = SPADES count = how_many count_trump = self.spades elif self.spades + self.trumps > l / 3 and p > 5: temp = SPADES count = how_many count_trump = self.spades elif c.suit == HEARTS: if self.hearts + self.trumps > l / 2.5 and p > 4: temp = HEARTS count = how_many count_trump = self.hearts elif self.hearts + self.trumps > l / 2 and p > 2 and avg(SPADES) + avg(DIAMONDS) + avg(CLUBS) > 27: temp = HEARTS count = how_many count_trump = self.hearts elif self.hearts + self.trumps > l / 3 and p > 5: temp = HEARTS count = how_many count_trump = self.hearts elif c.suit == CLUBS: if self.clubs + self.trumps > l / 2.5 and p > 4: temp = CLUBS count = how_many count_trump = self.clubs elif self.clubs + self.trumps > l / 2 and p > 2 and avg(SPADES) + avg(DIAMONDS) + avg(HEARTS) > 27: temp = CLUBS count = how_many count_trump = self.clubs elif self.clubs + self.trumps > l / 3 and p > 5: temp = CLUBS count = how_many count_trump = self.clubs elif c.suit == DIAMONDS: if self.diamonds + self.trumps > l / 2.5 and p > 4: temp = DIAMONDS count = how_many count_trump = self.diamonds elif self.diamonds + self.trumps > l / 2 and p > 2 and avg(SPADES) + avg(HEARTS) + avg(CLUBS) > 27: temp = DIAMONDS count = how_many count_trump = self.diamonds elif self.diamonds + self.trumps > l / 3 and p > 5: temp = DIAMONDS count = how_many count_trump = self.diamonds if jpair and temp == JOKER and count > 1: return temp + 5 * count elif count > 1 and count_trump > 0 and temp != JOKER: return temp + 5 * (count - 1) else: return None else: return None def bury(self, n): def checkPairs(x): count = 0 for c in self.hand.cardList: if c.suit == x: p = 1 for d in self.hand.cardList: if not c is d and d.suit == x and c.number != self.tNumber and c.equals(d): p = p + 1 if p > 1: count = count + p return count def countPoints(suit): sum = 0 for c in self.hand.cardList: if c.suit == suit and suit != 0 and c.number != self.tNumber: if c.number == 5: sum = sum + 5 elif c.number == 10 or c.number == 13: sum = sum + 10 elif suit == 0 and c.number == self.tNumber: if c.number == 5: sum = sum + 5 elif c.number == 10 or c.number == 13: sum = sum + 10 return sum def suitSingle(card): for c in self.hand.cardList: if not c is card and c.equals(card): return True return False tlist = [(1, self.spades), (2, self.hearts), (3, self.clubs), (4, self.diamonds)] tlist.sort(key = lambda x: x[1]) buriedCards = [] print tlist i = 0 while i < 4 and len(buriedCards) < n: if tlist[i][0] == self.trumpSuit: i = i + 1 pass # Check that the suit with the least number of cards contains points or pairs/triples. If those pairs and triples can be # subtracted from the total count of the suit to be less than the treasure card count, then we can proceed to bury them. x = countPoints(tlist[i][0]) y = checkPairs(tlist[i][0]) print x print y if x <= 10 * self.jokers and tlist[i][1] - y <= n: for c in self.hand.cardList: if c.suit == tlist[i][0] and c.number != self.tNumber and c.number != 14 and suitSingle(c): self.hand.remove(c) print "Got Here 1" buriedCards.append(c) print len(buriedCards) i = i + 1 if len(buriedCards) < n: print "F****d up!" else: return buriedCards
# Hand.py class Hand(object): def __init__(self, label=""): self.label = label self.cards = [] def add(self, card): self.cards.append(card) def dump(self): print(self.label + "'s Cards:") for c in self.cards: print(" ", c) from Hand import Hand from Card import Card h = Hand("North") h.add(Card(5, "c")) h.add(Card(10, "d")) h.add(Card(13, "s")) h.dump()
def play(self, extraDeckCount, players): shoe = Deck() for d in range(0, extraDeckCount): shoe.add52() shoe.shuffle() dealer = Hand() for p in players: v = Hand() v.add(shoe.giveRand()) v.add(shoe.giveRand()) p.addHand(v) dealer.add(shoe.giveRand()) dealer.add(shoe.giveRand()) for p in players: print("Player " + p.name + ": " + p.hands[0].print()) print("Dealer: " + dealer.contents[0].print() + ", ? of ?") for p in players: if p.hands[0].contents[0].value == p.hands[0].contents[1].value: i = input( "Player " + p.name + " do you wish to split? Hit enter to split, anything else before enter to continue. " ) if i != "": x = Hand() x.add(p.hands[0].giveRand()) p.hands.append(x) for p in players: for h in p.hands: stand = False print(h.print()) while h.eval() <= 21 and stand == False: t = input( "Player " + p.name + ", do you wish to hit? Type anything to hit, or enter blank to stand. " ) if len(t) > 0: h.add(shoe.giveRand()) else: stand = True print(h.print()) print( str(h.eval()) + " is Player " + p.name + "'s final value.") if (h.eval() == 21 & len(h.contents) == 2): print("Natural Blackjack!") if (h.eval() > 21): print("Player " + p.name + " busted!") print("Dealer " + dealer.print()) while dealer.eval() < 17: dealer.add(shoe.giveRand()) print("Dealer " + dealer.print()) d = dealer.eval() print("The Dealer's final value is " + str(d) + ".") if (len(dealer.contents) == 2): print("Natural Blackjack!") if (dealer.eval() > 21): print("The Dealer busted!") for p in players: for h in p.hands: v = h.eval() if v == 21 and len( h.contents) == 2 and len(dealer.contents) != 2: print("Player " + p.name + " won $15 with a natural Blackjack!") p.cash += 15 elif v == d: print("Player " + p.name + " tied the dealer.") elif v <= 21 and v > d: print("Player " + p.name + " won $10!") p.cash += 10 elif v <= 21 and d > 21: print("Player " + p.name + " won $10!") p.cash += 10 else: print("Player " + p.name + " lost $10") p.cash -= 10
def main(): D = Deck(); #create a deck of 52 cards D.shuffle() rank = randrange(1,14) suit = choice(Card.SUITS) myCard = Card(rank, suit) index = Card.SUITS.index(suit) suitN = Card.SUIT_NAMES[index] print("\nThe card is:", myCard, "and the trump suit is:",suitN,"\n") #outputs trump suit and card PN = 0 PS = 0 n = Hand("North") s = Hand("South") for numbers in range(1,27): TN = D.deal() print("round:", numbers,) TS = D.deal() print(TN) print(TS) #if both are prime suits if ((TN.suit() == TS.suit()) and (TN.suit() == suit)): if TN.rank() > TS.rank(): print("North won this round\n") n.add(TN) n.add(TS) PN+=1 else: print("South won this round\n") s.add(TS) s.add(TS) PS+=1 #if they are not the same suit and neither is the prime suit elif ((TN.suit() != TS.suit()) and (TN.suit() != suit) and (TS.suit() != suit)): print("Cards are discarded") elif (TN.suit() == suit and TS.suit() != suit): print("North won this round\n") n.add(TN) n.add(TS) PN+=1 elif (TN.suit != suit and TS.suit() == suit): print("South won this round\n") s.add(TS) s.add(TS) PS+=1 #if they are same suit but not prime suit elif(TN.suit() == TS.suit() and TN.suit() != suit): if TN.rank() > TS.rank(): print("North won this round\n") n.add(TN) n.add(TS) PN+=1 elif TS.rank() > TN.rank(): print("South won this round\n") s.add(TS) s.add(TS) PS+=1 else: print("Needs to be programmed\n") print("North player has ",PN," points") print("South player has ",PS," points") if(PN > PS): print("\nNorth Wins") elif(PS > PN): print("\nSouth Wins") elif(PN == PS): print("Nobody wins, it's a tie")