Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topics in Python Blackjack & TKinter

Similar presentations

Presentation on theme: "Topics in Python Blackjack & TKinter"— Presentation transcript:

1 Topics in Python Blackjack & TKinter
Professor Frank J. Rinaldo Creation Core - office 401

2 BlackJack Cards module
class Card(object): “”” A Playing Card “”” RANKS = [“A”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”] SUITS = [“c”, “d”, “h”, “s”] def __init__(self, rank, suit, face_up = True): self.rank = rank self.suit = suit self.is_face_up = face_up def __str__(self): if self.is_face_up: rep = self.rank + self.suit else: rep = “XX” return rep def flip(self) self.is_face_up = not self.is_face_up

3 BlackJack Cards module continued
class hand(object): def __init__(self): = [] def __str__(self): if rep = “” for card in rep += str(card) + “\t” else: rep = “<empty>” return rep def clear(self): def add(self, card): def give(self, card, other_hand): other_hand.add(card)

4 BlackJack Cards module continued
class Deck(Hand): “”” A deck of playing cards “”” def populate(self): for suit in Card.SUITS: for rank in Cards.RANKS: self.add(Card(rank, suit)) def shuffle(self): import random random.shuffle( def deal(self, hands, per_hand = 1) for rounds in range(per_hand): for hand in hands: if top_card =[0] self.give(top_card, hand) else: print “Can’t continue deal. Out of cards.”

5 BlackJack Cards module continued
if __name__ == “__main__”: print “This is a module with classes for playing cards.” raw_input(“\n\nPress the enter key to exit.”)

6 BlackJack Class Design
BJ_Card derived from cards.Card BJ_Deck derived from cards.Deck BJ_Hand derived from cards.Hand BJ_Player derived from BJ_Hand BJ_Dealer derived from BJ_Hand BJ_Game derived from object

7 BlackJack Pseudocode Deal each player and dealer initial two cards
For each player While the player asks for a hit and player not busted Deal the player an additional card If there are no players still playing Show the dealer’s two cards Otherwise While the dealer must hit and dealer is not busted Deal the dealer an additional card If the dealer is busted For each player who is still playing The player wins If the player’s total > dealer’s total Otherwise, if the player’s total < dealer’s total The player loses The player pushes

8 BlackJack Game # Blackjack
# From 1 to 7 players compete against the dealer import cards, games class BJ_Card(cards.Card): “”” A Blackjack Card. “”” ACE_VALUE = 1 def get_value(self): if self.is_face_up: value = BJ_Card.RANKS.index(self.rank) + 1 if value > 10: value = 10 else: value = None return value value = property(get_value)

9 BlackJack Game class BJ_Deck(cards.deck): “”” A Blackjack Hand “””
def populate(self): for suit in BJ_Card.SUITS for rank in BJ_Card.RANKS:, suit))

10 BlackJack Game class BJ_Hand(cards.Hand): “”” A Blackjack Hand. “””
def __init__(self, name): super(BJ_hand, self).__init__() = name def __str__(self): rep = + “:\t” + super(BJ_Hand, self.__STR__() if rep = “(“ + str( + “)” return rep def is_busted(self): return > 21

11 BlackJack Game def get_total(self):
# if a card in hand has value None, total is None for card in if not card.value: return None # add up card values, treat each Ace as 1 total = 0 total += card.value # determine if hand contains an Ace contains_ace = False if card.value == BJ_Card.ACE_VALUE: contains_ace = True # if hand contains Ace & total is low enough, treat Ace as 11 if contains_ace and total <= 11: total += 10 return total total = property(get_total)

12 BlackJack Game class BJ_Player(BJ_Hand): “”” A Blackjack Player “””
def is_hitting(self): response = games.ask_yes_no(“\n” + + “, do you want a hit? (Y/N): “) return response def bust(self): print, “busts.” self.lose def lose(self): print, “loses.” def win(self): print, “wins.” def push(self): print, “pushes.”

13 BlackJack Game class BJ_Dealer(BJ_Hand): “”” A Blackjack Dealer “””
def is_hitting(self): return self. total < 17 def bust(self): print, “busts” def flip_first_card(self): first_card =[0] first_card.flip()

14 BlackJack Game class BJ_Game(object): “”” A Blackjack Game “””
def __init__(self, names): self.players = [] for name in names: player = BJ_Player(name) self.player.append(player) = BJ_Dealer(“Dealer”) self.deck = BJ_Deck() self.deck.populate() self.deck.shuffle()

15 BlackJack Game def get_still_playing(self): remaining = []
for player in self.players: if not player.is_busted: remaining.append(player) return remaining def __additional_cards(self, player): while not player.is_busted() and player.is_hitting(): print player if player.is_busted(): player.bust()

16 BlackJack Game def play(self): # deal initial 2 cards to everyone + [].per_hand = 2) for player in self.players: print player print self.player self.__additional_cards(player) if not self.still_playing: print else: self.__additional_cards(

17 BlackJack Game if
for player in self.still_playing: else: if > elif < player.lose() player.push() for player in self.players: player.clear()

18 BlackJack Game # Main Function (method) def main():
print “\t\tWelcome to Blackjack!\n” names = [] number = games.ask_number(“How many players? (1-7):”, low = 1, high = 8) for i in range(number): name = raw_input(Enter player name: “) names.append(name) print game = BJ_Game(names) again = None while again != “n”: again = games.ask_yes_no(“\nDo you want to play again?: “) # Main program main() raw_input(“\n\nPress the enter key to exit.”

19 Graphical User Interface Development
Learn to work with a GUI toolkit Create and fill frames (“window”) Create and use buttons Create and use text entries and text boxes Create and use check buttons Create and use radio buttons

20 GUI’s All of our development to date has used ‘text’ graphics & ‘text’ input & output Now we will learn how to start to use graphical user interfaces to make input to & output from the computer more interesting!

21 TK Tk is a popular (and standard) GUI Toolkit
It is supports many programming languages: Python Tcl Perl Ruby Etc… It runs on many operating systems: Most UNIX (and UNIX like) systems MS Windows Apple

22 TKinter TKinter is a standard Python interface to the Tk GUI toolkit
It consists of a number of modules (libraries) It allows us to create a user interface ‘window’ for input & output

23 Sample TKinter Program
# File: from Tkinter import * class App: def __init__(self, master): frame = Frame(master) frame.pack() self.button = Button(frame,text="QUIT", fg="red", command=frame.quit) self.button.pack(side=LEFT) self.hi_there = Button(frame, text="Hello",command=self.say_hi) self.hi_there.pack(side=LEFT) def say_hi(self): print "hi there, everyone!" # Main program root = Tk() app = App(root) root.mainloop() # NOTE: See “An Introduction to Tkinter ”: #

24 Sample TKinter Widget Classes
Button A simple button to execute a command Checkbutton Creates a button that can be ‘checked’ Entry A text entry field Frame A container widget Label Displays a text or an image Listbox Displays a list of alternatives Menu A menu pane for pulldown & popup menus Menubutton Used to implement a pulldown menu Message Display text message Radiobutton Multi-valued button Scrollbar Standard scrollbar Text Formatted text display

25 Homework Due week 11 Write a Python Program:
See problem #3 on page 289 of textbook. In other words, allow players to bet money. As an example, if they bet 1000 Yen and win then they get back 2000 Yen. Keep track of each player’s money and remove any player who runs out of money Include: Simple Specification Document Simple Pseudocode Python code Demonstrate program in class

Download ppt "Topics in Python Blackjack & TKinter"

Similar presentations

Ads by Google