Commit fb3f295f authored by Lucas BRETTES's avatar Lucas BRETTES
Browse files

fix(yatzy): missing edge case on fullHouse 馃悰

parent 4a4bb0b9
...@@ -36,6 +36,7 @@ describe('Twos', () => { ...@@ -36,6 +36,7 @@ describe('Twos', () => {
describe('Threes', () => { describe('Threes', () => {
it('score the sum of 3s', () => { it('score the sum of 3s', () => {
assert.strictEqual(0, new Yatzy(1, 2, 4, 2, 4).threes());
assert.strictEqual(6, new Yatzy(1, 2, 3, 2, 3).threes()); assert.strictEqual(6, new Yatzy(1, 2, 3, 2, 3).threes());
assert.strictEqual(12, new Yatzy(2, 3, 3, 3, 3).threes()); assert.strictEqual(12, new Yatzy(2, 3, 3, 3, 3).threes());
}); });
...@@ -43,6 +44,7 @@ describe('Threes', () => { ...@@ -43,6 +44,7 @@ describe('Threes', () => {
describe('Fours', () => { describe('Fours', () => {
it('score the sum of 4s', () => { it('score the sum of 4s', () => {
assert.strictEqual(0, new Yatzy(1, 2, 3, 5, 5).fours());
assert.strictEqual(12, new Yatzy(4, 4, 4, 5, 5).fours()); assert.strictEqual(12, new Yatzy(4, 4, 4, 5, 5).fours());
assert.strictEqual(8, new Yatzy(4, 4, 5, 5, 5).fours()); assert.strictEqual(8, new Yatzy(4, 4, 5, 5, 5).fours());
assert.strictEqual(4, new Yatzy(4, 5, 5, 5, 5).fours()); assert.strictEqual(4, new Yatzy(4, 5, 5, 5, 5).fours());
...@@ -51,6 +53,7 @@ describe('Fours', () => { ...@@ -51,6 +53,7 @@ describe('Fours', () => {
describe('Fives', () => { describe('Fives', () => {
it('score the sum of fives', () => { it('score the sum of fives', () => {
assert.strictEqual(0, new Yatzy(1, 2, 4, 6, 3).fives());
assert.strictEqual(10, new Yatzy(4, 4, 4, 5, 5).fives()); assert.strictEqual(10, new Yatzy(4, 4, 4, 5, 5).fives());
assert.strictEqual(15, new Yatzy(4, 4, 5, 5, 5).fives()); assert.strictEqual(15, new Yatzy(4, 4, 5, 5, 5).fives());
assert.strictEqual(20, new Yatzy(4, 5, 5, 5, 5).fives()); assert.strictEqual(20, new Yatzy(4, 5, 5, 5, 5).fives());
...@@ -123,6 +126,7 @@ describe('Large straight', () => { ...@@ -123,6 +126,7 @@ describe('Large straight', () => {
describe('Full house', () => { describe('Full house', () => {
it('scores the sum of the full house', () => { it('scores the sum of the full house', () => {
assert.strictEqual(0, new Yatzy(2, 3, 4, 5, 6).fullHouse()); assert.strictEqual(0, new Yatzy(2, 3, 4, 5, 6).fullHouse());
assert.strictEqual(0, new Yatzy(4, 4, 4, 4, 1).fullHouse());
assert.strictEqual(22, new Yatzy(2, 2, 6, 6, 6).fullHouse()); assert.strictEqual(22, new Yatzy(2, 2, 6, 6, 6).fullHouse());
assert.strictEqual(18, new Yatzy(6, 2, 2, 2, 6).fullHouse()); assert.strictEqual(18, new Yatzy(6, 2, 2, 2, 6).fullHouse());
}); });
......
...@@ -51,7 +51,7 @@ export default class Yatzy { ...@@ -51,7 +51,7 @@ export default class Yatzy {
const diceMap = this.toDiceMap(this.dice); const diceMap = this.toDiceMap(this.dice);
const keyPairs = Object.entries(diceMap).filter(([key, value]) => value >= 2).map(([key, value]) => +key); const keyPairs = Object.entries(diceMap).filter(([key, value]) => value >= 2).map(([key, value]) => +key);
return keyPairs.length == 2 ? keyPairs.reduce((firstPair, secondPair) => (firstPair*2) + (secondPair*2)) : 0; return keyPairs.length === 2 ? keyPairs.reduce((firstPair, secondPair) => (firstPair*2) + (secondPair*2)) : 0;
} }
threeOfAKind(): number { threeOfAKind(): number {
...@@ -76,7 +76,8 @@ export default class Yatzy { ...@@ -76,7 +76,8 @@ export default class Yatzy {
fullHouse(): number { fullHouse(): number {
const diceMap = this.toDiceMap(this.dice); const diceMap = this.toDiceMap(this.dice);
return Object.keys(diceMap).length == 2 ? Object.keys(diceMap).reduce((sum, key) => sum += (+key * diceMap[key]), 0) : 0; return Object.keys(diceMap).length === 2 && Object.values(diceMap).some(d => d === 2 || d === 3)
? Object.keys(diceMap).reduce((sum, key) => sum += (+key * diceMap[key]), 0) : 0;
} }
private singles(dice: number[], selectedValue: number): number { private singles(dice: number[], selectedValue: number): number {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment