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):
        "Constructs a new Card object with the given rank (an int) and suit (a string)."
        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 rummyValue(self):
        "Returns the card's rummy value"
        if self.getRank() < 10: # handles number cards
            return 5
        elif self.rank == 14: # handles Ace
            return 15
        else: # handles 10 and face cards
            return 10
        
def main():
    c1 = Card(14, "spades")
    print(c1)
    c2 = Card(13, "hearts")
    print(c2)
    c3 = Card(2, "diamonds")
    print(c3)
    
    # testing rummyValue() method
    rummyValue = c1.rummyValue()
    print("Rummy value is", end = " ")
    if rummyValue == 15:
        print("good", end = " ")
    else:
        print("bad", end = " ")
    print("for", c1)
    
    testCases = [c1, c2, c3]
    expected = [15, 10, 5]

    for pos in range(len(testCases)):
        testCase = testCases[pos] # what data type is this?
        actualVal = testCase.rummyValue()
        if actualVal == expected[pos]:
            print("Success for rummyVal on", testCase, "-->", actualVal)
    
    #myString = c3.__str__()
    #myString2 = str(c3)
    
    #print(myString)
    #print(myString2)
    
# Since I am probably going to import this script into another script,
# I only want to call main() when it's *not* imported 
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):
        "Constructs a new Card object with the given rank (an int) and suit (a string)."
        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 getRummyValue(self):
        "Returns the value of this card in the game of Rummy"
        if self.rank < 10:
            return 5
        if self.rank <=13:
            return 10
        return 15
        
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)

# Since I am probably going to import this script into another script,
# I only want to call main() when it's *not* imported
if __name__ == '__main__':
    main()

deck.py 3/4

[
top][prev][next]
# Implementation of a deck of cards.
# by CSCI 111, 03/19/2012

from card import *

class Deck:
    """ A class to represent a deck of playing cards."""
    
    def __init__(self):
        """Creates a new Deck object, filled with one of each
        unique card."""
        
        self.listOfCards = []
        for suit in ["clubs","hearts","diamonds","spades"]:
            for rank in range(2,15):
                self.listOfCards.append(Card(rank, suit))

    def __str__(self):
        """Returns a string representing the cards that are
        in the deck."""
        
        deckRep= ""
        for c in self.listOfCards:
            deckRep += str(c) + "\n"
        return deckRep

    def numRemaining(self):
        """Returns the number of cards left in the deck."""
        return len(self.listOfCards)
        
def main():
    d = Deck()
    print(d)
    print("The number of cards remaining in the deck is", d.numRemaining())
    
# Since I am probably going to import this script into another script,
# I only want to call main() when it's *not* imported
if __name__ == "__main__":
    main()

war.py 4/4

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

from card import *
from random import *

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

    print("Player 1 draws a", player1Card)
    print("Player 2 draws a", player2Card)
    
    if player1Card.getRank() > player2Card.getRank():
        print("Player 1 wins", player2Card)
    elif player1Card.getRank() < player2Card.getRank():
        print("Player 2 wins", player1Card)
    else:
        print("Tie! Draw 4 cards each")
    

def genRandomCard():
    """ Returns a randomly generated Card object.
    Assumes an infinite number of decks of cards available."""
    
    suits = ["hearts", "diamonds", "clubs", "spades"]
    
    randRank = randint(2, 14) # generate a random rank
    randSuit = suits[randint(0, 3)]  # 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.