Computer Programming 150

(April 17, 2008)

 

Lab14a.py – A Date object: Note blue code denotes alternate approach

 

#

#  Name:

#  File: Lab14a.py

#  Date: April 17, 2008

#

#  Desc: Using a Date object

 

import string

 

class Date:

 

    def __init__(self, month, day, year):

        "  constructor - accepts month , day and year"   

        self.month = int(month)

        self.day = int(day)

        self.year = int(year)

 

    def getMonth(self):

        return self.month

 

    def getDay(self):

        return self.day

 

    def getYear(self):

        return self.year

 

    def isLeapYear(self):

        if ((self.year % 4) == 0 and (self.year % 100) != 0 or

            (self.year % 400) == 0):

            return True

        else:

            return False

 

    def dayOfYear(self):

        daysB4first = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]

        dayNumber = daysB4first[self.month - 1] + self.day

        if (self.month >2) and self.isLeapYear():

            dayNumber = dayNumber + 1

        return dayNumber   

 

 

    def __str__(self):

        months = ["January", "February", "March", "April", "May", "June",

                  "July", "August", "September", "October", "November",

                  "December"]

        return months[self.month-1] + " " + str(self.day) + \

               ", " + str(self.year)

 

def main():

 

    # Display Title

   

    print "\nDate Conversion"

 

    #  ask-before-iterating loop

 

    again = "y"

    while again == "y":

 

        # get the date

 

        dateStr = raw_input("\nEnter a date (mm/dd/yyyy): ")

 

        # split into string components

 

        monthStr, dayStr, yearStr = string.split(dateStr, "/")

 

        #  create Date object

       

        date = Date(monthStr, dayStr, yearStr)

       

        # output result in month day, year format

 

        print "The converted date is:", date

 

        #  list of number of days before 1st of the month

       

#       daysB4first = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]

 

        # look up number of days before 1st of month and add day

 

#       dayNumber = daysB4first[date.getMonth()-1] + date.getDay()

 

        # check for leap year

 

#       if ( date.getMonth() > 2) and (date.isLeapYear()):

#           dayNumber = dayNumber + 1

 

        #  display results

 

#       print "This is day number", dayNumber,"of the year"

 

        print "This is day number", date.dayOfYear(), "of the year"

       

        again = raw_input("\nAgain? (y/n): ")

main()  

 

Lab14b.py – Five Card Draw in Graphics

 

#

#   Name:

#   File: Lab14b.py

#   Date: April 17, 2008

#

#   Desc: Five Card Draw With Graphics

 

from random import randrange

from graphics import *

from button import *

 

class Card:

 

    def __init__(self, rank, suit):

        "  store rank as int between 1 - 13; suit as 'c', 'd', 'h', or 's' "

        self.rank = rank

        self.suit = suit

 

    def getRank(self):

        return self.rank

 

    def getSuit(self):

        return self.suit

 

    def BJValue(self):

        " return blackjack value of card"

        if slef.rank >= 10:

            return 10

        else:

            return self.rank

 

    def getImage(self):

        " returns path to image file"

        suit = str(self.suit)

        if (self.rank <= 10):

            rank = str(self.rank)

        elif self.rank == 11:

            rank = "j"

        elif self.rank == 12:

            rank = "q"

        elif self.rank == 13:

            rank = "k"

        return "H:\Comp150S08\Cards\\" + suit + rank + ".gif"

         

    def __str__(self):

        rankList = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", \

                    "Eight", "Nine", "Ten", "Jack", "Queen", "King"]

        if self.suit == "c":

            return rankList[self.rank-1]+" of Clubs"

        elif self.suit == "d":

            return rankList[self.rank-1]+" of Diamonds"

        if self.suit == "h":

            return rankList[self.rank-1]+" of Hearts"

        elif self.suit == "s":

            return rankList[self.rank-1]+" of Spades"

 

def isSameCard(c0, c1):

    #

    #  Returns True if rank and suits are same

    #

    if ((c0.getRank() == c1.getRank()) and

        (c0.getSuit() == c1.getSuit())):

        return True

    else:

        return False

 

def inList(c, list):

    #

    #  Returns True if card c is in list

    #

    i = 0;

    while (i < len(list) and not isSameCard(c, list[i])):

 

#     alternate search loop

       

#   while ((i < len(list)) and (c.getRank() != list[i].getRank()) and

#          (c.getSuit() != list[i].getRank())):

 

        i = i+1

    if i < len(list):

        return True

    else:

        return False

   

 

def main():

 

#   print "A Five Card Hand"

 

    windowTitle  = "A Five Card Hand"

    screenWidth  = 640

    screenHeight = 480

 

    #

    #  Open a graphics window object

    #

 

    win = GraphWin(windowTitle, screenWidth, screenHeight)

    win.setBackground("darkgreen")

    win.setCoords(0, 0, screenWidth, screenHeight)

 

    #  create and activate two buttons

 

    drawButton = button(win, Point(295, 30), 40, 20, "Draw")

    quitButton = button(win, Point(345, 30), 40, 20, "Quit")

    drawButton.activate()

    quitButton.activate()

 

    suitList = ["c", "d", "h","s"]   # list of suits

 

    #  event handling loop

   

    while (True):

        p1 = win.getMouse()

 

        #  draw a five card hand

 

        if drawButton.clicked(p1):

            hand = []

            while len(hand) < 5:

 

                # draw card

               

                c = Card(randrange(1, 14), suitList[randrange(0,4)])

 

                # check for duplicates; append if not duplicated

               

                if not inList(c, hand):       

                    hand.append(c)

 

            #  display hand

 

            for i in range(5): 

                Image(Point(120 + i*100, 240), hand[i].getImage()).draw(win)

 

        #  quit program

               

        elif quitButton.clicked(p1):

            break

 

    win.close()

    

main()