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

http://www.cse.msu.edu/~cse231/Labs/Lab12/ <------Link for files needed PART B E

ID: 663887 • Letter: H

Question

http://www.cse.msu.edu/~cse231/Labs/Lab12/ <------Link for files needed

PART B

Examine the Python statements in the file named “lab12.partb.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.

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

part a,b,c,d

##
## Demonstrate some of the operations of the Deck and Card classes
##

import cards

# Seed the random number generator to a specific value so every execution
# of the program uses the same sequence of random numbers (for testing).

import random
random.seed( 100 )

# Create a deck of cards

my_deck = cards.Deck()


# Shuffle the deck, then display it in 13 columns

my_deck.shuffle()
print( "===== shuffled deck =====" )
my_deck.display()


# Deal five cards to each player (alternating)

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 and the cards still in the deck

print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()


# First card dealt to Player #1

player1_card = player1_list.pop( 0 )

print( "First card dealt to player #1:", player1_card )


# First card dealt to Player #2

player2_card = player2_list.pop( 0 )

print( "First card dealt to player #2:", player2_card )


# Compare the two cards using operloaded operators

print()
if player1_card == player2_card:
    print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card > player2_card:
    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 )
  
# Second card dealt to Player #1

player1_card = player1_list.pop( 1 )

print( "First card dealt to player #1:", player1_card )


# Second card dealt to Player #2

player2_card = player2_list.pop( 1 )

print( "First card dealt to player #2:", player2_card )

# Compare the two cards using operloaded operators

print()
if player1_card == player2_card:
    print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card > player2_card:
    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 )

print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()
  

2.

##
## Demonstrate some of the operations of the Deck and Card classes
##

import cards

# Seed the random number generator to a specific value so every execution
# of the program uses the same sequence of random numbers (for testing).

import random
x=random.randint(1,1-00)
random.seed( x )

# Create a deck of cards

my_deck = cards.Deck()


# Shuffle the deck, then display it in 13 columns

my_deck.shuffle()
print( "===== shuffled deck =====" )
my_deck.display()


# Deal five cards to each player (alternating)

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 and the cards still in the deck

print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()


# First card dealt to Player #1

player1_card = player1_list.pop( 0 )

print( "First card dealt to player #1:", player1_card )


# First card dealt to Player #2

player2_card = player2_list.pop( 0 )

print( "First card dealt to player #2:", player2_card )


# Compare the two cards using operloaded operators

print()
if player1_card == player2_card:
    print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card > player2_card:
    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 )
  
# Second card dealt to Player #1

player1_card = player1_list.pop( 1 )

print( "First card dealt to player #1:", player1_card )


# Second card dealt to Player #2

player2_card = player2_list.pop( 1 )

print( "First card dealt to player #2:", player2_card )

# Compare the two cards using operloaded operators

print()
if player1_card == player2_card:
    print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card > player2_card:
    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 )

print( "===== player #1 =====" )
print()
print( player1_list )
print()
print( "===== player #2 =====" )
print()
print( player2_list )
print()
print( "===== remaining cards in deck =====" )
my_deck.display()