Python 3.4 Whist card class help Whist is a card game for 4 players. It is a for
ID: 3767748 • Letter: P
Question
Python 3.4 Whist card class help
Whist is a card game for 4 players. It is a forerunner of Bridge, but unlike Bridge, it has no bidding phase. It is played with a standard 52-card deck (see appendix at the bottom of the assignment if you’re unfamiliar with the standard deck). There is no ranking among the suits. Play proceeds as follows:
The game requires 4 players, who play in 2-person partnerships. Partners sit across the table from each other. Customarily, the players are identified by compass directions (North, South, East, West). North and South are partners, as are East and West.
A dealer is chosen by lot. The dealer deals the entire deck, one card at a time, starting with the player to the dealer’s left and proceeding clockwise, until the deck is exhausted and each player has a hand of 13 cards.
The last card dealt to the dealer is turned face up. The suit of this card is trump for this hand.
(After the first trick, the dealer puts this card back in her hand.)
Each hand consists of 13 tricks, in which each player plays one card. The first trick is led by the player to the dealer’s left and play proceeds to the left.
Each player plays one card. Players must follow suit (play the same suit that was led) if they have any cards of that suit. If they have no cards of that suit, they may play a card of the trump suit, or may discard (play any other card in their hand).
When each player has played, the highest card played of the trump suit takes the trick. If no trumps were played (which is more often the case), the highest card played of the suit led takes the trick. A card that is not the suit led or trump cannot take the trick.
Suppose Spades are trump, and East leads the 6 of Clubs. Other players play the Ace of Diamonds, Ace of Hearts, and King of Diamonds. Since no trump were played, and the other players were apparently unable to follow suit, the 6 of Clubs takes the trick. On the other hand, if the 2 of Spades were played instead of the King of Diamonds, it would take the trick, as it was trump.
The winner of one trick leads the next. The player leading the trick may lead with any card they choose.
When all cards have been played, each partnership counts the tricks they have taken. The winning partnership scores 1 point for each trick over 6 that they took. (If they took 7 tricks, they score 1 point; 8 tricks scores 2 points; and so on).
The game is 5 points. As only one partnership can score each hand, ties are impossible.
If neither partnership has yet won the game, the deal passes to the left and another hand is played.
You will write a Player object (class). This class will have a method called PlayCard(). The parameter list for this method is rather long:
A tuple of cards. These are the cards in your hand.
A (possibly empty) list of cards that have been played so far this trick. (If the list is empty, you need to lead for this trick.) Cards are listed in the order played.
A string (one of ‘S’, ‘H’, ‘D’, ‘C’) specifying trump for this hand.
A string (one of ‘N’, ‘S’, ‘E’, ‘W’) specifying which position you are playing.
A (possibly empty) list of tricks that have been played so far this hand. Each is a tuple consisting of: 4 cards (the cards that were played), a string “N”, “S”, “E”, “W” specifying who led that trick, and another string (same format) specifying who won that trick.
The current score of the game; NS score, then EW score.
The PlayCard() method should not have defaults for any of these parameters.
Your PlayCard method must select and return a card from your hand to play. If it is not leading, it must follow suit if possible. It must return a Card, not a string, integer, or other representation of the card.
That’s good for a B.
For an A, you must implement some kind of strategy in your class. Document this strategy in your code. How do you select which card to play? There are several possibilities.
You may choose to always play the highest card available.
You may decide to always play trump if you can (to maximize the number of tricks taken).
On the other hand, if a card’s already been played that’s higher than anything you have in that suit, then you can’t take the trick (you have to follow suit if you can), and should probably play your lowest card in that suit.
On the other hand, if your partner led an Ace in a suit you don’t have, that will probably take the trick and you should save your trump for when it would be more needed.
BUT, if it’s late in the hand and there are only a few cards of that suit left, an opponent may play a trump.
One strategy is to “draw trumps” by leading with your high trumps to force opponents to play their trumps early, so they can’t use them to take tricks later in the game. This assumes that you have the high trumps. If you only have low trumps, or only have a few, try to look for better opportunities.
If you only have a few cards of a suit, you may decide to discard them as soon as possible, so you can play trump the next time that suit is led.
With careful tracking of what cards have been played, it’s possible to work out how many cards of a particular suit are unplayed, and where some of the cards may lie. (If the last time an opponent led trump, your partner discarded, then your partner is out of trumps. If 12 trumps have already been played and you have 1 trump in your hand, then you have the last trump.)
You must use some method beyond playing the first (or last) legal card found or randomly choosing from legal cards. You can base it on the rank of the cards in your hand, on what cards have been played, etc. (The actual game has strict rules against going through previous tricks to see what’s been played, but your object will have that information available, since it won’t be ‘watching’ as the hand is played. You don’t have to use that information for your strategy, but it’s available if you want to use it.)
Support code:
Your program will use the Card class attached. DO NOT MODIFY THIS CLASS. The class used in the final program will have the same interface—the same public methods will be available and will have the same parameters. Do not assume the private/internal data elements of the class will be the same. Read the docstrings of the classes carefully; these are not quite the same as versions developed in class. Put the Card.py file in the same folder as your code. Your program will import Card early in the program.
Your file should be named Player.py. Your class name should be Player.
Your Player should have an __init__(self) method. You may add whatever other methods or data you please, but must have a PlayCard() method with the interface specified above.
A test script and file will be provided. Put the test script and data file into the same folder as your Player file. The test script will use the unittest module to run various tests on your class. It will start with the basics (Is there a Player module? Does it define a Player class? Does the Player class have a PlayCard method? Does the PlayCard method have the right number of parameters? And so on.) If all of these tests are passed, more advanced tests will be done, verifying that your object returns a Card, that it only returns a Card from the hand, that it follows suit if possible, etc. Submissions that pass ALL testing without errors will be entered into a tournament. Details will be announced as the deadline approaches (and the instructors get the various test scripts written).
Note that this gives you a lot of control over your grade. If your class returns a Card from the hand, always following suit if possible…then you’ve met the requirements for a B. So you can check for yourself how you’re progressing toward meeting the requirements of the assignment.
APPENDIX: The standard deck of cards (US/UK)
The standard deck consists of 52 cards, comprising 4 suits of 13 cards each. The suits are: Spades,
Hearts, Diamonds, Clubs. The suits are printed in contrasting colors: Spades and Clubs are Black suits, Hearts and Diamonds are Red suits. Color is not significant in Whist. There is no ranking among the suits in Whist.
The ranks of the cards are: Ace (high), King, Queen, Jack (often called the Knave in UK), 10, 9, 8, 7, 6, 5, 4, 3, 2 (low). In Whist the Ace is always high; this varies in some games.
Some games also have one or two Jokers, which are outside the normal ranking. They have no rank and no suit (or constitute their own). Whist does not use Jokers.
Explanation / Answer
from random import *
global northSpades
global northHearts
global northDiamonds
global northClubs
global eastSpades
global eastHearts
global eastDiamonds
global eastClubs
global southSpades
global southHearts
global southDiamonds
global southClubs
global westSpades
global westHearts
global westDiamonds
global westClubs
spades = "spades"
hearts = "hearts"
diamonds = "diamonds"
clubs = "clubs"
#a card object is a 2-tuple. The element of the tuple are face value and suit. 14 = A, 13 = K, 12 = Q, 11 = J
#declared as variables to make it easier for me to work with them
#spades:
SA = (14, "spades")
SK = (13, "spades")
SQ = (12, "spades")
SJ = (11, "spades")
S10 = (10, "spades")
S9 = (9, "spades")
S8 = (8, "spades")
S7 = (7, "spades")
S6 = (6, "spades")
S5 = (5, "spades")
S4 = (4, "spades")
S3 = (3, "spades")
S2 = (2, "spades")
#hearts:
HA = (14, "hearts")
HK = (13, "hearts")
HQ = (12, "hearts")
HJ = (11, "hearts")
H10 = (10, "hearts")
H9 = (9, "hearts")
H8 = (8, "hearts")
H7 = (7, "hearts")
H6 = (6, "hearts")
H5 = (5, "hearts")
H4 = (4, "hearts")
H3 = (3, "hearts")
H2 = (2, "hearts")
#diamonds:
DA = (14, "diamonds")
DK = (13, "diamonds")
DQ = (12, "diamonds")
DJ = (11, "diamonds")
D10 = (10, "diamonds")
D9 = (9, "diamonds")
D8 = (8, "diamonds")
D7 = (7, "diamonds")
D6 = (6, "diamonds")
D5 = (5, "diamonds")
D4 = (4, "diamonds")
D3 = (3, "diamonds")
D2 = (2, "diamonds")
#clubs:
CA = (14, "clubs")
CK = (13, "clubs")
CQ = (12, "clubs")
CJ = (11, "clubs")
C10 = (10, "clubs")
C9 = (9, "clubs")
C8 = (8, "clubs")
C7 = (7, "clubs")
C6 = (6, "clubs")
C5 = (5, "clubs")
C4 = (4, "clubs")
C3 = (3, "clubs")
C2 = (2, "clubs")
#unshuffledDeck is a list that contains all the cards in a standard 52 card deck (in order to make sure I didn't forget any)
unshuffledDeck = [SA, SK, SQ, SJ, S10, S9, S8, S7, S6, S5, S4, S3, S2, HA, HK, HQ, HJ, H10, H9, H8, H7, H6, H5, H4, H3, H2, DA, DK, DQ, DJ, D10, D9, D8, D7, D6, D5, D4, D3, D2, CA, CK, CQ, CJ, C10, C9, C8, C7, C6, C5, C4, C3, C2]
def nonzeromin(List): #returns the lowest non-zero value in a list
nonzerolist=[]
for i in List:
if i != 0:
nonzerolist.append(i)
if len(nonzerolist)==0:
return "Empty List"
else:
return min(nonzerolist)
def shuffleDeck():
shuffledDeck = []
unshuffledDeckCopy = unshuffledDeck[:]
while len(unshuffledDeckCopy) > 0:
length = len(unshuffledDeckCopy)-1
card = unshuffledDeckCopy.pop(randint(0, length))
shuffledDeck.append(card)
return shuffledDeck
def deal():
northSpades = []
northHearts = []
northDiamonds = []
northClubs = []
eastSpades = []
eastHearts = []
eastDiamonds = []
eastClubs = []
southSpades = []
southHearts = []
southDiamonds = []
southClubs = []
westSpades = []
westHearts = []
westDiamonds = []
westClubs = []
deck = shuffleDeck()
while len(deck) > 0:
north = deck.pop()
if north[1] == "spades":#
northSpades.append(north)
elif north[1] == "hearts":
northHearts.append(north)
elif north[1] == "diamonds":
northDiamonds.append(north)
elif north[1] == "clubs":
northClubs.append(north)
east = deck.pop()
if east[1] == "spades":
eastSpades.append(east)
elif east[1] == "hearts":
eastHearts.append(east)
elif east[1] == "diamonds":
eastDiamonds.append(east)
elif east[1] == "clubs":
eastClubs.append(east)
south = deck.pop()
if south[1] == "spades":
southSpades.append(south)
elif south[1] == "hearts":
southHearts.append(south)
elif south[1] == "diamonds":
southDiamonds.append(south)
elif south[1] == "clubs":
southClubs.append(south)
west = deck.pop()
if west[1] == "spades":
westSpades.append(west)
elif west[1] == "hearts":
westHearts.append(west)
elif west[1] == "diamonds":
westDiamonds.append(west)
elif west[1] == "clubs":
westClubs.append(west)
northSpades.sort(reverse = True)
northHearts.sort(reverse = True)
northDiamonds.sort(reverse = True)
northClubs.sort(reverse = True)
eastSpades.sort(reverse = True)
eastHearts.sort(reverse = True)
eastDiamonds.sort(reverse = True)
eastClubs.sort(reverse = True)
southSpades.sort(reverse = True)
southHearts.sort(reverse = True)
southDiamonds.sort(reverse = True)
southClubs.sort(reverse = True)
westSpades.sort(reverse = True)
westHearts.sort(reverse = True)
westDiamonds.sort(reverse = True)
westClubs.sort(reverse = True)
northHand = [northSpades, northHearts, northDiamonds, northClubs]#put the different parts of the hand together
eastHand = [eastSpades, eastHearts, eastDiamonds, eastClubs]
southHand = [southSpades, southHearts, southDiamonds, southClubs]
westHand = [westSpades, westHearts, westDiamonds, westClubs]
return northHand, eastHand, southHand, westHand
global handCount
handCount = 0
global leader
global trumps
trumps="dummy"
def trick(leader):
global northCard
global eastCard
global southCard
global westCard
global winner
winner = "dummy"
ledCard = "dummy"
if leader == "north":
#north plays a card
if trumps == "spades":
if len(northHearts) == nonzeromin([len(northHearts), len(northDiamonds), len(northClubs)]):
ledCard = northHearts.pop(0)
elif len(northDiamonds)== nonzeromin([len(northHearts), len(northDiamonds), len(northClubs)]):
ledCard = northDiamonds.pop(0)
elif len(northClubs) == nonzeromin([len(northHearts), len(northDiamonds), len(northClubs)]):
ledCard = northClubs.pop(0)
else:
ledCard = northSpades.pop(0)
elif trumps == "hearts":
if len(northSpades) == nonzeromin([len(northSpades), len(northDiamonds), len(northClubs)]):
ledCard = northSpades.pop(0)
elif len(northDiamonds) == nonzeromin([len(northSpades), len(northDiamonds), len(northClubs)]):
ledCard = northDiamonds.pop(0)
elif len(northClubs) == nonzeromin([len(northSpades), len(northDiamonds), len(northClubs)]):
ledCard = northClubs.pop(0)
else:
ledCard = northHearts.pop(0)
elif trumps == "diamonds":
if len(northSpades)== nonzeromin([len(northSpades), len(northHearts), len(northClubs)]):
ledCard = northSpades.pop(0)
elif len(northHearts) == nonzeromin([len(northSpades), len(northHearts), len(northClubs)]):
ledCard = northHearts.pop(0)
elif len(northClubs) == nonzeromin([len(northSpades), len(northHearts), len(northClubs)]):
ledCard = northClubs.pop(0)
else:
ledCard = northDiamonds.pop(0)
else:
if len(northSpades) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds)]):
ledCard = northSpades.pop(0)
elif len(northHearts) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds)]):
ledCard = northHearts.pop(0)
elif len(northDiamonds) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds)]):
ledCard = northDiamonds.pop(0)
else:
ledCard = northClubs.pop(0)
suit = ledCard[1]
print "North: ", ledCard
northCard = ledCard
#east plays a card
if suit == "spades" and len(eastSpades)>0:
eastCard = eastSpades.pop(0)
elif suit == "hearts" and len(eastHearts) >0:
eastCard = eastHearts.pop(0)
elif suit == "diamonds" and len(eastDiamonds)>0:
eastCard = eastDiamonds.pop(0)
elif suit == "clubs" and len(eastClubs)>0:
eastCard = eastClubs.pop(0)
elif trumps == "spades" and len(eastSpades)>0:
eastCard = eastSpades.pop(0)
elif trumps == "hearts" and len(eastHearts)>0:
eastCard = eastHearts.pop(0)
elif trumps == "diamonds" and len(eastDiamonds)>0:
eastCard = eastDiamonds.pop(0)
elif trumps == "clubs" and len(eastClubs)>0:
eastCard = eastClubs.pop(0)
elif len(eastSpades) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastSpades.pop()
elif len(eastHearts) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastHearts.pop()
elif len(eastDiamonds) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastDiamonds.pop()
else:
eastCard = eastClubs.pop()
print "East: ", eastCard
#south (human) plays a card
southCard = input("Which card will you play?")
print "South: ", southCard
if southCard in southSpades:
southSpades.remove(southCard)
elif southCard in southHearts:
southHearts.remove(southCard)
elif southCard in southDiamonds:
southDiamonds.remove(southCard)
else:
southClubs.remove(southCard)
#west plays a card
if suit == "spades" and len(westSpades)>0:#follows same proceedure for playing a card as east does (all non-leading computer players do this)
westCard = westSpades.pop(0)
elif suit == "hearts" and len(westHearts)>0:
westCard = westHearts.pop(0)
elif suit == "diamonds" and len(westDiamonds)>0:
westCard = westDiamonds.pop(0)
elif suit == "clubs" and len(westClubs)>0:
westCard = westClubs.pop(0)
elif trumps == "spades" and len(westSpades)>0:
westCard = westSpades.pop(0)
elif trumps == "hearts" and len(westHearts):
westCard = westHearts.pop(0)
elif trumps == "diamonds" and len(westDiamonds)>0:
westCard = westDiamonds.pop(0)
elif trumps == "clubs" and len(westClubs)>0:
westCard = westClubs.pop(0)
elif len(westSpades) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westSpades.pop()
elif len(westHearts) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westHearts.pop()
elif len(westDiamonds) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westDiamonds.pop()
else:
westCard = westClubs.pop()
print "West: ", westCard
elif leader == "east": #and the above is the general scheme of how a trick works, but here follow cases for different leaders
#east plays a card
if trumps == "spades":
if len(eastHearts) == nonzeromin([len(eastHearts), len(eastDiamonds), len(eastClubs)]):#play the highest card in shortest non-trump suit
ledCard = eastHearts.pop(0)
elif len(eastDiamonds)== nonzeromin([len(eastHearts), len(eastDiamonds), len(eastClubs)]):
ledCard = eastDiamonds.pop(0)
elif len(eastClubs) == nonzeromin([len(eastHearts), len(eastDiamonds), len(eastClubs)]):
ledCard = eastClubs.pop(0)
else:
ledCard = eastSpades.pop(0)
elif trumps == "hearts":
if len(eastSpades) == nonzeromin([len(eastSpades), len(eastDiamonds), len(eastClubs)]):
ledCard = eastSpades.pop(0)
elif len(eastDiamonds) == nonzeromin([len(eastSpades), len(eastDiamonds), len(eastClubs)]):
ledCard = eastDiamonds.pop(0)
elif len(eastClubs) == nonzeromin([len(eastSpades), len(eastDiamonds), len(eastClubs)]):
ledCard = eastClubs.pop(0)
else:
ledCard = eastHearts.pop(0)
elif trumps == "diamonds":
if len(eastSpades)== nonzeromin([len(eastSpades), len(eastHearts), len(eastClubs)]):
ledCard = eastSpades.pop(0)
elif len(eastHearts) == nonzeromin([len(eastSpades), len(eastHearts), len(eastClubs)]):
ledCard = eastHearts.pop(0)
elif len(eastClubs) == nonzeromin([len(eastSpades), len(eastHearts), len(eastClubs)]):
ledCard = eastClubs.pop(0)
else:
ledCard = eastDiamonds.pop(0)
else:
if len(eastSpades) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds)]):
ledCard = eastSpades.pop(0)
elif len(eastHearts) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds)]):
ledCard = eastHearts.pop(0)
elif len(eastDiamonds) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds)]):
ledCard = eastDiamonds.pop(0)
else:
ledCard = eastClubs.pop(0)
suit = ledCard[1]
print "East: ", ledCard
eastCard = ledCard
#south (human) plays a card
southCard = input("Which card will you play?") #human player
print "South: ", southCard
if southCard in southSpades:
southSpades.remove(southCard)
elif southCard in southHearts:
southHearts.remove(southCard)
elif southCard in southDiamonds:
southDiamonds.remove(southCard)
else:
southClubs.remove(southCard)
#west plays a card
if suit == "spades" and len(westSpades)>0:#length of suit for west > 0:
westCard = westSpades.pop(0)
elif suit == "hearts" and len(westHearts)>0:
westCard = westHearts.pop(0)
elif suit == "diamonds" and len (westDiamonds)>0:
westCard = westDiamonds.pop(0)
elif suit == "clubs" and len(westClubs)>0:
westCard = westClubs.pop(0)
elif trumps == "spades" and len(westSpades)>0:
westCard = westSpades.pop(0)
elif trumps == "hearts" and len(westHearts)>0:
westCard = westHearts.pop(0)
elif trumps == "diamonds" and len(westHearts)>0:
westCard = westDiamonds.pop(0)
elif trumps == "clubs" and len(westClubs)>0:
westCard = westClubs.pop(0)
elif len(westSpades) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westSpades.pop()
elif len(westHearts) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westHearts.pop()
elif len(westDiamonds) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westDiamonds.pop()
else:
westCard = westClubs.pop()
print "West: ", westCard
#north plays a card
if suit == "spades" and len(northSpades)>0:
northCard = northSpades.pop(0)
elif suit == "hearts" and len(northHearts)>0:
northCard = northHearts.pop(0)
elif suit == "diamonds" and len(northDiamonds)>0:
northCard = northDiamonds.pop(0)
elif suit == "clubs" and len(northClubs)>0:
northCard = northClubs.pop(0)
elif trumps == "spades" and len(northSpades)>0:
northCard = northSpades.pop(0)
elif trumps == "hearts" and len(northHearts)>0:
northCard = northHearts.pop(0)
elif trumps == "diamonds" and len(northDiamonds)>0:
northCard = northDiamonds.pop(0)
elif trumps == "clubs" and len(northClubs)>0:
northCard = northClubs.pop(0)
elif len(northSpades) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northSpades.pop()
elif len(northHearts) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northHearts.pop()
elif len(northDiamonds) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northDiamonds.pop()
else:
northCard = northClubs.pop()
print "North: ", northCard
elif leader == "south":
#south (human) plays a card
ledCard = input("Which card will you play? ")
suit = ledCard[1]
print "South: ", ledCard
if ledCard[1] == "spades":
southSpades.remove(ledCard)
elif ledCard[1] == "hearts":
southHearts.remove(ledCard)
elif ledCard[1] == "diamonds":
southDiamonds.remove(ledCard)
else:
southClubs.remove(ledCard)
southCard = ledCard
#west plays a card
if suit == "spades" and len(westSpades)>0:
westCard = westSpades.pop(0)
elif suit == "hearts" and len(westHearts):
westCard = westHearts.pop(0)
elif suit == "diamonds" and len(westDiamonds)>0:
westCard = westDiamonds.pop(0)
elif suit == "clubs" and len(westClubs)>0:
westCard = westClubs.pop(0)
elif trumps == "spades" and len(westSpades)>0:
westCard = westSpades.pop(0)
elif trumps == "hearts" and len(westHearts)>0:
westCard = westHearts.pop(0)
elif trumps == "diamonds" and len(westDiamonds)>0:
westCard = westDiamonds.pop(0)
elif trumps == "clubs" and len(westClubs)>0:
westCard = westClubs.pop(0)
elif len(westSpades) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westSpades.pop()
elif len(westHearts) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westHearts.pop()
elif len(westDiamonds) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds), len(westClubs)]):
westCard = westDiamonds.pop()
else:
westCard = westClubs.pop()
print "West: ", westCard
#north plays a card
if suit == "spades":
if len(northSpades)>0:
northCard = northSpades.pop(0)
elif suit == "hearts":
if len(northHearts)>0:
northCard = northHearts.pop(0)
elif suit == "diamonds":
if len(northDiamonds)>0:
northCard = northDiamonds.pop(0)
elif suit == "clubs":
if len(northClubs)>0:
northCard = northClubs.pop(0)
elif trumps == "spades":
if len(northSpades)>0:
northCard = northSpades.pop(0)
elif trumps == "hearts":
if len(northHearts)>0:
northCard = northHearts.pop(0)
elif trumps == "diamonds":
if len(northDiamonds)>0:
northCard = northDiamonds.pop(0)
elif trumps == "clubs":
if len(northClubs)>0:
northCard = northClubs.pop(0)
elif len(northSpades) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northSpades.pop()
elif len(northHearts) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northHearts.pop()
elif len(northDiamonds) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northDiamonds.pop()
else:
northCard = northClubs.pop()
print "North: ", northCard
#east plays a card
if suit == "spades" and len(eastSpades)>0:
eastCard = eastSpades.pop(0)
elif suit == "hearts" and len(eastHearts)>0:
eastCard = eastHearts.pop(0)
elif suit == "diamonds" and len(eastDiamonds)>0:
eastCard = eastDiamonds.pop(0)
elif suit == "clubs" and len(eastClubs)>0:
eastCard = eastClubs.pop(0)
elif trumps == "spades" and len(eastSpades)>0:
eastCard = eastSpades.pop(0)
elif trumps == "hearts" and len(eastHearts)>0:
eastCard = eastHearts.pop(0)
elif trumps == "diamonds" and len(eastDiamonds)>0:
eastCard = eastDiamonds.pop(0)
elif trumps == "clubs" and len(eastClubs)>0:
eastCard = eastClubs.pop(0)
elif len(eastSpades) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastSpades.pop()
elif len(eastHearts) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastHearts.pop()
elif len(eastDiamonds) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastDiamonds.pop()
else:
eastCard = eastClubs.pop()
print "East: ", eastCard
elif leader == "west":
#west plays a card
if trumps == "spades":
if len(westHearts) == nonzeromin([len(westHearts), len(westDiamonds), len(westClubs)]):
ledCard = westHearts.pop(0)
elif len(westDiamonds)== nonzeromin([len(westHearts), len(westDiamonds), len(westClubs)]):
ledCard = westDiamonds.pop(0)
elif len(westClubs) == nonzeromin([len(westHearts), len(westDiamonds), len(westClubs)]):
ledCard = westClubs.pop(0)
else:
ledCard = westSpades.pop(0)
elif trumps == "hearts":
if len(westSpades) == nonzeromin([len(westSpades), len(westDiamonds), len(westClubs)]):
ledCard = westSpades.pop(0)
elif len(westDiamonds) == nonzeromin([len(westSpades), len(westDiamonds), len(westClubs)]):
ledCard = westDiamonds.pop(0)
elif len(westClubs) == nonzeromin([len(westSpades), len(westDiamonds), len(westClubs)]):
ledCard = westClubs.pop(0)
else:
ledCard = westHearts.pop(0)
elif trumps == "diamonds":
if len(westSpades)== nonzeromin([len(westSpades), len(westHearts), len(westClubs)]):
ledCard = westSpades.pop(0)
elif len(westHearts) == nonzeromin([len(westSpades), len(westHearts), len(westClubs)]):
ledCard = westHearts.pop(0)
elif len(westClubs) == nonzeromin([len(westSpades), len(westHearts), len(westClubs)]):
ledCard = westClubs.pop(0)
else:
ledCard = westDiamonds.pop(0)
else:
if len(westSpades) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds)]):
ledCard = westSpades.pop(0)
elif len(westHearts) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds)]):
ledCard = westHearts.pop(0)
elif len(westDiamonds) == nonzeromin([len(westSpades), len(westHearts), len(westDiamonds)]):
ledCard = westDiamonds.pop(0)
else:
ledCard = westClubs.pop(0)
suit = ledCard[1]
print "West: ", ledCard
westCard = ledCard
#north plays a card
if suit == "spades" and len(northSpades)>0:
northCard = northSpades.pop(0)
elif suit == "hearts" and len(northHearts)>0:
northCard = northHearts.pop(0)
elif suit == "diamonds" and len(northDiamonds)>0:
northCard = northDiamonds.pop(0)
elif suit == "clubs" and len(northClubs)>0:
northCard = northClubs.pop(0)
elif trumps == "spades" and len(northSpades)>0:
northCard = northSpades.pop(0)
elif trumps == "hearts" and len(northHearts)>0:
northCard = northHearts.pop(0)
elif trumps == "diamonds" and len(northDiamonds)>0:
northCard = northDiamonds.pop(0)
elif trumps == "clubs" and len(northClubs)>0:
northCard = northClubs.pop(0)
elif len(northSpades) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northSpades.pop()
elif len(northHearts) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northHearts.pop()
elif len(northDiamonds) == nonzeromin([len(northSpades), len(northHearts), len(northDiamonds), len(northClubs)]):
northCard = northDiamonds.pop()
else:
northCard = northClubs.pop()
print "North: ", northCard
#east plays a card
if suit == "spades" and len(eastSpades)>0:
eastCard = eastSpades.pop(0)
elif suit == "hearts" and len(eastHearts)>0:
eastCard = eastHearts.pop(0)
elif suit == "diamonds" and len(eastDiamonds)>0:
eastCard = eastDiamonds.pop(0)
elif suit == "clubs" and len(eastClubs)>0:
eastCard = eastClubs.pop(0)
elif trumps == "spades" and len(eastSpades)>0:
eastCard = eastSpades.pop(0)
elif trumps == "hearts" and len(eastHearts)>0:
eastCard = eastHearts.pop(0)
elif trumps == "diamonds" and len(eastDiamonds)>0:
eastCard = eastDiamonds.pop(0)
elif trumps == "clubs" and len(eastClubs)>0:
eastCard = eastClubs.pop(0)
elif len(eastSpades) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastSpades.pop()
elif len(eastHearts) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastHearts.pop()
elif len(eastDiamonds) == nonzeromin([len(eastSpades), len(eastHearts), len(eastDiamonds), len(eastClubs)]):
eastCard = eastDiamonds.pop()
else:
eastCard = eastClubs.pop()
print "East: ", eastCard
#south (human) plays a card
southCard = input("Which card will you play?") #human player
print "South: ", southCard
if southCard in southSpades:
southSpades.remove(southCard)
elif southCard in southHearts:
southHearts.remove(southCard)
elif southCard in southDiamonds:
southDiamonds.remove(southCard)
else:
southClubs.remove(southCard)
playedCards = [northCard, eastCard, southCard, westCard] #the cards that have been played this trick
trumpCards = [] #will hold any trump cards
compareCards = [] #will hold any suit cards
for i in playedCards: #for every card played this trick
if i[1] == trumps:#if it's a trump card
trumpCards.append(i)#put it with the trump cards
elif i[1] == suit:#if it follows suit
compareCards.append(i)#put it with the cards in that suit
trumpCards.sort(reverse=True)#sort highest first
compareCards.sort(reverse=True)#ditto
if len(trumpCards)>0: #if a trump card has been played
winner = trumpCards.pop(0)#winner is the highest trump card
else:#otherwise
winner = compareCards.pop(0)#winner is the highest card in suit
return winner
def playHand(): #plays one hand of whist
hand = deal()#deal cards
northHand = hand[0]#the hands of the players
eastHand = hand[1]
southHand = hand[2]
westHand = hand[3]
global northSpades
global northHearts
global northDiamonds
global northClubs
global eastSpades
global eastHearts
global eastDiamonds
global eastClubs
global southSpades
global southHearts
global southDiamonds
global southClubs
global westSpades
global westHearts
global westDiamonds
global westClubs
northSpades = northHand[0]
northHearts = northHand[1]
northDiamonds = northHand[2]
northClubs = northHand[3]
eastSpades = eastHand[0]
eastHearts = eastHand[1]
eastDiamonds = eastHand[2]
eastClubs = eastHand[3]
southSpades = southHand[0]
southHearts = southHand[1]
southDiamonds = southHand[2]
southClubs = southHand[3]
westSpades = westHand[0]
westHearts = westHand[1]
westDiamonds = westHand[2]
westClubs = westHand[3]
global trumps
trumpCount = handCount % 4 #trumps cycles handwise in bridge order
if trumpCount == 0:
trumps = "spades"
elif trumpCount == 1:
trumps = "hearts"
elif trumpCount == 2:
trumps = "diamonds"
elif trumpCount == 3:
trumps = "clubs"
leaderCount = handCount % 4 #leader for 1st trick
if leaderCount == 0:
leader = "north"
elif leaderCount == 1:
leader = "east"
elif leaderCount == 2:
leader = "south"
elif leaderCount == 3:
leader = "west"
NorthSouthTricks = 0 #counter for how many tricks north/south partnership has taken
EastWestTricks = 0#counter for how many tricks east/west partnership has taken
print "Trumps is ", trumps #declaration of trumps for hand
for i in range(13):#thirteen tricks in a hand (at the end of the hand, players' hands will be empty [all cards will be played])
print "~~~~~" #visual dividing for the benefit of the human
print "start of trick"
print southHand[0]+southHand[1]+southHand[2]+southHand[3] #what cards are currently in the human's hand
winner = trick(leader) #play the trick, who wins the trick?
if winner == northCard: #north wins the trick
NorthSouthTricks += 1#plus 1 to the partnership's counter
leader = "north" #north leads the next trick
print "north takes the trick" #for the human's benefit, to see who took the trick
elif winner == eastCard: #ditto for east
EastWestTricks +=1
leader = "east"
print "east takes the trick"
elif winner == southCard: #ditto for south
NorthSouthTricks +=1
leader = "south"
print "south takes the trick"
elif winner == westCard:#ditto for west
EastWestTricks +=1
leader = "west"
print "west takes the trick"
#leader leads a card
#play goes clockwise: NESW, ESWN, SWNE, WNES
#spades are Hand[0], hearts are Hand[1], diamonds are Hand[2], clubs are Hand[3]
#card led[1] becomes suit
#play highest card in suit
#if void in suit, play highest trump
#if void in trump, play lowest in shortest other suit
#highest trump wins trick
#if no trump played, highest card in suit wins trick
#+1 to partnership score for winner of trick
#winner of trick becomes leader
#human player is south, partnered with north
return NorthSouthTricks, EastWestTricks #at the end of the hand, how many tricks did each partnership take?
def main(): #play a full game of whist
NorthSouthScore = 0 #counter for north/south score
EastWestScore = 0 #counter for east/west score
print "We are now going to play whist." #instructions for game
print "You are South. Type a card exactly as it appears to play it." #please don't break the program, human player!
print "14 = Ace, 13 = King, 12 = Queen, 11 = Jack" #since we're dealing with numeric rank rather than printing the face cards (and ace is high)
while (NorthSouthScore < 5 and EastWestScore <5): #while neither partnership has scored 5
print "~~~~~~~~~" #visual divider
print "Start of new hand."
score = playHand() #play the hand
if score[0] > score[1]: #if north/south has taken more tricks than east/west
NorthSouthScore += (score[0]-6) #add the value of the tricks minus six to their score
else:
EastWestScore += (score[1]-6) #ditto, except for east/west
print "Hand score: North/South: ", score[0], "East/West: ", score[1] #how many tricks everyone took
print "Score: North/South: ", NorthSouthScore, ", East/West: ", EastWestScore#how the game score is doing
global handCount
handCount+=1 #plus 1 to the hand count
if NorthSouthScore > 5 and EastWestScore != NorthSouthScore: #north/south broke 5 and the game's not tied
print "Yay, you won!"
elif EastWestScore > 5 and NorthSouthScore != EastWestScore: #east/west broke 5 and the game's not tied
print "Sorry, East/West won."
else:
print "Yay, you tied!"
print "Thank you for playing."
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.