Skip to content

Instantly share code, notes, and snippets.

@montycheese
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montycheese/660ef203e503677576ee to your computer and use it in GitHub Desktop.
Save montycheese/660ef203e503677576ee to your computer and use it in GitHub Desktop.
Black Jack game
from random import randint
import datetime
score = 100
round = 1
#date and time included in high scores
now = datetime.datetime.now()
today = datetime.date.today()
numericalDate = "%d/%d/%d" % (today.day, today.month, today.year)
def add_high_score(playerName, score):
filename = open("blackjack_highscores.txt", 'r+').read()
highScores_evaluated = eval(filename)
if score > highScores_evaluated[1] :
print "New HighScore!, congrats %s" % playerName
highScores[1] = [playerName, score, numericalDate]
#FIND A WAY TO ALLOW PROGRAM TO CHANGE VALUE OF HIGHSCORE WITHOUT using "w+" and erasing
#other scores
filename.write(str(highScores[1]))
filename.close()
else:
highScores = {}
for ranks in range(1, 11):
#create ranking system
if ranks == 1:
highScores[ranks] = [playerName, score, numericalDate]
else:
#default values
playerName = "MontyCheese"
score = 0
highScores[ranks] = [playerName, score, numericalDate]
#return highScores
filename.write(str(highScores))
filename.close()
def check_high_score():
pass # create later to check high scores
def create_deck():
deck = []
for number in range(2,12):
if number == 11:
for x in range(4): #looping to get 4 values of each card.
deck.append("Jack")
deck.append("Queen")
deck.append("King")
deck.append("Ace")
else:
for x in range(4): #looping to get 4 values of each card.
deck.append(number)
return deck
class Deck(object):
def __init__(self, deck):
self.deck = deck
self.size = len(create_deck())
#method to deal a random card from deck
def deal_card(self):
return self.deck[randint(0, self.size - 1)]
#method that takes face card string values and returns their equivalencies
def face_card_rank(self, card):
if card == "Jack":
return 10
elif card == "Queen":
return 10
elif card == "King":
return 10
elif card == "Ace":
return 1
#default for non-face cards
else:
return card
class Game(Deck):
def __init__(self, playerName, score, round):
self.playerName = playerName
self.score = score
self.round = round
def first_round(self, card1, card2):
if self.round == 6:
print "Thanks for playing %s, your total score is %d" % (self.playerName, self.score)
else:
print "\nRound %d" % self.round
print "Dealer deals:\n" + str(card1) + " " + str(card2)
if (card1 == "Ace" and type(card2) == type(str())) or (card2 == "Ace" and type(card1) == type(str())):
print "BLACKJACK!"
playAgain = raw_input("Another round? (Y/N): ")
if playAgain.upper() == "Y":
self.round += 1
return self.first_round(newDeck.deal_card(), newDeck.deal_card())
else:
print "Thanks for playing, your final score is %d" % self.score
else:
if type(card1) == type(str()) or type(card2) == type(str()):
return self.decision_making(self.round, newDeck.face_card_rank(card1) + newDeck.face_card_rank(card2))
#call face_card_rank method to convert face cards to integer value
else:
return self.decision_making(self.round, card1 + card2)
def consecutive_round(self, round, roundPoints):
nextCard = newDeck.deal_card()
print "\nRound %d" % self.round
print "You've been dealt a %s" % str(nextCard)
if roundPoints + newDeck.face_card_rank(nextCard) > 21:
print "You went over 21, you've lost this round! \nyour total was %d" % (roundPoints + newDeck.face_card_rank(nextCard))
self.score -= 21
playAgain = raw_input("Another round? (Y/N): ")
if playAgain.upper() == "Y":
self.round += 1
return self.first_round(newDeck.deal_card(), newDeck.deal_card())
else:
print "Thanks for playing, your final score is %d" % self.score
elif roundPoints + newDeck.face_card_rank(nextCard) == 21:
print "Congrats, you have 21 this round! Your score remains the same!"
playAgain = raw_input("Another round? (Y/N): ")
if playAgain.upper() == "Y":
self.round += 1
return self.first_round(newDeck.deal_card(), newDeck.deal_card())
else:
print "Thanks for playing, your final score is %d" % self.score
elif roundPoints + newDeck.face_card_rank(nextCard) < 21:
return self.decision_making(self.round, roundPoints + newDeck.face_card_rank(nextCard))
else:
print "Error occured, shutting down program."
def decision_making(self, round, roundPoints):
print "The combined sum of your cards is %d, do you want to hit or stay?" % roundPoints
choice = raw_input("Type \"Hit\" or \"Stay\": ")
if choice.upper() == "HIT":
return self.consecutive_round(round, roundPoints)
elif choice.upper() == "STAY":
self.score -= 21 - roundPoints
print "You had %d this round" % roundPoints
if self.round == 5:
print "Thanks for playing %s, your total score is %d" % (self.playerName, self.score)
else:
playAgain = raw_input("Another round? (Y/N): ")
if playAgain.upper() == "Y":
self.round += 1
return self.first_round(newDeck.deal_card(), newDeck.deal_card())
else:
print "Thanks for playing, your final score is %d" % self.score
if __name__ == "__main__":
newDeck = Deck(create_deck())
###_____________________________________start game interface###
print "Hello, welcome to BlackJack"
playerName = raw_input("What is your name?: ")
### Testing in progress..
#if playerName == "Test":
# add_high_score(playerName, 84)
#else:
# True
###
newGame = Game(playerName, score, round)
newGame.first_round(newDeck.deal_card(), newDeck.deal_card())
@montycheese
Copy link
Author

Need help w/ check_high_score() method and add_high_score() method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment