Skip to content

Instantly share code, notes, and snippets.

@monkeyworknet
Last active December 15, 2015 05:20
Show Gist options
  • Save monkeyworknet/5208703 to your computer and use it in GitHub Desktop.
Save monkeyworknet/5208703 to your computer and use it in GitHub Desktop.
posted to reddit.com/r/learnpython for feedback on improvements
from random import choice
from random import randint
drawncards = []
#playername = raw_input("What is your name: ")
playername = "Kevin"
""" Player Dict format
key = name
value = [[handscore], [availablecash]]
"""
players={
"Dealer": [[]],
playername : [[]],
"Wayne G." : [[]],
"Mike J." : [[]],
"Joe M." : [[]]
}
numberOfDecks = 5
""" Insert a red reshuffle card into the shoe, will appear randomly after the first 52 cards have been dealt but before the last 20 """
reshufflemarker = randint(52, (52*numberOfDecks) - 20)
print "Reshuffling at ", reshufflemarker
def drawfromshoe():
""" Function will draw a card and reshuffle deck if needed """
startingdeck = range(1,14)
suits = ["H","C","D","S"]
facecards = {1:"A", 11:"J", 12:"Q", 13:"K", 10:"T" }
card = ""
global drawncards
global reshufflemarker
if len(drawncards) == reshufflemarker:
print "Red Shuffle Card Drawn - Reshuffling Deck"
reshufflemarker = randint(52, (52*numberOfDecks) - 20)
drawncards=[]
""" We don't want to reshuffle in the currently in play cards """
for player in players:
for card in players[player][0]:
drawncards.append(card)
while drawncards.count(card) >= numberOfDecks or card == "":
card = choice(startingdeck)
suit = choice(suits)
if card in facecards:
whichface = facecards[card]
card = whichface
card = str(card) + suit
drawncards.append(card)
return card
def handvalue(hand):
handvalue = [0,0]
valueTencards = ["J","Q","K","T"]
for card in hand:
if card[0] in valueTencards:
handvalue[0] = handvalue[0] + 10
handvalue[1] = handvalue[1] + 10
elif card[0] == "A":
handvalue[0] = handvalue[0] + 1
handvalue[1] = handvalue[1] + 11
else:
handvalue[0] = handvalue[0] + int(card[0])
handvalue[1] = handvalue[1] + int(card[0])
if handvalue[0] == handvalue[1]:
return handvalue[0]
if handvalue[0] < 21 and handvalue[1] < 21:
return handvalue
else:
if handvalue[0] < handvalue[1]:
return handvalue[0]
else:
return handvalue[1]
""" Deal 2 cards to everyone """
for player in players:
players[player][0].append(drawfromshoe())
for player in players:
players[player][0].append(drawfromshoe())
""" Print out names, initial cards and scores """
for player in players:
print(player, players[player][0], handvalue(players[player][0]))
#!/usr/bin/env python
from random import choice
from random import randint
class Players(object):
status = "playing"
def __init__(self, name, hand, Score, cash, currentbet, strategy, role):
self.name = name
self.hand = hand
self.Score = Score
self.cash = cash
self.currentbet = currentbet
self.strategy = strategy
self.role = role
def standing(self):
status = "stand"
#playername = raw_input("Please input your name: ")
playername = "Kevin"
## Initialize players
dealer = Players("Edward T.", [], [], 10000, 0, "dealer", "dealer")
computer1 = Players("Mike J.", [], [], 1000, 0, "aggressive", "ai")
computer2 = Players("Wayne G.", [], [], 1000, 0, "cautious", "ai")
computer3 = Players("Tom B.", [], [], 1000, 0, "professional", "ai")
computer4 = Players("John M.", [], [], 1000, 0, "professional", "ai")
human1 = Players(playername, [], [], 1000, 0, "human", "player")
GamePlayers = [computer1, computer2, computer3, computer4, human1, dealer]
## Initialize Card shoe
## Insert a red reshuffle Card into the shoe, will appear randomly after the first 52 Cards have been dealt but before the last 20
## The Shoe will contain several decks as a real casino would
numberofdecks = 5
DrawnCards = []
reshufflemarker = randint(52, (52*numberofdecks) - 20)
def drawfromshoe():
startingdeck = range(1,14)
Suits = ["H","C","D","S"]
faceCards = {1:"A", 11:"J", 12:"Q", 13:"K", 10:"T" }
Card = ""
global DrawnCards
global reshufflemarker
if len(DrawnCards) == reshufflemarker:
print "Red Shuffle Card Drawn - Reshuffling Deck"
reshufflemarker = randint(52, (52*numberofdecks) - 20)
DrawnCards=[]
""" We don't want to reshuffle in the currently in play Cards """
for player in GamePlayers:
for Card in player.hand:
DrawnCards.append(Card)
while DrawnCards.count(Card) >= numberofdecks or Card == "":
Card = choice(startingdeck)
suit = choice(Suits)
if Card in faceCards:
whichface = faceCards[Card]
Card = whichface
Card = str(Card) + suit
DrawnCards.append(Card)
return Card
## Scoring Routine
## Returns a list due to an Ace offering up multiple scoring options
## Returns 99 on a bust
def handvalue(hand):
ValueTenCards = ["J","Q","K","T"]
Score=[0]
for Card in hand:
if Card[0] == "A":
Score[0] = Score[0] + 1
if len(Score) > 1:
ScoreOptions = range(1,len(Score))
for x in ScoreOptions:
Score[x] = Score[x] + 11
Score.append(11 + (Score[0]-1))
elif Card[0] in ValueTenCards:
Score[0] = Score[0] + 10
if len(Score) > 1:
ScoreOptions = range(1,len(Score))
for x in ScoreOptions:
Score[x] = Score[x] + 10
else:
Score[0] = Score[0] + int(Card[0])
if len(Score) > 1:
ScoreOptions = range(1,len(Score))
for x in ScoreOptions:
Score[x] = Score[x] + int(Card[0])
if 21 in Score:
return [21]
finalscore = [y for y in Score if y <= 21]
if len(finalscore) == 0:
finalscore = [99]
return finalscore
## Hand out two cards to each player
for player in GamePlayers:
player.hand.append(drawfromshoe())
for player in GamePlayers:
player.hand.append(drawfromshoe())
## Manually setting hand for debugging
#human1.hand = ["2H", "3D", "AS", "AS", "AD"]
#print human1.hand
#print handvalue(human1.hand)
## Print out players and their cards
for player in GamePlayers:
print(player.name, player.hand, handvalue(player.hand))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment