From 575b77ffe78c4ce73b2e78aa64bed49c794a7bc7 Mon Sep 17 00:00:00 2001 From: nik frank Date: Sun, 23 Apr 2017 01:48:38 +0300 Subject: [PATCH] flushes, names on specs --- canon/crib/src/util/cases.js | 41 +++++++++++++++++++++++++++++-- canon/crib/src/util/score.js | 11 ++++++--- canon/crib/src/util/score.test.js | 4 +-- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/canon/crib/src/util/cases.js b/canon/crib/src/util/cases.js index 8f34997..c5b8cc8 100644 --- a/canon/crib/src/util/cases.js +++ b/canon/crib/src/util/cases.js @@ -1,25 +1,62 @@ export default [ { + name: 'pairs', hand: [ { rank: 2, suit: 0 }, { rank: 2, suit: 1 }, { rank: 2, suit: 2 }, { rank: 12, suit: 3 }, - ], cut: { rank: 2, suit: 3 }, value: 12, }, { + name: 'dibs', hand: [ { rank: 4, suit: 0 }, { rank: 2, suit: 1 }, { rank: 6, suit: 2 }, { rank: 11, suit: 3 }, - ], cut: { rank: 8, suit: 3 }, value: 1, }, + + { + name: 'four flush hand', + hand: [ + { rank: 4, suit: 3 }, + { rank: 2, suit: 3 }, + { rank: 6, suit: 3 }, + { rank: 11, suit: 3 }, + ], + cut: { rank: 8, suit: 2 }, + value: 4, + }, + + { + name: 'five flush hand', + hand: [ + { rank: 4, suit: 3 }, + { rank: 2, suit: 3 }, + { rank: 6, suit: 3 }, + { rank: 10, suit: 3 }, + ], + cut: { rank: 8, suit: 3 }, + value: 5, + }, + + { + name: 'five flush crib', + hand: [ + { rank: 4, suit: 3 }, + { rank: 2, suit: 3 }, + { rank: 6, suit: 3 }, + { rank: 11, suit: 3 }, + { rank: 8, suit: 3 }, + ], + value: 5, + }, + ]; diff --git a/canon/crib/src/util/score.js b/canon/crib/src/util/score.js index f468a9c..3ca791f 100644 --- a/canon/crib/src/util/score.js +++ b/canon/crib/src/util/score.js @@ -1,17 +1,22 @@ // refactor into one big ugly reduce! [scoring fn, ..].reduce -export default (hand, cut) => { +export default (hand, cut = {}) => { + const cards = hand.concat(cut); + let total = 0; const dibsPts = hand.filter( ({ rank, suit }) => ((rank === 11) && (suit === cut.suit)) ).length; - const cards = hand.concat(cut); - const pairPts = cards.reduce( (p, c, i) => p + cards.slice(i + 1).filter( ({ rank }) => (rank === c.rank) ).length * 2, 0); + + const flushPts = (hand.reduce( (p, c) => (( p === c.suit ) ? p : -1), hand[0].suit) > -1) ? + cards.filter( ({ suit }) => (suit === hand[0].suit) ).length : 0; + total += dibsPts; total += pairPts; + total += flushPts; return total; }; diff --git a/canon/crib/src/util/score.test.js b/canon/crib/src/util/score.test.js index 01d6512..86f78a7 100644 --- a/canon/crib/src/util/score.test.js +++ b/canon/crib/src/util/score.test.js @@ -9,8 +9,8 @@ import score from './score'; import cases from './cases'; -cases.forEach(({ hand, cut, value }) => - it('calculates the score for each hand correctly', ()=>{ +cases.forEach(({ hand, cut, value, name }) => + it(`scores: ${name}`, ()=>{ expect( score(hand, cut) ).toEqual( value ); }) );