91ac8517 by Barry

A couple changes but mostly just adding a quick n dirty blackjack minigame.

1 parent ee04134a
1 from random import randrange
2
3 SUITS = (':clubs:', ':spades:', ':hearts:', ':diamonds:')
4 CARDS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
5 CARD_VALUES = {'A':11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
6
7 class Hand:
8 def __init__(self):
9 self.cards = []
10
11 def add_cards(self, cards):
12 if isinstance(cards, list):
13 for card in cards:
14 self.cards.append(card)
15 else:
16 self.cards.append(cards)
17
18 def remove_card(self, card):
19 self.cards.remove(card)
20
21 def get_cards(self):
22 return self.cards
23
24 def get_points(self):
25 value = 0
26 number_of_aces = 0
27 for card in self.cards:
28 value = value + CARD_VALUES[card[0]]
29 if card[0] == 'A':
30 number_of_aces = number_of_aces + 1
31
32 while value > 21 and number_of_aces > 0:
33 value = value - 10
34 number_of_aces = number_of_aces - 1
35 return value
36
37 def is_blackjack(self):
38 if self.get_points() == 21 and len(self.cards) == 2:
39 return True
40 else:
41 return False
42
43
44 class Deck:
45 def __init__(self, number_of_decks=1):
46 self.shuffle(number_of_decks)
47
48 def shuffle(self, number_of_decks):
49 self.cards = []
50 for d in range(number_of_decks):
51 for suit in SUITS:
52 for card in CARDS:
53 self.cards.append(card + suit)
54
55 def draw(self,number_of_cards=1):
56 drawn_cards = []
57 for n in range(number_of_cards):
58 choice = randrange(0,len(self.cards))
59 drawn_cards.append(self.cards.pop(choice))
60 return drawn_cards
61
62 def cards_left(self):
63 return len(self.cards)
64
65 class Blackjack:
66 def __init__(self, bet):
67 self.bet = bet
68 self.deck = Deck()
69 self.player_hand = Hand()
70 self.player_hand.add_cards(self.deck.draw(2))
71 #print(player_hand.get_cards())
72 self.player_points = self.player_hand.get_points()
73 #print("Player: {}".format(player_points,))
74
75 self.dealer_hand = Hand()
76 self.dealer_hand.add_cards(self.deck.draw(2))
77 #print(dealer_hand.get_cards())
78 self.dealer_points = self.dealer_hand.get_points()
79 #print("Dealer: {}".format(dealer_points,))
80
81 def is_busted(self):
82 if self.player_points > 21:
83 return 1
84 elif self.dealer_points > 21:
85 return 2
86 else:
87 return 0
88
89 def is_win(self):
90 # TODO: Check for blackjack Ace + 10 pt
91 if self.player_hand.is_blackjack() and not self.dealer_hand.is_blackjack():
92 return (self.bet + self.bet * 1.5, 'Blackjack, you win: {}')
93 elif self.dealer_hand.is_blackjack():
94 return (-self.bet, 'You lose: {}')
95 elif self.player_points > self.dealer_points:
96 return (self.bet * 2, 'You win: {}')
97 elif self.player_points == self.dealer_points:
98 return (self.bet, 'push your bet is returned: {}')
99 else:
100 return (-self.bet, 'You lose: {}')
101
102 def print_hand(self):
103 dealers = ' '.join(self.dealer_hand.get_cards()[1:])
104 print("Dealer's Hand: {}".format(dealers))
105
106
107 print("Player's Hand: {} for {} points".format(' '.join(self.player_hand.get_cards()), self.player_points))
108 bet = 10
109 bj = Blackjack(bet)
110 bj.print_hand()
111 busted = bj.is_busted()
112 if busted == 1:
113 print("YOU BUST")
114 elif busted == 2:
115 print("Dealer Busts, YOU WIN!")
116 else:
117 win, response = bj.is_win()
118 print(response.format(win,))
No preview for this file type
...@@ -12,6 +12,7 @@ import wikipedia ...@@ -12,6 +12,7 @@ import wikipedia
12 from dateutil.parser import parse 12 from dateutil.parser import parse
13 13
14 from discord.object import Object 14 from discord.object import Object
15 from discord.channel import PrivateChannel
15 from ago import human 16 from ago import human
16 import simplejson as json 17 import simplejson as json
17 from collections import defaultdict 18 from collections import defaultdict
...@@ -555,7 +556,24 @@ Stuff: ...@@ -555,7 +556,24 @@ Stuff:
555 out_string = out_string[1900:] 556 out_string = out_string[1900:]
556 return 557 return
557 558
559 if message.content.startswith('!bet'):
560 if type(message.channel) is not discord.channel.PrivateChannel:
561 client.send_message(message.author, "You must make all bets / gaming via private message.")
562 else:
563 bet_amount = message.content[5:]
564 client.send_message(message.author, "Welcome to BlackJack! :flower_playing_cards: You have placed a bet of: {}".format(bet_amount))
565 client.send_message(message.author, "Your Hand: {} {}".format(byteify('A:diamonds:'), byteify('J:hearts:')))
566 client.send_message(message.author, "Dealers Hand: {} {}".format(byteify('A:diamonds:'), byteify(':bell:')))
567 return
568
569 if message.content.startswith('!direct'):
570
571 channel = message.channel
572 author = message.author
558 573
574 log("{} {} - type: {}".format(channel.id, author.id, type(channel)))
575 client.send_message(author, "test")
576 return
559 # !msg joe in 5 minutes YOU ARE A DICK 577 # !msg joe in 5 minutes YOU ARE A DICK
560 if message.content.startswith('!msg'): 578 if message.content.startswith('!msg'):
561 try: 579 try:
......