Last active
January 6, 2018 03:05
-
-
Save crushedhat/2e739704d3189909ca84a8f7e3e887e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from random import choice as rchoice | |
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)) | |
#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 = rchoice(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 | |
self.betValue = 0 | |
self.playerbank = 500 | |
def subMoney(self): | |
self.playerbank -= self.betValue | |
def addMoney(self): | |
self.playerbank += self.betValue | |
#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 Card Total: ", self.playerTotal) | |
print ("Player Bank: ", self.playerbank) | |
#method to deal two cards out to the player | |
def initialDeal(self, deck): | |
self.playerHand(deck.drawCard(), deck.cardValue()) | |
self.playerHand(deck.drawCard(), deck.cardValue()) | |
#Check player total | |
def playerCheck(self): | |
self.count = 0 | |
if self.playerTotal > 21: | |
self.count += 1 | |
return self.count | |
elif self.playerTotal == 21: | |
self.count += 0 | |
return self.count | |
def bankCheck(self): | |
return self.playerbank | |
def clearHand(self): | |
self.playerCards = [] | |
self.playerTotal = 0 | |
self.betValue = 0 | |
def playerBet(self): | |
self.bet = int(input("Place your bet: ")) | |
self.bet_check() | |
def bet_check(self): | |
if self.bet < self.playerbank: | |
self.betValue += self.bet | |
else: | |
print ("Your bet is higher than the amount of money you have in your hand") | |
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 emptyDeck(): | |
clearScreen() | |
print (" =========================================================================") | |
print (" =========================================================================") | |
print (" ========================= The Cards Have Run Out ========================") | |
print (" ===================== Exiting Back To The Main Menu =====================") | |
print (" =========================================================================") | |
print (" =========================================================================\n\n\n\n\n\n\n\n\n\n\n\n") | |
def emptyBank(): | |
clearScreen() | |
print (" =========================================================================") | |
print (" =========================================================================") | |
print (" ========================= You Have Run Out Money ========================") | |
print (" ===================== Exiting Back To The Main Menu =====================") | |
print (" =========================================================================") | |
print (" =========================================================================\n\n\n\n\n\n\n\n\n\n\n\n") | |
def clearScreen(): | |
print ("\n" * 50) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import gameClass | |
import time | |
while True: | |
newDeal = False | |
gameClass.mainMenu() | |
deck = gameClass.Deck() | |
player = gameClass.Player() | |
menuSelection = input("Selection: ") | |
if menuSelection == '1': | |
gameClass.clearScreen() | |
player.playerBet() | |
player.initialDeal(deck) | |
while newDeal == False: | |
gameClass.menu() | |
if player.bankCheck() <= 0: | |
gameClass.emptyBank() | |
time.sleep(1) | |
break | |
player.displayHand() | |
selection = input("Selection: ") | |
if selection == '1': | |
player.playerHand(deck.drawCard(), deck.cardValue()) | |
player.displayHand() | |
if player.playerCheck() == 1: | |
gameClass.bust() | |
time.sleep(1) | |
player.subMoney() | |
player.clearHand() | |
player.playerBet() | |
player.initialDeal(deck) | |
elif player.playerCheck() == 0: | |
gameClass.win() | |
time.sleep(1) | |
player.addMoney() | |
player.clearHand() | |
player.playerBet() | |
player.initialDeal(deck) | |
if deck.checkDeck() == True: | |
gameClass.emptyDeck() | |
time.sleep(1) | |
break | |
gameClass.clearScreen() | |
if selection == '2': | |
time.sleep(1) | |
gameClass.clearScreen() | |
if deck.checkDeck() == True: | |
gameClass.emptyDeck() | |
time.sleep(1) | |
break | |
player.addMoney() | |
player.clearHand() | |
player.playerBet() | |
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