DEV Community

Cover image for Let's Get Clever #2: Spades
Jason C. McDonald
Jason C. McDonald

Posted on

Let's Get Clever #2: Spades

We spend a lot of time trying to write good code. I don't know about you, but I get weary of hearing the (apt) admonishment "don't be clever".

Everyone needs a break. So, in celebration of all our diverse skills and knowledge, Let's Get Clever!

Challenge #2

Determine the best card to play for a trick in spades.

Spades is a card game played partners, where in your team must capture the most Tricks without going over your Bid (the number of tricks you declared at the start of the round that you'd try to capture).

  • You can see all 1 to 13 cards in your hand at a time. You must play one each trick.

  • You must match the leading suit (the suit of the card first played in the trick) if you can. If you cannot match the suit, you may play any card.

  • The highest spade played wins the trick, or else the highest card in the leading suit.

  • Aces high. (Ace beats any other value in its suit.)

  • If either you or your partner takes the trick, your team gets credit for it.

  • Strategy tip: if you have no chance of winning the trick, or if your partner already has, you should take the occasion to get rid of bad cards.

(Spades players: yes, I am skipping over the rules about leading with spades, as it doesn't fit into this challenge.)

In short, if you can win the trick, output the weakest possible card from your hand that would allow you to do that. If you cannot win the trick (or if your partner already has), get rid of a bad card.

Input/Output

Cards are represented by two-character strings. The first character is the value (A, 2-9, X (10), J, Q, K), and the second character is the suit (H, S, C, D).

  1. You'll receive the three cards played so far as a space-delimited string.
  2. Your own hand will be specified first by an integer on a line telling you how many cards you have.
  3. Your hand will be specified by a space-deliminted string.

Example #1:

QH 8D 2S
5
KD 7D 2H 9S 8S
Enter fullscreen mode Exit fullscreen mode

The best card played so far is 2S, which your opponent put down. You can win the trick with a spade, and you have two: 9S and 8S. The weakest of these two that can still beat 2S is 8S, so you should play that.

Output: 8S

Example #2:

9D QD 3H
5
KD 7D 2H 9S 8S
Enter fullscreen mode Exit fullscreen mode

Your partner has already won the trick, as QD is the best played so far. There's no need for you to throw away a good card, so you can take the opportunity to get rid of your weak 2H.

Output: 2H

Example #3:

XS 3H 7S
5
KD 7D 2H 9S 8S
Enter fullscreen mode Exit fullscreen mode

Your opponent played XS, which is the best card so far. You have nothing to beat it, so you should (again) take the occasion to throw away your weakest card, 2H.

Constraints

  • Assume you're going last. You can see all three of the cards played by the other players. The middle one was played by your partner.

  • Assume your team wants to take the trick if possible.

  • You'll always receive 1-13 cards.

Bonus Points

These are NOT required, but if you achieve any of these goals, major bragging rights!

  • ⏱️ Efficiency: Can you achieve O(n) or better for the decision-making code? (Efficiency goal doesn't include parsing input data...as long as no part of the parsing directly informs the decision-making algorithm.)

  • No If-Statement: Can you write a solution that doesn't use an explicit if or switch statement? (I don't even know if this is possible. Let's find out!)

The Rules

(These are the same for every Let's Be Clever challenge.)

  1. Your clever code should accomplish the stated goal in whatever way you like. Watch out for edge cases, though.

  2. You may use any language you like. However, I discourage use of deliberately esoteric languages. Cleverness is usually more fun in production languages.

  3. You know all those rules about readability and maintainability? Fuhgeddaboudit. Cleverness is the goal.

  4. Portability, memory safety, security? Bah humbug. Mel is welcome.

  5. Efficiency is not the main goal, but if your cleverness happens to have an efficiency worth bragging about, definitely point it out!

  6. Explain your Cleverness. Don't wait for others to ask you, although you should be prepared to answer questions. The neat side-effect of Clever code is that everyone can learn cool, weird things.

  7. Be open to feedback. Others might be able to spot edge-cases you missed, or have ideas on how to make your Clever approach even more clever.

  8. Preening is permitted, but don't be obnoxious about it, and be equally liberal with your complements of others. Leave false modesty in the drawer. (Imposter Syndrome sucks, so let's use this to give it a good kick in the teeth.)

  9. Upvote Cleverness! If you really like someone's solution, be sure to give it a Like.

  10. Always be polite and constructive. Please don't criticize anyone for not being clever enough, not understanding what you did, or asking a "stupid question". This is Cleverness on display. Be the tour guide!

Don't forget: CODE HACKERY ENCOURAGED! No holds barred.

Top comments (0)