CPSC120
Fundamentals of Computer Science

Activity 29

Classes

Card

Create a class that represents a playing card. The class should have attributes for the rank and suit of the card. The rank attribute should be an int in the range 1 to 13. The suit attribute should be an int in the range 1 to 4. The initialization function should have parameters for the rank and suit of a card.

Test Cases

import test

class Card:
    # Put your code here

def main() -> None:
    a_card: Card

    a_card = Card(1, 2)
    test.equal(a_card.rank, 1)
    test.equal(a_card.suit, 2)

    a_card = Card(13, 4)
    test.equal(a_card.rank, 13)
    test.equal(a_card.suit, 4)

    # Put more test cases here
    return None

main()

Card To Str

Create the Python function card_to_str(a_card: Card) -> str that converts a card object to a string. The parameter a_card is Card object with the attributes rank and suit, both ints. The function should return a string that represents a playing card. The function should convert the rank and suit attributes to the following strings:

Rank Int Rank Str Suit Int Suit Str
1 A 1
2 2 2
3 3 3
4 4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 J
12 Q
13 K

You can copy the suit characters from the test cases for use in your card_to_str function.

Test Cases

import test

class Card:
 # Put your Card __init__ method here

def card_to_str(a_card: Card) -> str:
 # Put your code here

def main() -> None:
    a_card: Card

    a_card = Card(1, 1)
    test.equal(card_to_str(a_card), "A♠")

    a_card = Card(10, 2)
    test.equal(card_to_str(a_card), "10♥")

    a_card = Card(11, 3)
    test.equal(card_to_str(a_card), "J♦")

    a_card = Card(12, 4)
    test.equal(card_to_str(a_card), "Q♣")

     # Put more test cases here
    return None

main()

Hint

  • Create two string variables, one for the rank and one for the suit of the a_card parameter.

  • Set the rank string variable according to a_card.rank. Use an if-elif chain to test a_card.rank for ranks 1, 13, 12, and 11, and set the rank string to the literal “A”, “K”, “Q”, or “J”. All other ranks can be handled with a single conditional statement by converting a_card.rank to a string using the str function.

  • Set the suit string variable to the appropriate string. Use an if-elif chain to test a_card.suit for suit integers 1, 2, 3, and 4, and set the suit string to the literal “♠”, “♥”, “♦”, or “♣”.

  • Return the concatenation of the rank string and the suit string.

War

What is good for? Absolutely nothing. In our house we don’t play the card game War; we play Fun. Because playing war sounds doesn’t sound like fun.

Details

Create a Python program that plays a single hand of the card game War. The program should do the following:

  1. Deal two cards, one for the player and one for the computer.

  2. Print the the two cards.

  3. Print who won.

Example

Your Card: K♦
Computer’s Card: 9♥
You Win!

Hint

  • Create a Card object with a random rank and suit by using the random.randrange function to create random numbers for the rank and suit attributes of a card. The rank should be between 1 and 13 and the suit should be between 1 and 4. Initialize the card objects by calling the Card function with the random rank and suit integers.

  • Print the card objects by using the card_to_str function you previously created.

  • Use the rank of the card objects to determine who won the game. Use an if statement to print who the winner is. Don’t forget to handle whether there is tie.

Challenge

In the game War, a tie is handled by dealing three additional cards per user and using the last delt card to determine the winner. This repeats until the tie is settled. Add this to your program so that they game never ends in a tie.