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'.
    
    Difference with Card class in card.py: Includes implementation of
    rummyValue method
    """
    
    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):
        """This method returns the rummy point value of this card."""
        pointValue = 5
        if self.rank == 14:
            pointValue = 15
        elif self.rank >= 10:
            pointValue = 10
        return pointValue
        
def main():
    c1 = Card(14, "spades")
    print(c1)
    c2 = Card(13, "hearts")
    print(c2)
    c3 = Card(2, "diamonds")
    print(c3)
    
    # testing rummyValue() method
    
    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", end=" ")
        else:
            print("Error", end=" ") 
        print("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 this script is *not* imported 
if __name__ == '__main__':
    main()

card.py 2/4

[
top][prev][next]

"The possible suits for a Card object"
SUITS=["clubs","hearts","diamonds","spades"]

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 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

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 = []
        # as per Max's suggestion that SUITS be defined elsewhere
        for suit in SUITS:  #SUITS is defined in card module
            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 and CSCI111

from card import *
from random import *

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

    print("Player 1 draws a", player1Card)
    print("Player 2 draws a", player2Card)
    
    player1Rank = player1Card.getRank()
    player2Rank = player2Card.getRank()
    
    if player1Rank > player2Rank:
        print("Player 1 wins")
    elif player2Rank > player1Rank:
        print("Player 2 wins")
    else:
        print("You're going to war!!")
    
    

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.