Question: Task 1 :The Card Class Implement a class namedCard. An instance of this class should have two private properties,faceandvalue. Face is a string and value

Task 1 :The Card Class

  1. Implement a class namedCard. An instance of this class should have two private properties,faceandvalue. Face is a string and value is an integer. This allows a wild card "*" to have a value. The card is initialized with a given value, and the face is assigned automatically based on this value. If the value is -1 then face is '*'. The face can be "*", "0", "1", "2", "3", "4", "5", "6", "7", "8", or "9".An AssertionError should be raised if an invalid value is provided when attempting to create an instance of this Card class.
  2. The methodassign(value)allows assigning a new value to the card only if face of the card is '*'.Again, an AssertionError should be raised if an invalid value is provided.An Exception should be raised if trying to change the value of a non-joker card.
  3. The other methods in the interface includegetValue()which returns the value of a card,getFace()which returns the face of a card.__str__()generates "[self.face]" and __repr__() gives str()+"."+self.value.

Task 2: The PlayStack Class

  1. Implement a class namedPlayStack. An instance of this class should have a propertycards,which is a list of ordered cards that behaves like a stack. This property should not be directly accessible from outside the class. An instance of this class should behave like a stack, but it is a special stack as you will see below. You can only add cards from one end.Be sure to implement it so that you are adding to the end that will result in O(1) time efficiency.
  2. Implement a methodpeekValue()which returns the value of the card on top ofcards.Raise an Exception "Error: No cards in the playing stack" when appropriate.
  3. Implement a methodpeekFace()which returns the face of the card on top ofcards. Raise an Exception "Error: No cards in the playing stack" when appropriate.
  4. Implement a method namedplayCard(card), that takes a card and pushes it on top of thecardsstack. The card with value 0 can only be added when thecardsstack is empty, and the method only accepts valid cards from the Card class. Also, an added card can only be pushed on top of a card of directly lower value.For example, a card of value 5 can be added only on a card of value 4; Only a card of value 8 can be pushed on top of a card of value 7. If an error occurs the method should raise an Exception "Error: Card rejected". When the card of value 9 is played and accepted, the stack should be emptied and the method returns a list of the faces of all cards that were in the stack. Otherwise, the method returns an empty list.
  5. Implement the __str__() method to convert a PlayStack instance into a string such that the string represents the cards in the stackfrom the lowest value on the left to the highest value on the right delimited by two "|", eg. |[0][1][*][3][4][5]|.

Task 3: The Hand Class

  1. Implement the classHandwhich represents the cards held. An instance of this class should have the private propertyhand, which is a list of up to 5 Cards.
  2. Implement the methods of the class. The interface consists of:
  3. sort()sorts the cards in increasing order by value. ([*] is considered to have a value of -1 until played onto a Playing Pile.)
  4. pop()returns and removes the card on the far right. (i.e. the largest value card when the hand is sorted.)An Exception should be raised if there is no card to pop.
  5. pop(i)returns and removes the card at position i.Check that i is an integer in the correct range; an AssertionError should be raised if i is invalid.
  6. index(v)returns the index of the first card with value v.An AssertionError should be raised if v is invalid.Returns -1 if there are no cards with value v in the hand.
  7. check0()returns the index of the first card [0] in the hand. Returns -1 if there are no [0] cards in the hand.
  8. size()returns the number of cards in the hand.
  9. add(cardList)adds Cards in the cardList to the hand. Raises an Exception if adding those cards will cause the hand to contain more than 5 cards.
  10. Implement a method to convert a Hand into a string. The cards should show the face.

Task 4: Shuffle

Write a function that shuffles cards. Do not use any built-in shuffling function. This task is about creating your own. The function receives a list of cards and returns a list of the same cards, but shuffled. To shuffle the cards, proceed as follows. Repeat until you have no more cards to select: Select a random number between 0 and the size of the list of cards, pop the card at that position and append it to a result list. Return the result list.You should use the random module, but doNOTuse the random.shuffle() function.

Task 5: The Game

After shuffling all the cards, cards are distributed to the two players. The remaining cards go to the shoe. The player with the highest card on top of their Goal stack starts the game (or Player A starts if the top Goal cards are the same). Then the two players alternate until one finishes their goal pile. A diagram with the basic game algorithm is given below.

The general pseudo-code for the module dealing with the user's turn is illustrated below.

The following picture illustrates a given state of the game.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!