CSE231 Lab Exercise #10 Solved

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (3 votes)

Assignment Overview

 

You will work with a partner on this exercise during your lab session.  Two people should work at one computer.  Occasionally switch the person who is typing.  Talk to each other about what you are doing and why so that both of you understand each step.

 

Part A:  Using the Class Methods

 

  1. 1. Examine the Python statements in the file named “lab10a.py”, then extend that program to do the following tasks:

 

  1. Display each players’ hand after the first card has been played from each hand.
  2. Display the second card dealt to each player and compare them.
  3. Display each players’ hand after the second card has been played from each hand.
  4. 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: Q♠

Player #1 hand: [7♠, 10♦, 7♥]

 

Second card dealt to player #2: Q♣

Player #2 hand: [K♦, 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 Mimir testing: you will notice that the symbols for the suits are not printed in the Mimir 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 “lab10a.py”) for grading via the Mimir system.

 

Do steps 2 and 3 on your own without handing in to Mimir:

 

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

 

  1. 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 B:  Programming with the Class Methods

 

Consider the program in the file named “lab10b.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 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”).  Stop only if “n” or “N” is entered.

 

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

 

  1. 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 Test1—see output below).

 

  1. If the user chooses to quit the game after a battle, the player with the most cards will be declared the winner.

 

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

 

  1. Your program must model each player’s hand as a list of cards.

 

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

 

  1. 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 (Test1 below uses only five to each player).

 

  1. Note for Mimir testing:
    • We use a special version of cards.py for Test1 and provide it to you as cards_test1.py For your own testing copy cards_test1.py to cards.py (it has a special deck for test 1) Note that Test1 assumes you import cards (not import cards_test1).
    • Comment out shuffling (so we can have a consistent test)
    • Deal only 5 cards (to keep the game short)

 

« Demonstrate your completed program to your TA.  On-line students should submit the

completed program (named “lab10b.py”) for grading via the Mimir system.

 

Test 1

 

Starting Hands

Hand1: [3♠, 4♠, 5♠, 6♠, J♠]

Hand2: [8♠, 9♠, Q♠, K♠, 10♠]

 

Battle was 1: 3♠, 2: 8♠. Player 2 wins battle. hand1: [4♠, 5♠, 6♠, J♠] hand2: [9♠, Q♠, K♠, 10♠, 3♠, 8♠]

Keep Going: (Nn) to stop:y

Battle was 1: 4♠, 2: 9♠. Player 2 wins battle. hand1: [5♠, 6♠, J♠]

hand2: [Q♠, K♠, 10♠, 3♠, 8♠, 4♠, 9♠]

Keep Going: (Nn) to stop:y

Battle was 1: 5♠, 2: Q♠. Player 2 wins battle. hand1: [6♠, J♠]

hand2: [K♠, 10♠, 3♠, 8♠, 4♠, 9♠, 5♠, Q♠]

Keep Going: (Nn) to stop:y

Battle was 1: 6♠, 2: K♠. Player 2 wins battle. hand1: [J♠]

hand2: [10♠, 3♠, 8♠, 4♠, 9♠, 5♠, Q♠, 6♠, K♠]

Keep Going: (Nn) to stop:y

Battle was 1: J♠, 2: 10♠. Player 1 wins battle. hand1: [10♠, J♠]

hand2: [3♠, 8♠, 4♠, 9♠, 5♠, Q♠, 6♠, K♠]

Keep Going: (Nn) to stop:y

Battle was 1: 10♠, 2: 3♠. Player 1 wins battle. hand1: [J♠, 3♠, 10♠] hand2: [8♠, 4♠, 9♠, 5♠, Q♠, 6♠, K♠]

Keep Going: (Nn) to stop:y

Battle was 1: J♠, 2: 8♠. Player 1 wins battle. hand1: [3♠, 10♠, 8♠, J♠] hand2: [4♠, 9♠, 5♠, Q♠, 6♠, K♠]

Keep Going: (Nn) to stop:y

Battle was 1: 3♠, 2: 4♠. Player 2 wins battle. hand1: [10♠, 8♠, J♠] hand2: [9♠, 5♠, Q♠, 6♠, K♠, 3♠, 4♠]

Keep Going: (Nn) to stop:y

Battle was 1: 10♠, 2: 9♠. Player 1 wins battle. hand1: [8♠, J♠, 9♠, 10♠] hand2: [5♠, Q♠, 6♠, K♠, 3♠, 4♠]

Keep Going: (Nn) to stop:y

Battle was 1: 8♠, 2: 5♠. Player 1 wins battle.

