Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please use the python 3.6 this is what you may need: cards.py:https://www.cse.ms

ID: 3850611 • Letter: P

Question

please use the python 3.6

this is what you may need:

cards.py:https://www.cse.msu.edu/~cse231/Labs/Lab11/cards.py

strings.txt:https://www.cse.msu.edu/~cse231/Labs/Lab11/strings.txt

Part B: Programming with the Class Methods Consider the program in the file named "labl lb.py", which imports the module containing the Card and Deck classes. Extend that program to play the card game named "War". Note: you may not modity the contents of "cards. py" (the Card and Deck classes) Game Rules (Simple Version) War is a two-player game which uses a standard deck of 52 cards. Suits are ignored in the game, and the cards are ordered as follows: Ace King Queen Jack 10 9 8 7 6 5 4 3 2 (Aces are the highest cards) The cards are distributed to the two players one at a time (alternating), until both players have a stack of 26 cards Each player turns over the top card on his stack (a battle). Whoever played the card with the highest rank wins the battle and adds both cards to the bottom of his stack. In the simple version, if both cards have the same rank, each player simply returns his card to the bottom of his own stack (the battle was a stalemate). Play continues until one player has all 52 cards in his stack and wins the game Programming Notes l. After dealing out the cards, your program will repeatedly process one battle. After each battle, your program will ask the user if he wishes to continue or not (make sure that simply touching the Enter key is the default and means "continue Stop only if "n" or "N" is entered 2. For each battle, your program will display the card played by each player, announce the result of the battle, and display each player's stack of cards. If one player wins the game as the result of the battle, your program will announce the winner and halt. 3. After cards are played they go on the bottom of the winning hand placing your card first and then the opponents card next (this is the ordering of Testl-see output below) 4. If the user chooses to quit the game after a battle, the player with the most cards will be declared the Winner. 5. In the game of War, Aces are the highest-ranked cards. However, in the Card class, Aces have the lowest rank. Your program must account for this difference 6. Your program must model each player's hand as a list of cards 7. Your program will be subdivided into functions which will pass parameters and return values to communicate among the functions 8. When played with 52 cards, the game can take a long time to complete. To test your logic in a less

Explanation / Answer

The code is given below :

import random

class Card( object ):
""" Model a playing card. """

# Rank is an int (1-13), where aces are 1 and kings are 13.
# Suit is an int (1-4), where clubs are 1 and spades are 4.
# Value is an int (1-10), where aces are 1 and face cards are 10.

# List to map int rank to printable character (index 0 used for no rank)
rank_list = ['x','A','2','3','4','5','6','7','8','9','10','J','Q','K']

# List to map int suit to printable character (index 0 used for no suit)
# 1 is clubs, 2 is diamonds, 3 is hearts, and 4 is spades
suit_list = ['x','u2663','u2666','u2665','u2660']
#suit_list = ['x','c','d','h','s']
  
def __init__( self, rank=0, suit=0 ):
""" Initialize card to specified rank (1-13) and suit (1-4). """
self.__rank = 0
self.__suit = 0
self.__face_up = None
# Verify that rank and suit are ints and that they are within
# range (1-13 and 1-4), then update instance variables if valid.
if type(rank) == int and type(suit) == int:
if rank in range(1,14) and suit in range(1,5):
self.__rank = rank
self.__suit = suit
self.__face_up = True
  
def rank( self ):
""" Return card's rank (1-13). """
return self.__rank

def value( self ):
""" Return card's value (1 for aces, 2-9, 10 for face cards). """
# Use ternary expression to determine value.
return self.__rank if self.__rank < 10 else 10

def suit( self ):
""" Return card's suit (1-4). """
return self.__suit
  
def is_face_up( self ):
""" Returns True if card is facing up."""
return self.__face_up
  
def flip_card( self ):
""" Flips card between face-up and face-down"""
self.__face_up = not self.__face_up

def __str__( self ):
""" Convert card into a string (usually for printing). """
# Use rank to index into rank_list; use suit to index into suit_list.
if self.__face_up:
return "{}{}".format( (self.rank_list)[self.__rank],
(self.suit_list)[self.__suit] )
else:
return "{}{}".format( "X", "X")

def __repr__( self ):
""" Convert card into a string for use in the shell. """
return self.__str__()
  
def __eq__( self, other ):
""" Return True, if Cards of equal rank and suit; False, otherwise. """
if not isinstance(other, Card):
return False
  
return self.rank() == other.rank() and self.suit() == other.suit()
  
class Deck( object ):
""" Model a deck of 52 playing cards. """

# Implement the deck as a list of cards. The last card in the list is
# defined to be at the top of the deck.

def __init__( self ):
""" Initialize deck--Ace of clubs on bottom, King of spades on top. """
self.__deck = [Card(r,s) for s in range(1,5) for r in range(1,14)]

def shuffle( self ):
""" Shuffle deck using shuffle method in random module. """
random.shuffle(self.__deck)

def deal( self ):
""" Return top card from deck (return None if deck empty). """
# Use ternary expression to guard against empty deck.
return self.__deck.pop() if len(self.__deck) else None

def is_empty( self ):
""" Return True if deck is empty; False, otherwise """
return len(self.__deck) == 0

def __len__( self ):
""" Return number of cards remaining in deck. """
return len(self.__deck)
  
def __str__( self ):
""" Return string representing deck (usually for printing). """
return ", ".join([str(card) for card in self.__deck])
  
def __repr__( self ):
""" Return string representing deck (for use in shell). """
return self.__str__()

def display( self, cols=13 ):
""" Column-oriented display of deck. """
for index, card in enumerate(self.__deck):
if index%cols == 0:
print()
print("{:3s} ".format(str(card)), end="" )
print()
print()