Skip to content

Commit

Permalink
flushes, names on specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nik frank committed Apr 22, 2017
1 parent 6c690eb commit 575b77f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
41 changes: 39 additions & 2 deletions canon/crib/src/util/cases.js
Original file line number Diff line number Diff line change
@@ -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,
},

];
11 changes: 8 additions & 3 deletions canon/crib/src/util/score.js
Original file line number Diff line number Diff line change
@@ -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;
};
4 changes: 2 additions & 2 deletions canon/crib/src/util/score.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
})
);

0 comments on commit 575b77f

Please sign in to comment.