Contents

  1. card2.py
  2. card.py
  3. deck.py
  4. war.py

card2.py 1/4

[
top][prev][next]
class Card:
    """
    A class to represent a standard playing card. The ranks are ints:
    2-10 for numbered cards, 11=Jack, 12=Queen, 13=King, 14=Ace.
    The suits are strings: 'clubs', 'spades', 'hearts', 'diamonds'.
    """
    
    def __init__(self, rank, suit):
        "Constructor for class Card takes int rank and string suit."
        self.rank = rank
        self.suit = suit

    def __str__(self):
        "Returns a string describing the card as 'rank of suit'."
        result = ""
        if self.rank == 11:
            result += "Jack"
        elif self.rank == 12:
            result += "Queen"
        elif self.rank == 13:
            result += "King"
        elif self.rank == 14:
            result += "Ace"
        else:
            result += str(self.rank)
        result += " of " + self.suit
        return result

    def getRank(self):
        "Returns rank."	
        return self.rank

    def getSuit(self):
        "Returns suit."
        return self.suit
        
    def blackJackValue(self):
        """ Returns the value of this card in the game of Black Jack.
        Ace = 1; Face card = 10; Others = their rank"""
        # if this card is a Jack, Queen, or King
        if self.rank >= 11 and self.rank < 14:
            value = 10
            # could also do return 10
        # if this card is an Ace
        elif self.rank == 14:
            value = 1
        else:
            value = self.rank
            
        return value
        
        
    def rummyValue(self):
        """ Returns the value of this card in the game of Rummy.
        Ace = 15; Face card and 10s = 10; Others = 5"""

        # if this card is an Ace...
        if self.rank == 14:
            return 15
        # 10s and Face cards
        elif self.rank >= 10:
            return 10
        else:
            return 5

def main():
    c1 = Card(14, "spades")
    print c1
    c2 = Card(13, "hearts")
    print c2
    c3 = Card(2, "diamonds")
    print c3
    
    #myString = c3.__str__()
    #myString2 = str(c3)
    
    #print myString
    #print myString2
    
    print "c1's blackJackValue:", c1.blackJackValue()
    print c2.blackJackValue()
    print c3.blackJackValue()
    
    total = c1.blackJackValue() + c2.blackJackValue() + c3.blackJackValue() 
    print "Did you bust?", total
    
if __name__ == '__main__':
    main()

card.py 2/4

[
top][prev][next]
class Card:
    """
    A class to represent a standard playing card. The ranks are ints:
    2-10 for numbered cards, 11=Jack, 12=Queen, 13=King, 14=Ace.
    The suits are strings: 'clubs', 'spades', 'hearts', 'diamonds'.
    """
    
    def __init__(self, rank, suit):
        "Constructor for class Card takes int rank and string suit."
        self.rank = rank
        self.suit = suit

    def __str__(self):
        "Returns a string describing the card as 'rank of suit'."
        result = ""
        if self.rank == 11:
            result += "Jack"
        elif self.rank == 12:
            result += "Queen"
        elif self.rank == 13:
            result += "King"
        elif self.rank == 14:
            result += "Ace"
        else:
            result += str(self.rank)
        result += " of " + self.suit
        return result
        
    def rummyValue(self):
        """ Returns the value of this card in the game of Rummy.
        Ace = 15; Face card and 10s = 10; Others = 5"""

        # if this card is an Ace...
        if self.rank == 14:
            return 15
        # 10s and Face cards
        elif self.rank >= 10:
            return 10
        else:
            return 5

    def getRank(self):
        "Returns rank."	
        return self.rank

    def getSuit(self):
        "Returns suit."
        return self.suit
        
def main():
    c1 = Card(14, "spades")
    print c1
    c2 = Card(13, "hearts")
    print c2
    c3 = Card(2, "diamonds")
    print c3
    
    #myString = c3.__str__()
    #myString2 = str(c3)
    
    #print myString
    #print myString2
    
    print "c1's rummy value:", c1.rummyValue()
    print "c1's rummy value:", c2.rummyValue()
    print "c1's rummy value:", c3.rummyValue()
    
    total = c1.rummyValue() + c2.rummyValue() + c3.rummyValue() 
    print "Your score: ", total
    
if __name__ == '__main__':
    main()

deck.py 3/4

[
top][prev][next]
from card import *
from random import shuffle

class Deck:
    """ A class to represent a deck of playing cards."""
    
    def __init__(self):
        # the instance variable that represents the cards
        self.cards = []
        
        # executes 4 times (once for each suit)
        for suit in ["clubs","hearts","diamonds","spades"]:
            # executes 13 times (once for each rank)
            for rank in range(2,15):
                # the body of this loop gets executed a total of 52
                # times
                self.cards.append(Card(rank, suit))

    def __str__(self):
        result = ""
        for c in self.cards:
            # Either of the following two lines do the same thing,
            # have the same result:
        
            #result += c.__str__() + "\n"
            result += str(c) + "\n"
        return result

    # TODO: Complete remaining functionality

def main():
    d = Deck()
    print d

main()

war.py 4/4

[
top][prev][next]
# Simple implementation of the game of War
# There are some issues in simulation's accuracy.

from card import *
from random import *

def main():
    player1Card = genRandomCard()
    player2Card = genRandomCard()

    print "Player 1 draws a", player1Card
    print "Player 2 draws a", player2Card
    
    # TODO: determine which player wins this "round"
    if player1Card.getRank() > player2Card.getRank():
        print "PLayer 1 wins, takes", player2Card
    elif player2Card.getRank() > player1Card.getRank():
        print "Player 2 wins, takes", player1Card
    else:
        print "WAR!!!"
        
        # generate 3 more cards for each player (burned)
        
        # generate 4th card for each player, compare them --> winner
        
        # winner takes all
        


    
# Returns a randomly generated card.
# Assumes an infinite number of decks of cards available
def genRandomCard():
    suits = ["hearts", "diamonds", "clubs", "spades"]
    
    randRank = randint(2, 14) # generate a random rank
    randSuit = suits[randint(0, 0)]  # generate a random suit
    
    # create a new Card object from randomly generated rank and suit
    randomCard = Card( randRank, randSuit )
    
    return randomCard
    
main()

Generated by GNU enscript 1.6.4.