Assignment 10
War and Peace: Fun and Games with Lists
Pair or individual programming in this lab and assignment.
In Lab
Fill in the dots as usual in the following exercises, extend the test function as indicated, with output consistant with the transcript below, and submit in a file named lab10.py.
import random
SUITS = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
PIPS = ['two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
PIPS = PIPS + ['Jack', 'Queen', 'King', 'Ace'] # Ace-high order
def concatinate_after_all(los, s):
"""
Return a list of the results of concatinating each element of the given
list of strings with the string s.
>>> concatinate_after_all(['a', 'b'], 'c')
['ac', 'bc']
>>>
"""
#.
#.
#.
#.
#.
#.
def full_deck():
"""
Returns a list representing a deck of Anglo-American-French style playing
cards. Each card is represent by a list of the form '<pip> of <suit>', where
<pip> is 'one' through 'ten', 'Jack', 'Queen', 'King', or 'Ace', and <suit>
is one of the 'Spades', 'Hearts', 'Diamonds', or 'Clubs'. A deck represents
all possible combinations of pips and suits, with no duplicates, for a total
of 52 cards.
>>> deck = full_deck()
>>> len(deck)
52
>>> deck[0:2]
['two of Spades', 'three of Spades']
>>>
"""
# Hint: use concatinate_after_all
#.
#.
#.
#.
#.
#.
#.
def pip(card):
"""
Returns the pip of card, assuming card was returned by the full_deck
function.
>>> pip('3 of Spades')
'3'
>>>
"""
#.
#.
def shuffle(cards):
"""
Return a list of cards that is a shuffle (reordering or permutation) of the
given list of cards.
"""
#.
#.
#.
#.
#.
#.
#.
NEW_DECK = full_deck()
def shuffled_deck():
"""
Return a shuffle of a full deck of cards.
>>> equal_sets(shuffled_deck(), NEW_DECK)
True
>>> equal_sets(shuffled_deck(), full_deck())
True
>>>
"""
return shuffle(NEW_DECK)
def deal(cards):
"""
Remove and return the first (top) element from the list of cards.
>>> deck = full_deck()
>>> deal(deck)
'two of Spades'
>>> deal(deck)
'three of Spades'
>>>
"""
#.
#.
#.
def equal_sets(set1, set2):
"""
The arguments are sets (lists without duplicate values). Returns a boolean
value indicating if the sets are equal, that is, one is a shuffle (a.k.a
reordering or permutation) of the other.
"""
# The sets may represent hands or decks of cards.
# By the no-duplicates assumption, if set1 has the same number of values
# as set2 and every value in set1 is in set2, then set1 is equal to
# set2, and not otherwise.
#.
#.
#.
#.
#.
#.
#.
#.
def deal_hand(deck, n):
"""
Returns a hand (list) of n cards dealt from the deck.
>>> deck = shuffled_deck()
>>> poker_hand = deal_hand(deck, 5)
>>> equal_sets(poker_hand + deck, full_deck())
True
>>>
"""
#.
#.
#.
#.
#.
def card_wins(card1, card2):
"""
Returns a boolean value indicating if card1 wins over card2 in by the
usual Ace-high pip ordering. Suite is ignored.
"""
# Hint: card with greater pip index in PIPS wins
#...
def test():
"""
Add tests for functions in this file.
"""
Start work on the assignment if time permits.
Assignment
Complete the peace function in the following program skeletons. For an A grade, complete the war function as well.
This skeleton assumes a solution to the lab above is in the file lab10.py in the same directory as the program. As usual, the lab solution will be posted over the weekend and you are free to use this solution, or your own.
Use the name_elements function to format output as in the example transcripts.
import random
import lab10
def name_elements(los):
"""
Return a string made of the values in the given list of strings,
separated by commas (followed by a space), except the last two,
which are separated by " and ". Assume lst has at least two elements.
>>> name_elements(['a', 'b', 'c'])
'a, b and c'
>>>
"""
#.
#.
#.
#.
#.
#.
def peace(max_tricks):
"""
Simulates the card game 'war' with two players, as described in
http://www.pagat.com/war/war.html, but without wars: if pips of the cards
exposed in a trick are the same, the cards are taken out of the game and
play continues. Assume players do not reorder cards from the order in which
they are dealt. The game ends if either player runs out of cards or after
max_tricks have been played. The player with the most cards at the end wins.
>>> peace(5)
Player 2 takes three of Diamonds and eight of Clubs
Player 1 takes King of Clubs and ten of Diamonds
Neither player takes three of Spades and three of Hearts
Player 1 takes Queen of Diamonds and two of Clubs
Player 1 takes ten of Hearts and eight of Hearts
Player 1 wins
>>>
"""
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
def war():
"""
Simulates the card game 'war', as described in
http://www.pagat.com/war/war.html, with option 1:
If you don't have enough cards to complete the war, you lose.
Players shuffle (randomly order) cards that are won before adding them
to the bottom of their deck. This prevents a possibly infinite game due to
a recurrent card pattern.
"""
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
#.
A partial trancript of the war game follows:
Player 1 takes Ace of Spades and King of Hearts Player 2 takes four of Spades and Queen of Clubs Player 2 takes seven of Diamonds and ten of Spades Player 2 takes two of Spades and eight of Hearts Player 1 takes nine of Hearts and eight of Diamonds War over seven of Clubs and seven of Spades Player 1 takes seven of Clubs, seven of Spades, five of Hearts, three of Clubs, King of Spades and five of Diamonds Player 1 takes Jack of Clubs and two of Hearts War over six of Diamonds and six of Hearts Player 2 takes six of Diamonds, six of Hearts, nine of Spades, eight of Clubs, three of Diamonds and Jack of Diamonds ...
Of course since all the transcripts here are based on randomly "shuffled" cards, your results will be different.
Turn in your solution via Oncourse as usual.