Skip to content

Instantly share code, notes, and snippets.

@Stantheman
Forked from crushedhat/Main.py
Last active January 15, 2018 05:05
Show Gist options
  • Save Stantheman/263ab99d7f596f0529e5c5be7aabac9f to your computer and use it in GitHub Desktop.
Save Stantheman/263ab99d7f596f0529e5c5be7aabac9f to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from random import choice, seed
import re
class Deck:
#Create a new deck of cards
def __init__(self):
self.deck = []
self.cardType = ['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']
self.cardSuit = ['Hearts','Clubs', 'Diamonds','Spades']
for card in self.cardSuit:
for face in self.cardType:
self.deck.append("{} of {}".format(face, card))
seed(5)
#Check how many cards are left in the list
def checkDeck(self):
if len(self.deck) <= 10:
return True
#randomly draw a card from deck
def drawCard(self):
self.choice = choice(self.deck)
self.deck.remove(self.choice)
return self.choice
#Find card value utilzing regex for face and numbered cards
def cardValue(self):
tenValue = re.match(r'\A(K|Q|J|10)', self.choice)
aceValue = re.match(r'\A(A)', self.choice)
normalValue = re.match(r'\A(1|2|3|4|5|6|7|8|9)', self.choice)
aceVal = 0
if tenValue:
self.faceValue = 10
return self.faceValue
elif normalValue:
return int(self.choice[0])
elif aceValue:
aceVal += 1
return aceVal
class Player:
#initialize players hand and total
def __init__(self):
self.playerCards = []
self.playerTotal = 0
#save value and type of card player has drawn
def playerHand(self, card, value):
self.playerCards.append(card)
if value == 1 and self.playerTotal <= 10:
self.playerTotal += 11
elif value:
self.playerTotal += value
else:
self.playerTotal += value
def displayHand(self):
print "Player Cards: ", self.playerCards
print "Player Total: ", self.playerTotal
def initialDeal(self, deck):
self.playerHand(deck.drawCard(), deck.cardValue())
self.playerHand(deck.drawCard(), deck.cardValue())
#Check player total
def playerCheck(self):
if self.playerTotal > 21:
bust = "BUST"
self.clearHand()
return bust
elif self.playerTotal == 21:
blackjack = "BJ"
self.clearHand()
return blackjack
def clearHand(self):
self.__init__()
#!/usr/bin/python
import gameClass
import time
def mainMenu():
clearScreen()
print " Welcome to the Blackjack table! Please select an option below"
print " ========================================================================="
print " ==============================(1) New Game =============================="
print " ==============================(2) Exit =============================="
print " =========================================================================\n\n\n\n\n\n\n\n\n\n\n\n"
def menu():
clearScreen()
print " ========================================================================="
print " ================================ (1)Hit ================================"
print " ================================ (2)Stay ================================"
print " ================================ (3)Exit ================================"
print " =========================================================================\n\n\n\n\n\n\n\n\n\n\n\n"
def bust():
clearScreen()
print " ========================================================================="
print " ========================================================================="
print " =========================== PLAYER HAS BUSTED! =========================="
print " ========================================================================="
print " =========================================================================\n\n\n\n\n\n\n\n\n\n\n\n"
def win():
clearScreen()
print " ========================================================================="
print " ========================================================================="
print " ========================== PLAYER HAS BLACKJACK! ========================"
print " ========================================================================="
print " =========================================================================\n\n\n\n\n\n\n\n\n\n\n\n"
def clearScreen():
print ("\n" * 50)
while True:
newDeal = False
mainMenu()
deck = gameClass.Deck()
player = gameClass.Player()
menuSelection = raw_input("Selection: ")
if menuSelection == '1':
clearScreen()
player.initialDeal(deck)
while newDeal == False:
menu()
player.displayHand()
selection = raw_input("Selection: ")
if selection == '1':
player.playerHand(deck.drawCard(), deck.cardValue())
player.displayHand()
if player.playerCheck() == "BUST":
clearScreen()
bust()
time.sleep(1)
player.initialDeal(deck)
elif player.playerCheck() == "BJ":
clearScreen()
win()
time.sleep(1)
player.initialDeal(deck)
if deck.checkDeck() == True:
break
clearScreen()
if selection == '2':
time.sleep(1)
clearScreen()
if deck.checkDeck() == True:
break
player.clearHand()
player.initialDeal(deck)
deck.checkDeck()
elif selection == '3':
break
if menuSelection == '2':
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment