===============================第一版=======================================
# Mini-project #6 - Blackjack
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")
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")
# initialize some useful global variables
in_play = False
outcome = ""
score = 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
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 draw(self, canvas, pos):
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)
# define hand class
class Hand:
def __init__(self):
self.hand_card = []
def __str__(self):
# return a string representation of a hand
ret_str = "Hand contains"
for card in self.hand_card:
ret_str += " " + card.get_suit() + card.get_rank()
return ret_str
def add_card(self, card):
self.hand_card.append(card)
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
a = False
hand_value = 0
for i in self.hand_card:
hand_value = hand_value + VALUES[i.get_rank()]
if i.get_rank() == 'A':
a = True
if a and hand_value + 10 <= 21:
return hand_value + 10
else:
return hand_value
def draw(self, canvas, pos):
i = 0
for card in self.hand_card:
card.draw(canvas, (pos[0] + i*1.35*CARD_SIZE[0], pos[1]))
i += 1
# define deck class
class Deck:
def __init__(self):
# create a Deck object
self.deck_card = [Card(suit, rank) for suit in SUITS for rank in RANKS]
def shuffle(self):
# shuffle the deck
random.shuffle(self.deck_card)
def deal_card(self):
# deal a card object from the deck
return self.deck_card.pop(0)
def __str__(self):
# return a string representing the deck
ret_str = "Deck contains"
for card in self.deck_card:
ret_str += " " + card.get_suit() + card.get_rank()
return ret_str
#define event handlers for buttons
def deal():
global outcome, in_play, dealer, player, deck, tips, score
dealer = Hand()
player = Hand()
deck = Deck()
deck.shuffle()
outcome = ""
tips = "Hit or Stand?"
in_play = True
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
in_play = True
def hit():
global player, deck, in_play, outcome, tips, score
# if the hand is in play, hit the player
if in_play is True and player.get_value() <= 21:
player.add_card(deck.deal_card())
# if busted, assign a message to outcome, update in_play and score
if player.get_value() > 21:
outcome = "You went bust and lose."
tips = "New deal?"
in_play = False
score -= 1
# if busted, assign a message to outcome, update in_play and score
def stand():
global in_play, dealer, player, outcome, tips, score
# if hand is in play, repeatedly hit dealer until his hand has value 17 or more
if in_play is True:
while dealer.get_value() < 17:
dealer.add_card(deck.deal_card())
dealer_value = dealer.get_value()
# assign a message to outcome, update in_play and score
if dealer_value > 21:
outcome = "Dealer went bust. You win!"
score += 1
else:
player_value = player.get_value()
if player_value <= dealer_value:
outcome = "You lose!"
score -= 1
else:
outcome = "You win!"
score += 1
tips = "New deal?"
in_play = False
# draw handler
def draw(canvas):
# test to make sure that card.draw works, replace with your code below
canvas.draw_text("Blackjack", (100,40), 45, "Black")
canvas.draw_text("Score " + str(score), (450, 50), 25, "Black")
canvas.draw_text("Dealer", (60, 90), 30, "Maroon")
canvas.draw_text(outcome, (200, 90), 30, "Black")
dealer.draw(canvas, (60, 100))
if in_play is True:
card_loc = (CARD_BACK_CENTER[0] + CARD_BACK_SIZE[0], CARD_BACK_CENTER[1])
canvas.draw_image(card_back, card_loc, CARD_BACK_SIZE, [60 + CARD_BACK_CENTER[0], 100 + CARD_BACK_CENTER[1]], CARD_BACK_SIZE)
canvas.draw_text("Player", (60, 350), 30, "Navy")
canvas.draw_text(tips, (200, 350), 30, "Black")
player.draw(canvas, (60, 370))
# initialization frame
frame = simplegui.create_frame("Blackjack", 600, 600)
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
========================================第二版=======================================================
改变计分方法
# Mini-project #6 - Blackjack
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")
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")
# initialize some useful global variables
in_play = False
outcome = ""
money = 100
warning = ""
# 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
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 draw(self, canvas, pos):
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)
# define hand class
class Hand:
def __init__(self, money):
self.hand_card = []
self.money = money
def __str__(self):
# return a string representation of a hand
ret_str = "Hand contains"
for card in self.hand_card:
ret_str += " " + card.get_suit() + card.get_rank()
return ret_str
def add_card(self, card):
self.hand_card.append(card)
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
a = False
hand_value = 0
for i in self.hand_card:
hand_value = hand_value + VALUES[i.get_rank()]
if i.get_rank() == 'A':
a = True
if a and hand_value + 10 <= 21:
return hand_value + 10
else:
return hand_value
def draw(self, canvas, pos):
i = 0
for card in self.hand_card:
card.draw(canvas, (pos[0] + i*1.35*CARD_SIZE[0], pos[1]))
i += 1
if i >= 5:
card.draw(canvas, (pos[0] + (i % 5)*1.35 * CARD_SIZE[0],
pos[1] + 1.2*CARD_SIZE[1]))
def get_money(self):
return self.money
# define deck class
class Deck:
def __init__(self):
# create a Deck object
self.deck_card = [Card(suit, rank) for suit in SUITS for rank in RANKS]
def shuffle(self):
# shuffle the deck
random.shuffle(self.deck_card)
def deal_card(self):
# deal a card object from the deck
return self.deck_card.pop(0)
def __str__(self):
# return a string representing the deck
ret_str = "Deck contains"
for card in self.deck_card:
ret_str += " " + card.get_suit() + card.get_rank()
return ret_str
#define event handlers for buttons
def deal():
global outcome, in_play, dealer, player, deck, tips, warning
p_money = player.get_money()
d_money = dealer.get_money()
if p_money <= 0 or d_money <= 0:
tips = "No Money! Game Over!"
warning = "Gambling is harmful!"
return
dealer = Hand(d_money)
player = Hand(p_money)
deck = Deck()
deck.shuffle()
outcome = ""
tips = "Hit or Stand?"
in_play = True
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
in_play = True
def hit():
global player, deck, in_play, outcome, tips
# if the hand is in play, hit the player
if in_play is True and player.get_value() <= 21:
player.add_card(deck.deal_card())
# if busted, assign a message to outcome, update in_play and score
if player.get_value() > 21:
outcome = "You went bust and lose."
tips = "New deal?"
in_play = False
player.money -= 20
dealer.money += 20
# if busted, assign a message to outcome, update in_play and score
def stand():
global in_play, dealer, player, outcome, tips
# if hand is in play, repeatedly hit dealer until his hand has value 17 or more
if in_play is True:
while dealer.get_value() < 17:
dealer.add_card(deck.deal_card())
dealer_value = dealer.get_value()
# assign a message to outcome, update in_play and score
if dealer_value > 21:
outcome = "Dealer went bust. You win!"
player.money += 20
dealer.money -= 20
else:
player_value = player.get_value()
if player_value <= dealer_value:
outcome = "You lose!"
player.money -= 20
dealer.money += 20
else:
outcome = "You win!"
player.money += 20
dealer.money -= 20
tips = "New deal?"
in_play = False
# draw handler
def draw(canvas):
# test to make sure that card.draw works, replace with your code below
canvas.draw_text("Blackjack", (100,40), 45, "Black")
canvas.draw_text("Player:" + str(player.get_money()), (300, 250), 20, "Black")
canvas.draw_text("Dealer:" + "A lot of money", (300, 200), 20, "Black")
canvas.draw_text("Dealer", (60, 90), 30, "Maroon")
canvas.draw_text(outcome, (200, 90), 30, "Black")
dealer.draw(canvas, (60, 100))
if in_play is True:
card_loc = (CARD_BACK_CENTER[0] + CARD_BACK_SIZE[0], CARD_BACK_CENTER[1])
canvas.draw_image(card_back, card_loc, CARD_BACK_SIZE, [60 + CARD_BACK_CENTER[0], 100 + CARD_BACK_CENTER[1]], CARD_BACK_SIZE)
canvas.draw_text("Player", (60, 350), 30, "Navy")
canvas.draw_text(tips, (200, 350), 30, "Black")
canvas.draw_text(warning, (20, 300), 55, "Red")
player.draw(canvas, (60, 370))
# initialization frame
frame = simplegui.create_frame("Blackjack", 600, 600)
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
dealer = Hand(10000)
player = Hand(100)
deal()
frame.start()
# remember to review the gradic rubric