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

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

ID: 3850787 • Letter: C

Question

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

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

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

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

lab11.pdf:https://www.cse.msu.edu/~cse231/Online/Labs/Lab11/lab11.pdf

Part A: Using the Class Methods 1. Examine the Python statements in the file named "labl la py", then extend that program to do the following tasks: a. Display each players' hand after the first card has been played from each hand. b. Display the second card dealt to each player and compare them c. Display each players' hand after the second card has been played from each hand d. Display the last card dealt to each player and compare them. Here is the additional output after those modifcations Second card dealt to player #1: Q4 Player #1 hand: [74, 10 Second card dealt to player #2: Q Player #2 hand EK 2 8 Last card in hand of player #1 7 Last card in hand of player #2: 8+ 8* of higher rank than 7 Note on Mirmir testing: you will notice that the symbols for the suits are not printed in the Mirmir tests instead the letters hcds are used. That is because Mimir cannot handle the symbols. You do not need to do anything about that-it is built into the cards.py that we use for testing. Demonstrate your completed program to your TA. on-line students should submit the completed program (named "labila.py") for grading via the Mirmir system Do steps 2 and 3 on your own without handing in to Mirmir 2. Revise the program to use a different integer number as the argument in the invocation of function "random seed". Run the program several times and observe the results. 3. Revise the program to eliminate the invocation of function "random.seed Run the program several times and observe the results. Note: if function "random.seed" is not invoked, then the current system time is used to initialize the random number generator

Explanation / Answer

Note:

* Given code used as it is without changing the variables and functions, required changes done in the given code.

* Multiple questions with multiple sup part is posted.

Solution:

a. Code to display each players hand after the playing first card from each hand:
# import required packages
import cards
import random
random.seed( 100 )
# deck of cards
my_deck = cards.Deck()
# Deck Shuffle
my_deck.shuffle()
print( "===== shuffled deck =====" )
my_deck.display()
print( "Dealt five cards to each player (alternating)" )
print()
player1_list=[]
player2_list=[]
for i in range( 5 ):
    player1_list.append( my_deck.deal() )
    player2_list.append( my_deck.deal() )
# Display each player's cards
print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()
player1_card = player1_list.pop( 0 )
print( "First card dealt to player #1:", player1_card )
player2_card = player2_list.pop( 0 )
print( "First card dealt to player #2:", player2_card )
print( "===== player #1 after 1st dealt=====" )
print()
print( player1_list )
print()
print( "===== player #2 after 1st dealt=====" )
print()
print( player2_list )
print()

b. Code to display second card dealt to each player and comparing it:

import cards
import random
random.seed( 100 )
# deck of cards
my_deck = cards.Deck()
# Deck Shuffle
my_deck.shuffle()
print( "===== shuffled deck =====" )
my_deck.display()
print( "Dealt five cards to each player (alternating)" )
print()
player1_list=[]
player2_list=[]
for i in range( 5 ):
    player1_list.append( my_deck.deal())
    player2_list.append( my_deck.deal())
# Display player's cards
print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()
player1_card = player1_list.pop( 0 )
player1_card = player1_list.pop( 0 )
print( "second card dealt to player #1:", player1_card )
player2_card = player2_list.pop( 0 )
player2_card = player2_list.pop( 0 )
print( "second card dealt to player #1:", player2_card )
print()
if player1_card.rank() == player2_card.rank():
    print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card.rank() > player2_card.rank():
    print( "Player #1 wins:", player1_card, "of higher rank than", player2_card )
else:
    print( "Player #2 wins:", player2_card, "of higher rank than", player1_card )

C. Code to display each player's hand after the second card has beed played from each hand:

# import required packages
import cards
import random
random.seed( 100 )
# deck of cards
my_deck = cards.Deck()
# Deck Shuffle
my_deck.shuffle()
print( "===== shuffled deck =====" )
my_deck.display()
print( "Dealt five cards to each player (alternating)" )
print()
player1_list=[]
player2_list=[]
for i in range( 5 ):
    player1_list.append( my_deck.deal() )
    player2_list.append( my_deck.deal() )
# Display each player's cards
print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()
player1_card = player1_list.pop( 0 )
player1_card = player1_list.pop( 0 )
print( "second card dealt to player #1:", player1_card )
player2_card = player2_list.pop( 0 )
player2_card = player2_list.pop( 0 )
print( "second card dealt to player #1:", player2_card )
print( "===== player #1 after 2nd dealt=====" )
print()
print( player1_list )
print()
print( "===== player #2 after 2nd dealt=====" )
print()
print( player2_list )
print()

d. Code to display the last card dealt to each player and comparison:

# import required packages
import cards
import random
random.seed( 100 )
# deck of cards
my_deck = cards.Deck()
# Deck Shuffle
my_deck.shuffle()
print( "===== shuffled deck =====" )
my_deck.display()
# Last Deal cards to each player
print( "Dealt five cards to each player (alternating)" )
print()
player1_list=[]
player2_list=[]
for i in range( 5 ):
    player1_list.append( my_deck.deal() )
    player2_list.append( my_deck.deal() )
