Skip to content

A6 - Three card "Poker" hands

Run the update, and try out the program for assignment #6. It is a variation on poker, but using only three cards rather than 5. Please note that you can click anywhere inside the canvas to deal a new set of cards.

When the program is done, it should indicate the best poker hand the player holds, as defined below:

  1. (Worst) - High card - only used if none of the below are found
  2. Pair - two cards of the same value
  3. Triple - three cards of the same value
  4. Straight - three cards in a row (eg, 9, 10, Jack)
  5. Flush - three cards, all the same suit
  6. (Best) - Straight Flush - meets the conditions of both a straight and a flush

Numbers 1 and 2 are already implemented. Your task will be to complete the remaining ones.

Take some time to go through the code to get an understanding of it. Please note that it uses a Card object, defined within the file cards.js. If you haven't used objects before or don't remember, this one is pretty intuitive and you should be able to pretty much figure it out by looking at the code. Here's what you need to know about it:

  • A Card object contains three attributes you can use:
    • value - The number (or rank) of the card, from 2 up through Ace. Note that these are all stored as strings, meaning numbers are quoted.
    • suit - Either Hearts, Diamonds, Clubs or Spades
    • strength - The relative stregth of the cards in a poker hand, as shown in the box below.

Card Strengths (value : strength)

  • 2 : 0
  • 3 : 1
  • 4 : 2
  • 5 : 3
  • 6 : 4
  • 7 : 5
  • 8 : 6
  • 9 : 7
  • 10 : 8
  • Jack : 9
  • Queen : 10
  • King : 11
  • Ace : 12

Hints

  • Triples - This should be reasonably simple. Check to see if the values of all cards match.

  • Straights - You have been given a line of advanced JavaScript code that will copy the cards, in ascending order by strength, into variables a, b, and c. This leaves you with the task of checking if b is one level of strength above a, and c is one level of strength above b.

  • Flushes - Determining if a flush exists should be pretty simple. However, if one does, you need to determine the highest value card in the hand so that the value can be returned. There are a couple strategies you can use here, which I will leave to you to figure out.

  • Straight Flush - Notice there is no function for this. That's because you can use ones you already have available to check for this.