Topics in Python Blackjack & TKinter

Slides:



Advertisements
Similar presentations
17 HTML, Scripting, and Interactivity Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and.
Advertisements

Computer Science 112 Fundamentals of Programming II User Interfaces
EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
An Introduction to Visual Basic Terms & Concepts.
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
MATLAB PROJECT DO YOU WANT TO PLAY A GAME?. DESIGN CRITERIA For this project, you are required to implement any card game using MATLAB  All selections.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
Computer Science 313 – Advanced Programming Topics.
Text Box controls are used when users are required to type some input (during program execution), or output is displayed on the form (known as the user-
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Sample Development: HiLo Game *Chapter 6: Console.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
CSC Proprietary and Confidential 1 General MSS Report Lesson 5 After completing this lesson, you will be able to open and run a MSS report.
Guide to Programming with Python
1 ADVANCED MICROSOFT WORD Lesson 15 – Creating Forms and Working with Web Documents Microsoft Office 2003: Advanced.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
CS 0004 –Lecture 8 Jan 24, 2011 Roxana Gheorghiu.
An Introduction to Visual Basic
Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Computer Science 111 Fundamentals of Programming I More Data Modeling.
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and check boxes using HTML Add a pull-down.
Guide to Programming with Python
Enhancing the Graphical User Interface Multiple Forms, Controls, and Menus.
PC204 Lecture 9 Conrad Huang Genentech Hall, N453A x
PYTHON GUI PROGRAMMING
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 20 Graphical User Interface (GUI)
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
SD1230 Unit 6 Desktop Applications. Course Objectives During this unit, we will cover the following course objectives: – Identify the characteristics.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
November 28, 2005ICP: Chapter 9: Object Oriented Programming 1 Introduction to Computer Programming Chapter 9: Object Oriented Programming Michael Scherger.
9-Nov-97Tri-Ada '971 TASH An Alternative to the Windows API TRI-Ada ‘97 Terry J. Westley
Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...
Creating visual interfaces in python
HTML Forms.
Coding Time This is a starter activity and should take about 10 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.Start a script session (Select.
Guide to Programming with Python
Week 8 : User-Defined Objects (Simple Blackjack Game)
CSE 403 Lecture 8 UML Sequence Diagrams Reading: UML Distilled, Ch. 4, M. Fowler slides created by Marty Stepp
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
Microsoft Office 2008 for Mac – Illustrated Unit D: Getting Started with Safari.
Introducing Scratch Learning resources for the implementation of the scenario
Topics Graphical User Interfaces Using the tkinter Module
Lecture 11.
UML UML Sequence Diagrams CSE 403
Fundamentals of Programming I More Data Modeling
An Introduction to Visual Basic
GUI Using Python.
Fundamentals of Python: From First Programs Through Data Structures
Tkinter Python User Interface
This Week: Tkinter for GUI Interfaces Some examples
Truth tables: Ways to organize results of Boolean expressions.
I210 review.
Truth tables: Ways to organize results of Boolean expressions.
Whatcha doin'? Aims: Begin to create GUI applications. Objectives:
Topics Graphical User Interfaces Using the tkinter Module
Truth tables: Ways to organize results of Boolean expressions.
Task 2 Implementation help
The University of Texas – Pan American
Presentation transcript:

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

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

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

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(self.cards) def deal(self, hands, per_hand = 1) for rounds in range(per_hand): for hand in hands: if self.cards: top_card = self.cards[0] self.give(top_card, hand) else: print “Can’t continue deal. Out of cards.”

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.”)

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

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

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)

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: self.cards.append(BJ_Card(rank, suit))

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

BlackJack Game def get_total(self): # if a card in hand has value None, total is None for card in self.cards: 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)

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

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

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) self.dealer = BJ_Dealer(“Dealer”) self.deck = BJ_Deck() self.deck.populate() self.deck.shuffle()

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(): self.deck.deal(player) print player if player.is_busted(): player.bust()

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

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

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”: game.play() again = games.ask_yes_no(“\nDo you want to play again?: “) # Main program main() raw_input(“\n\nPress the enter key to exit.”

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

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!

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

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

Sample TKinter Program # File: hello2.py 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 ”: # http://www.pythonware.com/library/tkinter/introduction/

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

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