# Display each player's cards
print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()
for i in range( 5 ):
     player1_card = player1_list.pop( 0 )
     player2_card = player2_list.pop( 0 )
print( "last card dealt to player #1:", player1_card )
print( "last card dealt to player #1:", player2_card )
print()
if player1_card.rank() == player2_card.rank():
    print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card.rank() > player2_card.rank():
    print( "Player #1 wins:", player1_card, "of higher rank than", player2_card )
else:
    print( "Player #2 wins:", player2_card, "of higher rank than", player1_card )

2. Code with different integer number as argument to ranom seed function:

import cards
import random
random.seed( 56 )
# deck of cards
my_deck = cards.Deck()
# Deck Shuffle
my_deck.shuffle()
print( "===== shuffled deck =====" )
my_deck.display()
print( "Dealt five cards to each player (alternating)" )
print()
player1_list=[]
player2_list=[]
for i in range( 5 ):
    player1_list.append( my_deck.deal() )
    player2_list.append( my_deck.deal() )
# Display each player's cards
print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()
player1_card = player1_list.pop( 0 )
print( "First card dealt to player #1:", player1_card )
player2_card = player2_list.pop( 0 )
print( "First card dealt to player #2:", player2_card )
print()
if player1_card.rank() == player2_card.rank():
    print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card.rank() > player2_card.rank():
    print( "Player #1 wins:", player1_card, "of higher rank than", player2_card )
else:
    print( "Player #2 wins:", player2_card, "of higher rank than", player1_card )

3. Code withot ranom seed function:

import cards
# deck of cards
my_deck = cards.Deck()
# Deck Shuffle
my_deck.shuffle()
print( "===== shuffled deck =====" )
my_deck.display()
print( "Dealt five cards to each player (alternating)" )
print()
player1_list=[]
player2_list=[]
for i in range( 5 ):
    player1_list.append( my_deck.deal() )
    player2_list.append( my_deck.deal() )
# Display each player's cards
print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()
player1_card = player1_list.pop( 0 )
print( "First card dealt to player #1:", player1_card )
player2_card = player2_list.pop( 0 )
print( "First card dealt to player #2:", player2_card )
print()
if player1_card.rank() == player2_card.rank():
    print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card.rank() > player2_card.rank():
    print( "Player #1 wins:", player1_card, "of higher rank than", player2_card )
else:
    print( "Player #2 wins:", player2_card, "of higher rank than", player1_card )

From the result of program 2 and 3,the usage of same integer for random seed generator will produce same result for multiple runs. the usage of system time produces different result.

Part C: War:

import cards
#deal the cards
def dealCards(the_deck):
     print( "Dealt cards equally to each player (alternating)" )
     print()
     player1_list=[]
     player2_list=[]
     for i in range( 26 ):
          player1_list.append( the_deck.deal() )
          player2_list.append( the_deck.deal() )
     return player1_list,player2_list

# Compare the value of the two cards
def compareCards(player1_card,player2_card):
     print()
     if player1_card.rank() == player2_card.rank():
          print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
          return 0
     elif player1_card.rank() > player2_card.rank():
          if player2_card.value()!=1:
              print( "Player #1 wins:", player1_card, "of higher rank than", player2_card )
              return 1
          else:
              print( "Player #2 wins:", player2_card, "of higher rank than", player1_card )
              return 2
     else:
         if player1_card.value()!=1:
              print( "Player #2 wins:", player2_card, "of higher rank than", player1_card )
              return 2
         else:
              print( "Player #1 wins:", player1_card, "of higher rank than", player2_card )
              return 1
# Display each player's cards
def displayList(player1_list, player2_list):
     print( "===== player #1 =====" )
     print()
     print( player1_list )
     print()
     print( "===== player #2 =====" )
     print()
     print( player2_list )
     print()

the_deck = cards.Deck()
the_deck.shuffle()
print( "===== shuffled deck =====" )
the_deck.display()
player1_list,player2_list=dealCards(the_deck)
while True:
     player1_card = player1_list.pop( 0 )
     print( "card dealt to player #1:", player1_card )
     player2_card = player2_list.pop( 0 )
     print( "card dealt to player #2:", player2_card )
     win=compareCards(player1_card,player2_card)
     if win==0:
         player1_list.append(player1_card)
         player2_list.append(player2_card)
     if win==1:
          player1_list.append(player1_card)
          player1_list.append(player2_card)
     elif win==2:
          player2_list.append(player2_card)
          player2_list.append(player1_card)
     displayList(player1_list, player2_list)
     cont=raw_input("Press enter to continue battle: ")
     if cont=="q":
          break;
#declare the winner
if len(player1_list)> len(player2_list):
     print "Player 1 wins in the war game"
elif len(player2_list)>len(player1_list):
     print "Player 2 wins in the war game"
else:
     print "Game tie"