hand1: [J♠, 9♠, 10♠, 5♠, 8♠] hand2: [Q♠, 6♠, K♠, 3♠, 4♠]

Keep Going: (Nn) to stop:y

Battle was 1: J♠, 2: Q♠. Player 2 wins battle. hand1: [9♠, 10♠, 5♠, 8♠] hand2: [6♠, K♠, 3♠, 4♠, J♠, Q♠]

Keep Going: (Nn) to stop:y

Battle was 1: 9♠, 2: 6♠. Player 1 wins battle.

hand1: [10♠, 5♠, 8♠, 6♠, 9♠] hand2: [K♠, 3♠, 4♠, J♠, Q♠]

Keep Going: (Nn) to stop:y

Battle was 1: 10♠, 2: K♠. Player 2 wins battle. hand1: [5♠, 8♠, 6♠, 9♠] hand2: [3♠, 4♠, J♠, Q♠, 10♠, K♠]

Keep Going: (Nn) to stop:y

Battle was 1: 5♠, 2: 3♠. Player 1 wins battle.

hand1: [8♠, 6♠, 9♠, 3♠, 5♠] hand2: [4♠, J♠, Q♠, 10♠, K♠] Keep Going: (Nn) to stop:y

Battle was 1: 8♠, 2: 4♠. Player 1 wins battle. hand1: [6♠, 9♠, 3♠, 5♠, 4♠, 8♠] hand2: [J♠, Q♠, 10♠, K♠]

Keep Going: (Nn) to stop:

Battle was 1: 6♠, 2: J♠. Player 2 wins battle.

hand1: [9♠, 3♠, 5♠, 4♠, 8♠] hand2: [Q♠, 10♠, K♠, 6♠, J♠] Keep Going: (Nn) to stop:

Battle was 1: 9♠, 2: Q♠. Player 2 wins battle. hand1: [3♠, 5♠, 4♠, 8♠] hand2: [10♠, K♠, 6♠, J♠, 9♠, Q♠] Keep Going: (Nn) to stop:

Battle was 1: 3♠, 2: 10♠. Player 2 wins battle. hand1: [5♠, 4♠, 8♠]

hand2: [K♠, 6♠, J♠, 9♠, Q♠, 3♠, 10♠] Keep Going: (Nn) to stop:

Battle was 1: 5♠, 2: K♠. Player 2 wins battle. hand1: [4♠, 8♠]

hand2: [6♠, J♠, 9♠, Q♠, 3♠, 10♠, 5♠, K♠] Keep Going: (Nn) to stop:

Battle was 1: 4♠, 2: 6♠. Player 2 wins battle. hand1: [8♠]

hand2: [J♠, 9♠, Q♠, 3♠, 10♠, 5♠, K♠, 4♠, 6♠] Keep Going: (Nn) to stop:

Battle was 1: 8♠, 2: J♠. Player 2 wins battle. hand1: []

hand2: [9♠, Q♠, 3♠, 10♠, 5♠, K♠, 4♠, 6♠, 8♠, J♠] Player 2 wins!!!

 

Test2

Illustrates play where the game is stopped before one player’s hand is empty, i.e. ‘n’ entered at prompt.

 

Starting Hands

Hand1: [3♠, 4♠, 5♠, 6♠, J♠]

Hand2: [8♠, 9♠, Q♠, K♠, 10♠]

 

Battle was 1: 3♠, 2: 8♠. Player 2 wins battle. hand1: [4♠, 5♠, 6♠, J♠] hand2: [9♠, Q♠, K♠, 10♠, 3♠, 8♠]

Keep Going: (Nn) to stop:n Player 2 wins!!!

 

Optional – not for handing in

If you have the time and interest, revise your program to handle the full version of the game rules.

 

The rules for the full version of War are the same as above, with one exception.  Instead of a stalemate when both players turn over a card with the same rank, the battle continues.  Each player turns over two more cards, and the second card of each pair is used to determine the result of the battle.  If one player’s card outranks the other player’s card, the battle is over and the winner adds all six cards to the bottom of his stack.  If the two cards have the same rank, the battle continues (as above).

 

The game rules for the full version do not specify what happens if one player runs out of cards during a protracted battle.  For example, assume both players turn over a King.  Each player is supposed to put down two more cards, but one of the players only has one card in his stack.

 

Use the following procedure to resolve this situation.  If one player runs out of cards without being able to complete a protracted battle, the other player wins.  If both players run out of cards simultaneously, the game is a draw (neither player wins).

  • lab10-da8wly.zip