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: 663852 • 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.

PART C

Consider the program in the file named “lab12.partc.py”(LINK IS LISTED ABOVE FOR THE FILES NEEDED), which imports the module containing the Card and Deck classes. Extend that program to play the card game named “War”.

Note: you may not modify 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

1. 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”).

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. If the user chooses to quit the game after a battle, the player with the most cards will be declared the winner.

4. 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.

5. Your program must model each player’s stack of cards. One approach would be to use a list of cards, where slot 0 of the list is viewed as the top of the stack. The list method “append” could be used to add a card to the bottom of the stack.

6. Your program will be subdivided into functions which will pass parameters and return values to communicate between the functions.

7. When played with 52 cards, the game can take a long time to complete. To test your logic in a less time-consuming way, have your program deal out a much smaller number of cards (perhaps five to each player).

Explanation / Answer

Answer:

Note: user given code has been used and modified according to requirements.

Part B:

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 )

print( "===== player #1 after 1st dealt=====" )

print()

print( player1_list )

print()

print( "===== player #2 after 1st dealt=====" )

print()

print( player2_list )

print()

b. second card dealt and compare the results:

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 )

# second card dealt to Player #1

player1_card = player1_list.pop( 0 )

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

# First card dealt to Player #2

player2_card = player2_list.pop( 0 )

# second card dealt to Player #2

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. players hands after second dealt:

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 )

# second card dealt to Player #1

player1_card = player1_list.pop( 0 )

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

# First card dealt to Player #2

player2_card = player2_list.pop( 0 )

# second card dealt to Player #2

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. dealing all 5 cards:

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()

for i in range( 5 ):

     # ith card dealt to Player #1

     player1_card = player1_list.pop( 0 )

     # ith card dealt to Player #2

     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. program with different random integer to seed:

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( 56 )

# 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 ranks of the two cards

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. program without random.seed():

import cards

# 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 ranks of the two cards

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 )

Answers from 2 and 3 implicate that if the same integer is used to seed the random number generator then the program produces the same result for several runs. But if you use system time then the program produces the different results for several runs.

Part C:

War game:

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()

    

# Create a deck of cards

the_deck = cards.Deck()

# Shuffle the deck, then display it in 13 columns

the_deck.shuffle()

print( "===== shuffled deck =====" )

the_deck.display()

player1_list,player2_list=dealCards(the_deck)

#play battleship

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"