Fall 2013, David Matuszek
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.
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.
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:
deal method in Bid.scala is inadequate for dealing more than one hand.) "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.Next, thirteen tricks are played. Each trick proceeds as follows:
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.
After the cards are shuffled and dealt:
| Cards played | Result |
|---|---|
|
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.
object BridgeGame
var trumpSuit: Stringdef main(args: Array[String])
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. |
class Card(val value: Int, val suit: String)
object CardDeck
cards variable, as in Bid.scala.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.
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 { |
class Player(val Name name: String)
val hand: Hand to hold cardsdef 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 def play(suitLed: String): Card "plays" a card by removing it from this Player's Hand and returning it.
suitLed should be one of the four suits, or "lead"."lead" means this player leads the trick, and can play any randomly chosen card.BridgeGame object which suit is trump); else 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.
.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.