cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              .container
  .row
    .col-md-12
      .panel.panel-default
        .panel-heading
          Game Results (
          %a#newGame{ :href => '#' }
            play again
          )
        %table.table
          %tbody#hands

#mocha
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Open+Sans);

body {
  margin-top: 60px;
}

#hands {
  background-color: #23A484;
  color: #fff;
  font-weight: bold;
}

#hands > tr.success > td {
  background-color: #48cfad;
}

.card {
  color: #333;
  font-family: 'Open Sans', sans-serif;
  display: table;
  border-radius: 4.8px;
  background-color: #fff;
  box-sizing: border-box;
  box-shadow: 1px 1px 0 rgba(0, 0, 0, 0.2);
  height: 41px;
  width: 28px;
  margin: 0 auto;
}

.card__rank {
  display: table-cell;
  font-size: 16.272px;
  line-height: 16.272px;
  text-align: center;
  vertical-align: middle;
  font-weight: bold;
}

.card__rank:before {
  box-sizing: border-box;
  width: 100%;
  height: 15px;
  text-align: center;
  display: block;
}

.card--H,
.card--D {
  color: #fc6e51;
}

.card--H .card__rank:before {
  content: '\2665';
}

.card--D .card__rank:before {
  content: '\2666';
}

.card--S .card__rank:before {
  content: '\2660';
}

.card--C .card__rank:before {
  content: '\2663';
}

            
          
!
            
              class PokerCheck
  constructor: (@rules = {}) ->
    @rankLookup = {}
    @rankLookup[key] = index for key, index in [2..9].concat 'TJQKA'.split ''

  evaluateGame: (hands) ->
    winner = @evaluateHand hands[0]
    winnerIndex = 0
    for index in [1...hands.length]
      hand = hands[index]
      result = @evaluateHand hand
      if result.handRank > winner.handRank
        winnerIndex = index
        winner = result
    [winnerIndex, winner.handName]

  evaluateHand: (hand) ->
    cards = hand.split ' '
    histogram = @createHistogram cards
    index = 0
    size = Object.keys(@rules).length
    width = Object.keys(@rankLookup).length
    maxIndex = Object.keys(@rules).length - 1
    for handName, ruleFn of @rules
      if handRank = ruleFn histogram
        handRank = (maxIndex - index) * width + handRank
        return {handName, handRank}
      index += 1

  createHistogram: (cards) ->
    suits = {}
    ranks = {}
    for card in cards
      [rank, suit] = card.split ''
      suits[suit] or= {}
      suits[suit][@rankLookup[rank]] or= 0
      suits[suit][@rankLookup[rank]] += 1
      ranks[@rankLookup[rank]] or= 0
      ranks[@rankLookup[rank]] += 1
    rankCount = Object.keys(ranks).length
    suitCount = Object.keys(suits).length
    {ranks, suits, rankCount, suitCount}

count = (obj) ->
  ret = {}
  ret[handRank] = Math.max (ret[handRank] or 0), key for key, handRank of obj
  ret

pokerRules =
  'straight flush': (histogram) -> # 5 ranks in sequence, uniform suit
    isStraight = pokerRules['straight'] histogram
    isFlush = pokerRules['flush'] histogram
    return 0 unless isStraight and isFlush
    Math.max.apply [], Object.keys histogram.ranks

  'four of a kind': (histogram) -> # 4 of a rank
    return 0 if histogram.rankCount > 2
    counts = count histogram.ranks
    counts[4] or 0

  'full house': (histogram) -> # 3 of a rank, 2 of a rank
    return 0 if histogram.rankCount > 2
    counts = count histogram.ranks
    return 0 unless counts[3]
    return 0 unless counts[2]
    counts[3]

  'flush': (histogram) -> # 5 of a suit
    return 0 unless histogram.suitCount is 1
    Math.max.apply [], Object.keys histogram.ranks

  'straight': (histogram) -> # 5 ranks in sequence
    return 0 unless histogram.rankCount is 5
    max = Math.max.apply [], Object.keys histogram.ranks
    min = Math.min.apply [], Object.keys histogram.ranks
    if max - min is 4
      return max
    0

  'three of a kind': (histogram) -> # 3 of a rank
    count(histogram.ranks)[3] or 0

  'two pair': (histogram) -> # 2 of a rank, 2 of a rank
    return if histogram.rankCount > 3
    counts = count histogram.ranks
    if counts[2] and counts[1] then +counts[2] else 0

  'one pair': (histogram) -> # 2 of a rank
    counts = count histogram.ranks
    return 0 unless counts[2]
    +counts[2]

  'high card': (histogram) -> # default
    Math.max.apply [], Object.keys histogram.ranks

mocha.setup 'bdd'
expect = chai.expect

