Commit 767def6d authored by Lucas BRETTES's avatar Lucas BRETTES
Browse files

Merge branch 'develop' into 'master'

Fix some edge cases and refactor dice-utils

See merge request !2
parents 12379035 4cca0284
......@@ -3,7 +3,7 @@
Make sure you have [node.js](https://nodejs.org), and [npm](https://www.npmjs.com/get-npm)
installed. Then in this directory:
## To install mocha in node_modules:
## To install jest in node_modules:
```bash
npm install
......
......@@ -2,8 +2,8 @@
"name": "yatzy-refactoring-kata",
"version": "0.0.1",
"author": {
"name": "Lorens León Parra",
"email": "lorensleon@gmail.org"
"name": "Lucas Brettes",
"email": "lbrettes@ippon.fr"
},
"dependencies": {
"@types/node": "*"
......
import assert from 'assert';
import Yatzy from './Yatzy';
import Yatzy from './yatzy';
describe('Chance', () => {
......@@ -36,6 +36,7 @@ describe('Twos', () => {
describe('Threes', () => {
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(12, new Yatzy(2, 3, 3, 3, 3).threes());
});
......@@ -43,6 +44,7 @@ describe('Threes', () => {
describe('Fours', () => {
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(8, new Yatzy(4, 4, 5, 5, 5).fours());
assert.strictEqual(4, new Yatzy(4, 5, 5, 5, 5).fours());
......@@ -51,6 +53,7 @@ describe('Fours', () => {
describe('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(15, new Yatzy(4, 4, 5, 5, 5).fives());
assert.strictEqual(20, new Yatzy(4, 5, 5, 5, 5).fives());
......@@ -123,6 +126,7 @@ describe('Large straight', () => {
describe('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(4, 4, 4, 4, 1).fullHouse());
assert.strictEqual(22, new Yatzy(2, 2, 6, 6, 6).fullHouse());
assert.strictEqual(18, new Yatzy(6, 2, 2, 2, 6).fullHouse());
});
......
import { isConsecutives, sumOfSelectedValue, toDiceMap } from './dice-utils';
type D6 = 1 | 2 | 3 | 4 | 5 | 6;
export default class Yatzy {
......@@ -17,81 +18,66 @@ export default class Yatzy {
}
ones(): number {
return this.singles(this.dice, 1);
return sumOfSelectedValue(this.dice, 1);
}
twos(): number {
return this.singles(this.dice, 2);
return sumOfSelectedValue(this.dice, 2);
}
threes(): number {
return this.singles(this.dice, 3);
return sumOfSelectedValue(this.dice, 3);
}
fours(): number {
return this.singles(this.dice, 4);
return sumOfSelectedValue(this.dice, 4);
}
fives(): number {
return this.singles(this.dice, 5);
return sumOfSelectedValue(this.dice, 5);
}
sixes(): number {
return this.singles(this.dice, 6);
return sumOfSelectedValue(this.dice, 6);
}
onePair(): number {
const diceMap = this.toDiceMap(this.dice);
const diceMap = toDiceMap(this.dice);
const keyPairs = Object.entries(diceMap).filter(([key, value]) => value >= 2).map(([key, value]) => +key);
return keyPairs.length > 0 ? Math.max(...keyPairs) * 2 : 0;
}
twoPair(): number {
const diceMap = this.toDiceMap(this.dice);
const diceMap = toDiceMap(this.dice);
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 {
const diceMap = this.toDiceMap(this.dice);
const diceMap = toDiceMap(this.dice);
const threeOfAKindDice = Object.keys(diceMap).find(k => diceMap[k] >= 3);
return threeOfAKindDice ? +threeOfAKindDice * 3 : 0;
}
fourOfAKind(): number {
const diceMap = this.toDiceMap(this.dice);
const diceMap = toDiceMap(this.dice);
const fourOfAKindDice = Object.keys(diceMap).find(k => diceMap[k] >= 4);
return fourOfAKindDice ? +fourOfAKindDice * 4 : 0;
}
smallStraight(): number {
return this.isConsecutives(this.dice) && Math.min(...this.dice) === 1 ? 15 : 0;
return isConsecutives(this.dice) && Math.min(...this.dice) === 1 ? 15 : 0;
}
largeStraight(): number {
return this.isConsecutives(this.dice) && Math.max(...this.dice) === 6 ? 20 : 0;
return isConsecutives(this.dice) && Math.max(...this.dice) === 6 ? 20 : 0;
}
fullHouse(): number {
const diceMap = this.toDiceMap(this.dice);
return Object.keys(diceMap).length == 2 ? Object.keys(diceMap).reduce((sum, key) => sum += (+key * diceMap[key]), 0) : 0;
}
private singles(dice: number[], selectedValue: number): number {
return dice.filter(d => d === selectedValue).reduce((previous, next) => previous + next, 0);
}
private toDiceMap(dice: number[]): {[key:string]: number} {
const diceMap: {[key:string]:number} = {};
for(const d of dice) {
diceMap[d] = diceMap[d] ? ++diceMap[d] : 1;
}
return diceMap;
}
private isConsecutives(dice: number[]): boolean {
return dice.sort().every((d, index) => (dice[index+1] || d+1)-d === 1);
const diceMap = toDiceMap(this.dice);
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;
}
}
import { isConsecutives, sumOfSelectedValue, toDiceMap } from './dice-utils';
describe('Sum of selected value in array', () => {
it('should give the sum of a selected value in an array', () => {
expect(sumOfSelectedValue([1, 1, 2, 20, 5, 4, 1], 1)).toBe(3);
expect(sumOfSelectedValue([1, 1, 2, 20, 5, 4, 1], 9)).toBe(0)
});
});
describe('Convert dice array to map', () => {
it('should return a map with the dice face as key and his number of occurences as value', () => {
expect(toDiceMap([1, 1, 1, 2, 2, 3])).toStrictEqual({'1': 3, '2': 2, '3':1});
});
});
describe('Check if array contains only consecutives values', () => {
it('should return true', () => {
expect(isConsecutives([2, 1, 5, 4, 3])).toBe(true);
});
it('should return false', () => {
expect(isConsecutives([2, 1, 5, 3, 7, 8])).toBe(false);
})
});
\ No newline at end of file
export const sumOfSelectedValue = (dice: number[], selectedValue: number) : number =>
dice.filter(d => d === selectedValue).reduce((previous, next) => previous + next, 0);
export const toDiceMap = (dice: number[]): {[key:string]: number} => {
const diceMap: {[key:string]:number} = {};
for(const d of dice) {
diceMap[d] = diceMap[d] ? ++diceMap[d] : 1;
}
return diceMap;
}
export const isConsecutives = (dice: number[]): boolean =>
dice.sort().every((d, index) => (dice[index+1] || d+1)-d === 1);
\ No newline at end of file
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