+/IBCS

git clone https://git.julia.blue/+/IBCS

Joseph RyanAdd acsl 1c0ce159

master
5.3 KiB158 linesraw
1import random
2
3rankValue = {"Two": 2, "Three": 3, "Four": 4, "Five": 5, "Six": 6, "Seven": 7, "Eight": 8, "Nine": 9, "Ten": 10, "Jack": 10, "Queen": 10, "King": 10, "Ace": 11}
4
5
6class Card:
7
8    def __init__(self, n, s):
9        self.suit = s
10        self.rank = n
11        self.value = rankValue[n]
12
13    def __str__(self):
14        return self.rank + " of " + self.suit
15
16
17class Hand:
18
19    def __init__(self):
20        self.cards = []
21        self.bet = 0
22
23    def __str__(self):
24        return ", ".join([i.__str__() for i in self.cards])
25
26    def score(self):
27        s = [0]
28        for c in self.cards:
29            for i in range(len(s)):
30                s[i] += c.value
31            if c.rank == 'Ace':
32                s.append(s[-1] - 10)
33        if any(i < 21 for i in s):
34            return max([i for i in s if i <= 21])
35        return max(s)
36
37    def hit(self, c):
38        self.cards.append(c)
39
40
41class Deck:
42
43    def __init__(self):
44        self.reset()
45        self.shuffle()
46
47    def reset(self):
48        self.cards = [Card(n, s) for n in rankValue.keys() for s in ["Spades", "Hearts", "Diamonds", "Clubs"]]
49
50    def shuffle(self):
51        random.shuffle(self.cards)
52
53    def deal(self):
54        temp = self.cards[-1]
55        del(self.cards[-1])
56        return temp
57
58
59class Player:
60
61    def __init__(self, n):
62        self.money = 100
63        self.name = n
64        self.hands = [Hand()]
65
66    def __str__(self):
67        return self.name + " ($" + str(self.money) + ")"
68
69    def currentBet(self):
70        return sum([h.bet for h in self.hands])
71
72players = [Player(raw_input("Player " + str(i + 1) + "'s name: ")) for i in range(input("How many players?\n"))]
73
74while True:
75    d = Deck()
76    dealer = Hand()
77    for i in range(len(players)):
78        if players[i].money < 10:
79            print players[i].name, "is out of money, and is removed from the game."
80    players = [players[i] for i in range(len(players)) if players[i].money > 9]
81    if len(players) == 0:
82        print "Dealer wins!"
83        break
84    for i in range(2):
85        dealer.hit(d.deal())
86    for p in players:
87        p.hands = [Hand()]
88        for i in range(2):
89            p.hands[0].hit(d.deal())
90        p.hands[0].bet = max(min(input("How much would you like to bet, " + p.name + "? (min $10) you have $" + str(p.money) + "\n"), p.money), 10)
91        p.money -= p.hands[0].bet
92    print "The dealer has a", dealer.cards[-1], "showing.\n"
93    for p in players:
94        print p, "Current bet:", p.currentBet()
95        print "Would you like to surrender? (yes,no)"
96        if(raw_input() == "yes"):
97            print "You get", p.currentBet() / 2, "back"
98            p.money += p.currentBet() / 2
99            continue
100        for h in p.hands:
101            while h.score() < 22:
102                print p.name + "'s hand: " + str(h)
103                if h.score() == 21 and len(p.hands) == 1 and len(h.cards) == 2:
104                    if raw_input("BLACKJACK!\n would you like even money? (yes,no)\n") == "yes":
105                        p.money += p.currentBet() * 2
106                        print "You take your", p.currentBet()*2,"and run"
107                        break
108                print "Would", p.name, "like to:", "split," if h.cards[0].rank == h.cards[1].rank and len(h.cards) == 2 and p.money > h.bet else "", "double down," if len(h.cards) == 2 and p.money > h.bet else "", "hit, or stay"
109                choice = raw_input()
110                if choice == "double down":
111                    h.hit(d.deal())
112                    p.money -= h.bet
113                    h.bet *= 2
114                    break
115                elif choice == "hit":
116                    h.hit(d.deal())
117                elif choice == "split":
118                    p.hands.append(Hand())
119                    p.hands[-1].cards.append(h.cards[1])
120                    del(h.cards[1])
121                    h.hit(d.deal())
122                    p.hands[-1].hit(d.deal())
123                    p.hands[-1].bet = h.bet
124                    p.money -= h.bet
125                else:
126                    break
127            print "Final hand:", h
128            print "bust!\n" if h.score() > 21 else ""
129
130    print "Dealer reveals a", dealer.cards[0]
131    if dealer.score() == 17 and (dealer.cards[0].rank == "Ace" or dealer.cards[1].rank == "Ace"):
132        dealer.hit(d.deal())
133        print "Dealer hits on a soft 17, and gets a", dealer.cards[-1]
134    while dealer.score() < 17:
135        dealer.hit(d.deal())
136        print "Dealer hits, and gets a", dealer.cards[-1]
137    print "Dealer's final hand:", dealer
138    if dealer.score() > 21:
139        print "Dealer busted"
140    for p in players:
141        winnings = 0
142        for h in p.hands:
143            if h.score() == 21 and dealer.score() != 21 and len(h.cards) == 2 and len(p.hands) == 1:
144                winnings += h.bet * 2.5
145            elif h.score() < 22 and dealer.score() > 21 or h.score() > dealer.score() and h.score() < 22:
146                winnings += h.bet * 2
147            elif h.score() == dealer.score():
148                winnings += h.bet
149        if winnings > 0:
150            print p.name, "wins", winnings - p.currentBet(), "dollars."
151            p.money += winnings
152        else:
153            print p.name, "loses", p.currentBet() - winnings
154
155# dealer has soft 17, hits and busts
156# player has ace ace splits to get ace jack stay, ace ace, splits to get ace 8 stay, ace three double down get 2
157
158# dealer has ace showing, but no blackjack, player takes even money