describe 'PokerCheck', ->
  poker = undefined

  beforeEach ->
    poker = new PokerCheck pokerRules

  describe 'core', ->
    it 'evaluates hand', ->
      result = poker.evaluateHand '8H 7H 6H 5H 4H'
      expect(result).to.deep.equal {handName: 'straight flush', handRank: 110}

    it 'evaluates game', ->
      [index, handName] = poker.evaluateGame ['8H 7H 6H 5H 4H', 'AD TD 9S 5C 4C']
      expect(index).to.equal 0
      expect(handName).to.equal 'straight flush'
    
    it 'supports many players per game', ->
      hands = [
        'TH TD TC 4C 4H'
        'AD TD 9S 5C 4C'
        '8H 7H 6H 5H 4H'
        'AD 8C 6S TC 5H'
      ]
      [index, handName] = poker.evaluateGame hands
      expect(index).to.equal 2

  describe 'rules', ->
    describe 'straight flush', ->
      it 'basic', ->
        {handName} = poker.evaluateHand '8H 7H 6H 5H 4H'
        expect(handName).to.equal 'straight flush'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['8H 7H 6H 5H 4H', '6S 5S 4S 3S 2S']
        expect(index).to.equal 0

    describe 'four of a kind', ->
      it 'basic', ->
        {handName} = poker.evaluateHand '5S 5D 5C 5H AD'
        expect(handName).to.equal 'four of a kind'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['TC TD TH TS QD', '6D 6H 6S 6C 7S']
        expect(index).to.equal 0

    describe 'full house', ->
      it 'basic', ->
        {handName} = poker.evaluateHand 'TH TD TC 4C 4H'
        expect(handName).to.equal 'full house'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['TC TH TD 4S 4D', '9H 9C 9S AH AC']
        expect(index).to.equal 0

    describe 'flush', ->
      it 'basic', ->
        {handName} = poker.evaluateHand '5D 4D 3D 2D AD'
        expect(handName).to.equal 'flush'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['AH QH TH 5H 3H', 'KS QS JS 9S 6S']
        expect(index).to.equal 0

    describe 'straight', ->
      it 'basic', ->
        {handName} = poker.evaluateHand '5C 4D 3S 2S 6H'
        expect(handName).to.equal 'straight'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['8S 7S 6H 5H 4S', '6D 5S 4D 3H 2C']
        expect(index).to.equal 0

    describe 'three of a kind', ->
      it 'basic', ->
        {handName} = poker.evaluateHand 'AD 8C 8S 8C 5H'
        expect(handName).to.equal 'three of a kind'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['QS QC QD 5S 3C', '5C 5H 5D QD TC']
        expect(index).to.equal 0

    describe 'two pair', ->
      it 'basic', ->
        {handName} = poker.evaluateHand 'TH TD 2C 2S 4D'
        expect(handName).to.equal 'two pair'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['KH KD 2C 2D JH', 'JD JS TS TC 9S']
        expect(index).to.equal 0

    describe 'one pair', ->
      it 'basic', ->
        {handName} = poker.evaluateHand 'TC TH 3D 8C 7S'
        expect(handName).to.equal 'one pair'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['TC TS 6S 4H 2H', '9H 9C AH QD TD']
        expect(index).to.equal 0

    describe 'high card', ->
      it 'basic', ->
        {handName} = poker.evaluateHand 'AD 8C 6S TC 5H'
        expect(handName).to.equal 'high card'

      it 'tie breaker', ->
        [index] = poker.evaluateGame ['AD TD 9S 5C 4C', 'KS QD JS 8H 7H']
        expect(index).to.equal 0

mocha.run()

deck = undefined

$ ->
  deck = buildDeck()
  newGame _.shuffle deck

$('#newGame').click (event) ->
  event.preventDefault()
  $('#hands').empty()
  newGame _.shuffle deck

buildDeck = ->
  deck = []
  for suit in 'HDCS'.split ''
    for rank in [2..9].concat 'TJQKA'.split ''
      deck.push "#{rank}#{suit}"
  deck

newGame = (deck, playerCount = 4) ->
  poker = new PokerCheck pokerRules
  hands = []
  bestHandIndex = 0
  bestHandRank = 0
  for playerIndex in [0...playerCount]
    hand = deck.splice 0, 5
    {handName, handRank} = poker.evaluateHand hand.join ' '
    $('#hands').append createHand hand, handName
    if handRank > bestHandRank
      bestHandRank = handRank
      bestHandIndex = playerIndex
  $('#hands tr').eq(bestHandIndex).addClass 'success'

suitLookup =
  H: '&hearts;'
  D: '&diams;'
  C: '&clubs;'
  S: '&spades;'

createHand = (hand, handName) ->
  $row = $ '<tr>'
  for card in hand
    [rank, suit] = card.split ''
    $td = $ '<td>'
    $td.html buildCard suit, rank #"#{rank}#{suitLookup[suit]}"
    $row.append $td
  $row.append $('<td>').html handName
  $row

buildCard = (suit, rank) ->
  if rank is 'T' then rank = '10'
  $cardEl = $ '<div>'
  $cardEl.addClass "card card--#{suit}"
  $rankEl = $ '<span>'
  $rankEl.addClass 'card__rank'
  $rankEl.html rank
  $cardEl.append $rankEl
  $cardEl

            
          
!
999px
Loading ..................

Console