91ac8517 by Barry

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

1 parent ee04134a
from random import randrange
SUITS = (':clubs:', ':spades:', ':hearts:', ':diamonds:')
CARDS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
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}
class Hand:
def __init__(self):
self.cards = []
def add_cards(self, cards):
if isinstance(cards, list):
for card in cards:
self.cards.append(card)
else:
self.cards.append(cards)
def remove_card(self, card):
self.cards.remove(card)
def get_cards(self):
return self.cards
def get_points(self):
value = 0
number_of_aces = 0
for card in self.cards:
value = value + CARD_VALUES[card[0]]
if card[0] == 'A':
number_of_aces = number_of_aces + 1
while value > 21 and number_of_aces > 0:
value = value - 10
number_of_aces = number_of_aces - 1
return value
def is_blackjack(self):
if self.get_points() == 21 and len(self.cards) == 2:
return True
else:
return False
class Deck:
def __init__(self, number_of_decks=1):
self.shuffle(number_of_decks)
def shuffle(self, number_of_decks):
self.cards = []
for d in range(number_of_decks):
for suit in SUITS:
for card in CARDS:
self.cards.append(card + suit)
def draw(self,number_of_cards=1):
drawn_cards = []
for n in range(number_of_cards):
choice = randrange(0,len(self.cards))
drawn_cards.append(self.cards.pop(choice))
return drawn_cards
def cards_left(self):
return len(self.cards)
class Blackjack:
def __init__(self, bet):
self.bet = bet
self.deck = Deck()
self.player_hand = Hand()
self.player_hand.add_cards(self.deck.draw(2))
#print(player_hand.get_cards())
self.player_points = self.player_hand.get_points()
#print("Player: {}".format(player_points,))
self.dealer_hand = Hand()
self.dealer_hand.add_cards(self.deck.draw(2))
#print(dealer_hand.get_cards())
self.dealer_points = self.dealer_hand.get_points()
#print("Dealer: {}".format(dealer_points,))
def is_busted(self):
if self.player_points > 21:
return 1
elif self.dealer_points > 21:
return 2
else:
return 0
def is_win(self):
# TODO: Check for blackjack Ace + 10 pt
if self.player_hand.is_blackjack() and not self.dealer_hand.is_blackjack():
return (self.bet + self.bet * 1.5, 'Blackjack, you win: {}')
elif self.dealer_hand.is_blackjack():
return (-self.bet, 'You lose: {}')
elif self.player_points > self.dealer_points:
return (self.bet * 2, 'You win: {}')
elif self.player_points == self.dealer_points:
return (self.bet, 'push your bet is returned: {}')
else:
return (-self.bet, 'You lose: {}')
def print_hand(self):
dealers = ' '.join(self.dealer_hand.get_cards()[1:])
print("Dealer's Hand: {}".format(dealers))
print("Player's Hand: {} for {} points".format(' '.join(self.player_hand.get_cards()), self.player_points))
bet = 10
bj = Blackjack(bet)
bj.print_hand()
busted = bj.is_busted()
if busted == 1:
print("YOU BUST")
elif busted == 2:
print("Dealer Busts, YOU WIN!")
else:
win, response = bj.is_win()
print(response.format(win,))
No preview for this file type
......@@ -12,6 +12,7 @@ import wikipedia
from dateutil.parser import parse
from discord.object import Object
from discord.channel import PrivateChannel
from ago import human
import simplejson as json
from collections import defaultdict
......@@ -555,7 +556,24 @@ Stuff:
out_string = out_string[1900:]
return
if message.content.startswith('!bet'):
if type(message.channel) is not discord.channel.PrivateChannel:
client.send_message(message.author, "You must make all bets / gaming via private message.")
else:
bet_amount = message.content[5:]
client.send_message(message.author, "Welcome to BlackJack! :flower_playing_cards: You have placed a bet of: {}".format(bet_amount))
client.send_message(message.author, "Your Hand: {} {}".format(byteify('A:diamonds:'), byteify('J:hearts:')))
client.send_message(message.author, "Dealers Hand: {} {}".format(byteify('A:diamonds:'), byteify(':bell:')))
return
if message.content.startswith('!direct'):
channel = message.channel
author = message.author
log("{} {} - type: {}".format(channel.id, author.id, type(channel)))
client.send_message(author, "test")
return
# !msg joe in 5 minutes YOU ARE A DICK
if message.content.startswith('!msg'):
try:
......