Question: Python Project : The steps for the game are: 1. Shuffle the Deck of Cards and deal all the Cards evenly between the players. Each
Python Project :
The steps for the game are: 1. Shuffle the Deck of Cards and deal all the Cards evenly between the players. Each player has their Hand as a stack of Cards on the table. 2. Next, each player draws the top card from his Hand and face up onto the Pile in the center of the table. The Cards are compared. 3. What happens next depends on the rank of the Cards drawn: a. If the Cards have different ranks, a Coin flip occurs to determine if the buyer or the seller gets the Cards on the Pile. If the flip is a heads, the buyer takes the Pile. If the flip is tails, the seller takes the Pile. Go to Step 2. b. If the Cards have equal rank, a negotiation round begins. This involves each player placing up to four Cards on the Pile. The amount is based on the number of Cards in the Hand of the Player with the lowest number of Cards. The last set of Cards played are compared. If both players have Cards left in their Hand, then repeat this Step 3, else go to Step 4. Figure 1: Players in Negotiation 4. If a Player is playing his last Card and it matches the Card of the other Player, a Coin is flipped to see if he gets the Pile (Coin is tails), or the Player with the most Cards gets the Pile (Coin is heads) and wins the game. If Coin is tails, then go to Step 2. If heads is flipped, then go to Step 5. 5. Game Over. Here are the descriptions of each of the classes: Node +data: object +next: Node +__init__(item: object) +__str__(): string Figure 2: Node Class Diagram The Node class holds the Node of the LinkedList. The instance variables are public for simplicity. There are no getters or setters for the Node instance variables. The data is set by the constructor. The __str__ method returns a string version of the data object. Be sure that the data object type codes up a __str__ method. See the Lesson 5 slides for this code.
LinkedList #head: Node #tail: Node #size: int +__init__() +__len__(): int +is_empty(): boolean +add_first(data: object) +add_last(data: object) +remove_first(): object +__str__(): string Figure 3: LinkedList Class Diagram The LinkedList class is an implementation of the Singly LinkedList ADT, where both a head pointer and a tail pointer are used. The number of Nodes in the list is kept and updated as Nodes are added and removed. The instance variables of head, tail and size are protected, so that the child classes can get direct access. The constructor sets the head and tail pointers to None and the size to 0. There is a getter for the size utilizing the Python special __len__ method which allows you to use the len function when needing to know the number of Nodes in the LinkedList. There is an is_empty method for ease in coding the add and remove methods. The add_first method places a new Node at the beginning of the LinkedList and the add_last method places a new Node at the end of the LinkedList. The remove_first method removes the first Node from the LinkedList and returns the data stored in the Node. The __str__ method traverses the list, adding a string version of the data stored at every Node. Card(Comparable) -suit: int -rank: int +__init__( suit: int, rank: int) +get_rank(): int +get_suit(): int +compare(otherCard: Card): int +__str__(): string Figure 4: Card Class Diagram The Card class is immutable and represents a Comparable playing card. It inherits from the Comparable class, but the number of compares is not kept. Its instance variables are rank and suit. Both are private integers that are set through the constructor. With this game Aces are low and Kings are high, and only the rank is used for comparison. There are getters for the rank and suit. The compare method returns a positive number when the rank of self is greater than the rank of the otherCard, and it returns a negative number when the rank of self is less than the rank of the otherCard, and it returns zero, when the two Cards have the same rank. The __str__ method returns a string version of the Card as rank-suit, where "C" = Clubs, "D" = Diamonds, "H" = Heats, "S" = Spades, "A" = Aces, "J" = Jacks, "Q" = Queens, "K" = Kings, and the number Cards are returned as the string version of the number. For instance: A-H is the Ace of Hearts. The integer values of the suits and rank are given below: Suit: Clubs = 0 Diamonds = 1 Hearts = 2 Spades = 3 Rank: Ace = 1 2-10 = 2-10 Jack = 11 Queen = 12 King = 13 Deck(list) +__init__() +initialize(): +add_card(card: Card) +deal(): Card +shuffle(): Deck +__str__(): string Figure 5: Deck Class Diagram The Deck class hold 52 playing Cards. It inherits from the list class, so that you can use all the list methods to manipulate the Card objects. Be sure to call the list constructor, when creating the Deck object. The initialize method adds all 52 Cards to the Deck. It uses a nested for loop one for suit and one for rank. Use the add_card method to place the Card on the Deck list. The deal method removes and returns the first Card in the list. The shuffle method uses the shuffle method of the random class that accepts a list as an argument and shuffles the contents of the list. Use Google to find out how this is done. The __str__ method returns a string containing the list of Cards in the Deck: 13 Cards to a line. Hand(LinkedList) +__init__() +__str__(): string Figure 6: Hand Class Diagram The Hand class represents a player's Hand in the card game. It inherits from the LinkedList class, so that you can use all the LinkedList methods to manipulate the Card objects contained as data in the Node objects. The Hand is controlled by the Player class. The Player holds the Hand as an instance variable and has the methods used to manipulate the LinkedList. Player -hand: Hand +__init__() +add_card(card: Card) +add_pile(pile: Pile) +play_card(pile: Pile) +get_num_cards(): int +display_hand(): Figure 7: Player Class Diagram The Player class represents a Player of the card game. It holds the players Hand as an instance variable, using object composition. The Hand is a LinkedList of Cards. When a player is dealt a card from the Deck, the Card is added at the beginning of the Hand. When a player adds a Pile after becoming the round winner, the Pile of Cards is placed at the end of the Hand, one-by-one. When a Player plays a Card, it is taken from the front of the Hand. The add_card method adds the passed-in Card to the front of the LinkedList Hand. The add_pile method removes each Card from the passed in Pile and adds it to the end of the LinkedList Hand. The play_card method removes the top Card from the Hand and adds it to the passed in Pile using the add_card Pile method . The get_num_cards method returns the number of Cards in the Hand. The display_hand method prints Player's Hand by calling the Hand's __str__ method. Pile(LinkedList) +__init__() +add_card(card: Card) +remove_card(): Card +top_two_cards(): player1Card: Card, Player2card: Card +__str__(): string Figure 8: Pile Class Diagram The Pile class holds the stack of Cards on the table. It inherits from the LinkedList class, so that you can use all the LinkedList methods to manipulate the Card objects contained as data in the Node objects. Be sure to call the LinkedList constructor, when creating the Pile object. The add_card method adds the Card to the front of the LinkedList. The top_two_cards method returns the Card data from the first two nodes on the LinkedList. The __str__ method returns the Cards in the Pile as a string as Rank-Suit Rank-Suit Rank-Suit: For example: K-C 7-D 6-H
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
