CIT 591 Assignment 4: Play a Hand of Bridge
Fall 2013, David Matuszek

# Purposes

• To give you experience with classes and objects

# The General Idea

Play a hand of bridge.

The object of this assignment is to play legally, that is, according to the rules of bridge. Playing well, and attempting to win the game, is outside the scope of this assignment.

## Very short summary

There are four players. Each player gets 13 cards. A suit and a player are chosen (randomly). The chosen player plays the first card, and each of the other players then plays a card (of the same suit, if possible). The player who played the highest card wins the "trick" (the four cards just played) and plays the first card of the next trick.

The assignment: Shuffle and deal the cards, then play the hand (13 tricks). Follow the rules for what cards may be played, but don't try to implement any strategy or any scoring.

# The rules

Cards and the card deck are as described in the previous assignment. In fact, the `Card` class (as given in Bid.scala) can be used without any changes.

There are four players, whom we will call `"North"`, `"East"`, `"South"` and `"West"`.

Here's how it goes:

1. One of the players is chosen as the dealer. It doesn't really matter which one; every player should have the ability (a method) to deal.
2. The dealer shuffles the deck, then deals the cards; that is, she gives 13 cards to each player, including herself. The 13 cards held by a player are that player's hand. (Note: the `deal` method in Bid.scala is inadequate for dealing more than one hand.)
3. A trump suit is chosen: One of` "Clubs"`,` "Diamonds"`,` "Hearts"`,` "Spades`", and `"No Trump"`. "No trump" means just what it sounds like: No suit is trump. This suit will remain trump until the entire hand has been played.
• In an actual bridge game, the trump suit would be chosen by bidding, but we'll skip over that and just choose one randomly.
4. One of the players is chosen (again, randomly) to "lead," that is, to play the very first card.

Next, thirteen tricks are played. Each trick proceeds as follows:

1. The player chosen to lead plays the first card.
• "Playing a card" means removing it from her hand and letting the other players know which card it is. (In the real game, the card is placed face up on the table.) It is important to notice what the suit of this card is.
• In this assignment, any card may be played as the first card of a trick (the players have no strategy or any idea of a "good" card to play).
2. Proceeding clockwise (N → E → S → W → N), each of the remaining players then plays a card.
• If a player has any cards of the same suit as the first card played, then she must follow suit (play a card of the same suit).
• If she has no cards of the same suit, but has cards in the trump suit, she should play one of those cards. This is called trumping. Cards in the trump suit are "higher" than any card in a non-trump suit.
• Otherwise she may play (discard) any card.
3. When all four players have played a card, the player who played the highest card "takes the trick".
• If one or more trump cards were played, the highest card in the trump suit takes the trick.
• Otherwise, the highest card in the suit led takes the trick.
4. The player who took the trick is the player that will lead the next trick.

After doing the above 13 times, all cards have been played and the hand ends.

There is no strategy to this game, it isn't scored, nobody wins or loses.

## Partial Example (one trick)

After the cards are shuffled and dealt:

Cards played Result
1. North plays the 5 of Hearts
2. East plays the 8 of Hearts
3. South (who has no Hearts) plays the Queen of Diamonds
4. West plays the 4 of Hearts
If the trump suit is anything but Diamonds, East wins the trick.
If the trump suit is Diamonds, South wins the trick.

Your program should detrmine who won the trick, but it does not have to keep track of all the tricks.

## Create these classes and objects:

• `object BridgeGame`
• `var trumpSuit: String`
• `def main(args: Array[String]) `
• Creates the four Player objects
• Randomly chooses a suit to be trump (print out what it is)
• Randomly chooses a player to "lead" the first trick
• For each of 13 tricks,
• Tell each player to play, in the correct order (clockwise)
• Determine who won the trick (and will lead the next)
• Print out how the trick was played, like this:  ```Trick # 2 South leads with the 8 of Clubs West plays the 7 of Clubs North plays the 10 of Clubs East plays the Queen of Diamonds North takes the trick.```
• All printing should be done from this method
• ```class Card(val value: Int, val suit: String) ```
• Use the class as given in Bid.scala.
• Feel free to add methods (if you can think of something else an individual Card should do).
• `object CardDeck`
• Creates the cards and saves them in a `cards` variable, as in Bid.scala.
• Does not have a `deal` method.
• `class Hand`
• `val cards: Set[Card] `holds the cards for a given Player.
• `def add(card: Card): Unit` adds a Card to this Hand.
• `def remove(card: Card): Unit` removes a Card from this Hand.
• Hint: Look up `Set` in the Scala API to find out how to add and remove values from a Set. The operations you need are near the top of the list.
• `def select(suit: String): Option[Card]` looks for a Card of the given suit in this Hand. If found, return `Some(card)`; if not found, return `None`. You can use this method as follows:  ```select("Clubs") match {   case Some(card) => // do something with the variable "card"   case None => // do something else }```
• Feel free to add methods (if you can think of something else a Hand should do).
• `class Player(val Name name: String)`
• `val hand: Hand` to hold cards
• `def deal(players: List[Player])` gets cards from the CardDeck, shuffles them, and deals 13 cards to each of the four Players.
• `def take(card:Card): Unit `receives a hand Card (from the dealer) and adds it to this Player's Hand.
• `def play(suitLed: String): Card `"plays" a card by removing it from this Player's Hand and returning it.
• The `suitLed` should be one of the four suits, or `"lead"`.
• `"lead" `means this player leads the trick, and can play any randomly chosen card.
• follow suit, if possible; else
• play a trump, if possible (ask the `BridgeGame` object which suit is trump); else
• play any card.
• Feel free to add methods (if you can think of something else an individual Player might do.)

We will be running tests on all the methods mentioned above, except `main`, which we will test "manually." Therefore, be sure that all your methods are in the right places, spelled and capitalized as shown, and have the correct parameter types and return types.

Please name your file `bridge_hand.scala`.

# Due date

Submit your team's program, as a single `.scala` file, to Canvas by 6am next Friday, September 27. Decide which of you is going to turn in the assignment, and make only one submission for the two of you. Make sure both of your names are in comments at the top of each file.