Last active
January 1, 2018 19:42
-
-
Save soleshka/3f3d53a3ee30ce1ab16764920f9e3942 to your computer and use it in GitHub Desktop.
Simple Black jack game
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
| # Mini-project #6 - Blackjack | |
| """ | |
| This game implements the gameBlack Jack in its simple variant | |
| Date : 12/21/2017 | |
| Implementor : Oleg Greenberg | |
| """ | |
| import simplegui | |
| import random | |
| # load card sprite - 936x384 - source: jfitz.com | |
| CARD_SIZE = (72, 96) | |
| CARD_CENTER = (36, 48) | |
| card_images = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/cards_jfitz.png") | |
| WIDTH = 500 | |
| HEIGHT = 500 | |
| CARD_DRAW_SHIFT = CARD_SIZE[0]/3 | |
| SEPERATOR_COLOR = "White" | |
| SEP_WIDTH = 3 | |
| CARD_BACK_SIZE = (72, 96) | |
| CARD_BACK_CENTER = (36, 48) | |
| card_back = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/card_jfitz_back.png") | |
| background = simplegui.load_image("http://www.junkets.ca/gaming/blackjack.jpg") | |
| outcome_color = "Red" | |
| # initialize some useful global variables | |
| in_play = False | |
| outcome = "" | |
| next_todo = "Hit or Stand?" | |
| deck_l = [] | |
| score = [0,0] | |
| # define globals for cards | |
| SUITS = ('C', 'S', 'H', 'D') | |
| RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K') | |
| VALUES = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10} | |
| # define card class | |
| class Card: | |
| def __init__(self, suit, rank): | |
| if (suit in SUITS) and (rank in RANKS): | |
| self.suit = suit | |
| self.rank = rank | |
| self.exposed = True | |
| else: | |
| self.suit = None | |
| self.rank = None | |
| print "Invalid card: ", suit, rank | |
| def __str__(self): | |
| return self.suit + self.rank | |
| def get_suit(self): | |
| return self.suit | |
| def get_rank(self): | |
| return self.rank | |
| def expose(self): | |
| self.exposed = True | |
| def hide(self): | |
| self.exposed = False | |
| def is_exposed(self): | |
| return self.exposed | |
| def draw(self, canvas, pos): | |
| if self.exposed: | |
| card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank), | |
| CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit)) | |
| canvas.draw_image(card_images, card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1] + CARD_CENTER[1]], CARD_SIZE) | |
| else: | |
| card_loc = CARD_BACK_CENTER | |
| canvas.draw_image(card_back , card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1]+ CARD_CENTER[1]], CARD_SIZE) | |
| # define hand class | |
| class Hand: | |
| def __init__(self): | |
| self.hand_l = [] | |
| self.value = "" | |
| def __str__(self): | |
| return "Hand contains "+self.value # return a string representation of a hand | |
| def add_card(self, card): | |
| self.hand_l.append(card) # add a card object to a hand | |
| self.value += card.get_suit() + card.get_rank() + " " | |
| def get_value(self): | |
| # count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust | |
| # compute the value of the hand, see Blackjack video | |
| val = 0 | |
| has_ace = False | |
| for crd in self.hand_l: | |
| rank = crd.get_rank() | |
| val += VALUES[rank] | |
| if VALUES[rank] == 1: #this is an ACE | |
| has_ace = True | |
| if has_ace: | |
| if val + 10 <= 21: | |
| val += 10 | |
| return val | |
| def draw(self, canvas, pos): | |
| # draw a hand on the canvas, use the draw method for cards | |
| i = 0 | |
| for crd in self.hand_l: | |
| crd.draw(canvas,[pos[0]+i*CARD_DRAW_SHIFT,pos[1]]) | |
| i +=1 | |
| def expose_all_cards(self): | |
| for crd in self.hand_l: | |
| crd.expose() | |
| # define deck class | |
| class Deck: | |
| def __init__(self): | |
| # create a Deck object | |
| self.deck_l = [] | |
| for st in SUITS: | |
| for rnk in RANKS: | |
| crd = Card(st,rnk) | |
| self.deck_l.append(crd) | |
| def shuffle(self): | |
| # shuffle the deck | |
| # use random.shuffle() | |
| random.shuffle(self.deck_l) | |
| def deal_card(self): | |
| # deal a card object from the deck | |
| return self.deck_l.pop() | |
| def __str__(self): | |
| # return a string representing the deck | |
| val = "" | |
| for crd in self.deck_l: | |
| val += str(crd) + " " | |
| return "Deck contains " + ''.join() | |
| #define event handlers for buttons | |
| def deal(): | |
| global outcome, in_play , deck_l , next_todo, score,outcome_color,outcome | |
| global player1_h , player2_h,first_move | |
| # your code goes here | |
| if in_play: | |
| score[0] +=1 | |
| in_play = True | |
| next_todo = "Hit or Stand?" | |
| outcome = "" | |
| outcome_color = "Red" | |
| deck_l = Deck() | |
| deck_l.shuffle() | |
| player1_h = Hand() #computer | |
| player2_h = Hand() #user | |
| card = deck_l.deal_card() #this is required in order to draw a hidden card | |
| card.hide() | |
| player1_h.add_card(card) | |
| player2_h.add_card(deck_l.deal_card()) | |
| player1_h.add_card(deck_l.deal_card()) | |
| player2_h.add_card(deck_l.deal_card()) | |
| in_play = True | |
| def hit(): | |
| global player2_h,outcome,deck_l, in_play,next_todo,score | |
| # if the hand is in play, hit the player | |
| if in_play: | |
| player2_h.add_card(deck_l.deal_card()) | |
| # if busted, assign a message to outcome, update in_play and score | |
| if player2_h.get_value() > 21: | |
| player1_h.expose_all_cards() | |
| outcome = "Dealer Wins" | |
| score[0] +=1 | |
| outcome_color = "Red" | |
| next_todo = "New Deal?" | |
| in_play = False | |
| def stand(): | |
| global player1_h,outcome,next_todo, in_play , outcome_color | |
| # assign a message to outcome, update in_play and score | |
| if in_play: | |
| while player1_h.get_value() < 17: | |
| player1_h.add_card(deck_l.deal_card()) | |
| player1_h.expose_all_cards() | |
| in_play = False | |
| if player1_h.get_value() >= player2_h.get_value() and\ | |
| player1_h.get_value() <= 21: | |
| outcome = "Dealer Wins" | |
| outcome_color = "Red" | |
| score[0] +=1 | |
| else: | |
| outcome = "You Win" | |
| outcome_color = "Lime" | |
| score[1] +=1 | |
| next_todo = "New Deal?" | |
| # draw handler | |
| def draw(canvas): | |
| # test to make sure that card.draw works, replace with your code below | |
| #texts | |
| #Background Image | |
| canvas.draw_image(background,(380,238) , (760,477),(WIDTH/2,HEIGHT/2), (WIDTH,HEIGHT)) | |
| #Footer text and seperator line | |
| canvas.draw_text("Black Jack(C)2017", [0,HEIGHT-5],40,'Aqua') | |
| canvas.draw_line([0,HEIGHT-40],[WIDTH,HEIGHT-40],SEP_WIDTH,SEPERATOR_COLOR) | |
| #Dealer and Player | |
| canvas.draw_text("Dealer", [0,HEIGHT/6],30,"White") | |
| canvas.draw_line([0,HEIGHT/6+5],[WIDTH/2,HEIGHT/6+5],SEP_WIDTH,SEPERATOR_COLOR) | |
| canvas.draw_text("Player", [0,HEIGHT/2+20],30,"White") | |
| canvas.draw_line([0,HEIGHT/2+25],[WIDTH/2,HEIGHT/2+25],SEP_WIDTH,SEPERATOR_COLOR) | |
| #Horizontal Seperator | |
| canvas.draw_line([WIDTH/2,10],[WIDTH/2,HEIGHT-50],SEP_WIDTH,SEPERATOR_COLOR) | |
| #information table | |
| canvas.draw_polygon([[WIDTH/2+5,10],[WIDTH-5,10], [WIDTH-5,HEIGHT/2], [WIDTH/2+5,HEIGHT/2]], 2, 'White','Silver') | |
| canvas.draw_line([WIDTH/2+10,HEIGHT/6],[WIDTH-10,HEIGHT/6],SEP_WIDTH,SEPERATOR_COLOR) | |
| canvas.draw_line([WIDTH/2+10,HEIGHT/3],[WIDTH-10,HEIGHT/3],SEP_WIDTH,SEPERATOR_COLOR) | |
| #outcome & next_todo $ score messages | |
| canvas.draw_text(outcome, [WIDTH*7/12,HEIGHT*10/24],30,outcome_color) | |
| canvas.draw_text(next_todo, [WIDTH*7/12,HEIGHT*3/12],30,"Teal") | |
| score_str = "Dealer " + str(score[0])+ ":" + str(score[1]) + " Player" | |
| canvas.draw_text(score_str, [WIDTH*13/24,HEIGHT*1/8],30,"Teal") | |
| #Hands | |
| player1_h.draw(canvas,[WIDTH/7,HEIGHT/4]) | |
| player2_h.draw(canvas,[WIDTH/7,HEIGHT/1.6]) | |
| # initialization frame | |
| frame = simplegui.create_frame("Blackjack", WIDTH, HEIGHT) | |
| frame.set_canvas_background("Green") | |
| #create buttons and canvas callback | |
| frame.add_button("Deal", deal, 200) | |
| frame.add_button("Hit", hit, 200) | |
| frame.add_button("Stand", stand, 200) | |
| frame.set_draw_handler(draw) | |
| # get things rolling | |
| deal() | |
| frame.start() | |
| # remember to review the gradic rubric |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple implementation of Black jack game . The code is written in http://www.codeskulptor.org/ environment