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

feat: Initial commit 馃帀

parents
/build/
/node_modules/
*.log
# TypeScript version of Yatzy Refactoring Kata
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:
```bash
npm install
```
## To run unit tests:
```bash
npm test
```
## To run unit tests in watch mode:
```bash
npm run watch
```
This diff is collapsed.
{
"name": "yatzy-refactoring-kata",
"version": "0.0.1",
"author": {
"name": "Lorens Le贸n Parra",
"email": "lorensleon@gmail.org"
},
"dependencies": {
"@types/node": "*"
},
"devDependencies": {
"@types/mocha": "^5.2.0",
"mocha": "^8.2.1",
"ts-node": "^6.0.5",
"typescript": "^2.8.3"
},
"scripts": {
"test": "mocha --require ts-node/register/transpile-only 'test/**/*.ts'",
"watch": "mocha --require ts-node/register --watch --watch-files src, test/**/*.ts"
},
"private": true
}
export default class Yatzy {
private dice: number[];
constructor(d1: number, d2: number, d3: number, d4: number, _5: number) {
this.dice = [];
this.dice[0] = d1;
this.dice[1] = d2;
this.dice[2] = d3;
this.dice[3] = d4;
this.dice[4] = _5;
}
static chance(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var total = 0;
total += d1;
total += d2;
total += d3;
total += d4;
total += d5;
return total;
}
static yatzy(...args: number[]): number {
var counts = [0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i != args.length; ++i) {
var die = args[i];
counts[die - 1]++;
}
for (i = 0; i != 6; i++) if (counts[i] == 5) return 50;
return 0;
}
static ones(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var sum = 0;
if (d1 == 1) sum++;
if (d2 == 1) sum++;
if (d3 == 1) sum++;
if (d4 == 1) sum++;
if (d5 == 1) sum++;
return sum;
}
static twos(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var sum = 0;
if (d1 == 2) sum += 2;
if (d2 == 2) sum += 2;
if (d3 == 2) sum += 2;
if (d4 == 2) sum += 2;
if (d5 == 2) sum += 2;
return sum;
}
static threes(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var s;
s = 0;
if (d1 == 3) s += 3;
if (d2 == 3) s += 3;
if (d3 == 3) s += 3;
if (d4 == 3) s += 3;
if (d5 == 3) s += 3;
return s;
}
static score_pair(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var counts = [0, 0, 0, 0, 0, 0, 0, 0, 0];
counts[d1 - 1]++;
counts[d2 - 1]++;
counts[d3 - 1]++;
counts[d4 - 1]++;
counts[d5 - 1]++;
var at;
for (at = 0; at != 6; at++) if (counts[6 - at - 1] >= 2) return (6 - at) * 2;
return 0;
}
static two_pair(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var counts = [0, 0, 0, 0, 0, 0, 0, 0, 0];
counts[d1 - 1]++;
counts[d2 - 1]++;
counts[d3 - 1]++;
counts[d4 - 1]++;
counts[d5 - 1]++;
var n = 0;
var score = 0;
for (let i = 0; i < 6; i += 1)
if (counts[6 - i - 1] >= 2) {
n++;
score += 6 - i;
}
if (n == 2) return score * 2;
else return 0;
}
static four_of_a_kind(_1: number, _2: number, d3: number, d4: number, d5: number): number {
var tallies;
tallies = [0, 0, 0, 0, 0, 0, 0, 0];
tallies[_1 - 1]++;
tallies[_2 - 1]++;
tallies[d3 - 1]++;
tallies[d4 - 1]++;
tallies[d5 - 1]++;
for (let i = 0; i < 6; i++) if (tallies[i] >= 4) return (i + 1) * 4;
return 0;
}
static three_of_a_kind(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var t;
t = [0, 0, 0, 0, 0, 0, 0, 0, 0];
t[d1 - 1]++;
t[d2 - 1]++;
t[d3 - 1]++;
t[d4 - 1]++;
t[d5 - 1]++;
for (let i = 0; i < 6; i++) if (t[i] >= 3) return (i + 1) * 3;
return 0;
}
static smallStraight(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var tallies;
tallies = [0, 0, 0, 0, 0, 0, 0];
tallies[d1 - 1] += 1;
tallies[d2 - 1] += 1;
tallies[d3 - 1] += 1;
tallies[d4 - 1] += 1;
tallies[d5 - 1] += 1;
if (tallies[0] == 1 && tallies[1] == 1 && tallies[2] == 1 && tallies[3] == 1 && tallies[4] == 1) return 15;
return 0;
}
static largeStraight(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var tallies;
tallies = [0, 0, 0, 0, 0, 0, 0, 0];
tallies[d1 - 1] += 1;
tallies[d2 - 1] += 1;
tallies[d3 - 1] += 1;
tallies[d4 - 1] += 1;
tallies[d5 - 1] += 1;
if (tallies[1] == 1 && tallies[2] == 1 && tallies[3] == 1 && tallies[4] == 1 && tallies[5] == 1) return 20;
return 0;
}
static fullHouse(d1: number, d2: number, d3: number, d4: number, d5: number): number {
var tallies;
var _2 = false;
var i;
var _2_at = 0;
var _3 = false;
var _3_at = 0;
tallies = [0, 0, 0, 0, 0, 0, 0, 0];
tallies[d1 - 1] += 1;
tallies[d2 - 1] += 1;
tallies[d3 - 1] += 1;
tallies[d4 - 1] += 1;
tallies[d5 - 1] += 1;
for (i = 0; i != 6; i += 1)
if (tallies[i] == 2) {
_2 = true;
_2_at = i + 1;
}
for (i = 0; i != 6; i += 1)
if (tallies[i] == 3) {
_3 = true;
_3_at = i + 1;
}
if (_2 && _3) return _2_at * 2 + _3_at * 3;
else return 0;
}
fours(): number {
var sum;
sum = 0;
for (let at = 0; at != 5; at++) {
if (this.dice[at] == 4) {
sum += 4;
}
}
return sum;
}
fives(): number {
let s = 0;
var i;
for (i = 0; i < this.dice.length; i++) if (this.dice[i] == 5) s = s + 5;
return s;
}
sixes(): number {
let sum = 0;
for (var at = 0; at < this.dice.length; at++) if (this.dice[at] == 6) sum = sum + 6;
return sum;
}
}
import assert from 'assert';
import Yatzy from '../src/Yatzy';
describe('Chance', () => {
it('scores sum of all dice', () => {
assert.strictEqual(15, Yatzy.chance(2, 3, 4, 5, 1));
assert.strictEqual(16, Yatzy.chance(3, 3, 4, 5, 1));
});
});
describe('Yatzy', () => {
it('scores 50', () => {
assert.strictEqual(50, Yatzy.yatzy(4, 4, 4, 4, 4));
assert.strictEqual(50, Yatzy.yatzy(6, 6, 6, 6, 6));
assert.strictEqual(0, Yatzy.yatzy(6, 6, 6, 6, 3));
});
});
describe('Ones', () => {
it('score the sum of 1s', () => {
assert.strictEqual(1, Yatzy.ones(1, 2, 3, 4, 5));
assert.strictEqual(2, Yatzy.ones(1, 2, 1, 4, 5));
assert.strictEqual(0, Yatzy.ones(6, 2, 2, 4, 5));
assert.strictEqual(4, Yatzy.ones(1, 2, 1, 1, 1));
});
});
describe('Twos', () => {
it('score the sum of 2s', () => {
assert.strictEqual(4, Yatzy.twos(1, 2, 3, 2, 6));
assert.strictEqual(10, Yatzy.twos(2, 2, 2, 2, 2));
});
});
describe('Threes', () => {
it('score the sum of 3s', () => {
assert.strictEqual(6, Yatzy.threes(1, 2, 3, 2, 3));
assert.strictEqual(12, Yatzy.threes(2, 3, 3, 3, 3));
});
});
describe('Fours', () => {
it('score the sum of 4s', () => {
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());
});
});
describe('Fives', () => {
it('score the sum of 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());
});
});
describe('Sixes', () => {
it('score the sum of sixes', () => {
assert.strictEqual(0, new Yatzy(4, 4, 4, 5, 5).sixes());
assert.strictEqual(6, new Yatzy(4, 4, 6, 5, 5).sixes());
assert.strictEqual(18, new Yatzy(6, 5, 6, 6, 5).sixes());
});
});
describe('One pair', () => {
it('scores the sum of the highest pair', () => {
assert.strictEqual(6, Yatzy.score_pair(3, 4, 3, 5, 6));
assert.strictEqual(10, Yatzy.score_pair(5, 3, 3, 3, 5));
assert.strictEqual(12, Yatzy.score_pair(5, 3, 6, 6, 5));
});
});
describe('Two pair', () => {
it('scores the sum of the two pairs', () => {
assert.strictEqual(16, Yatzy.two_pair(3, 3, 5, 4, 5));
assert.strictEqual(16, Yatzy.two_pair(3, 3, 5, 5, 5));
});
});
describe('Three of a kind', () => {
it('scores the sum of the three of the kind', () => {
assert.strictEqual(9, Yatzy.three_of_a_kind(3, 3, 3, 4, 5));
assert.strictEqual(15, Yatzy.three_of_a_kind(5, 3, 5, 4, 5));
assert.strictEqual(9, Yatzy.three_of_a_kind(3, 3, 3, 3, 5));
});
});
describe('Four of a kind', () => {
it('scores the sum of the four of the kind', () => {
assert.strictEqual(12, Yatzy.four_of_a_kind(3, 3, 3, 3, 5));
assert.strictEqual(20, Yatzy.four_of_a_kind(5, 5, 5, 4, 5));
assert.strictEqual(9, Yatzy.three_of_a_kind(3, 3, 3, 3, 3));
});
});
describe('Small straight', () => {
it('scores 15', () => {
assert.strictEqual(15, Yatzy.smallStraight(1, 2, 3, 4, 5));
assert.strictEqual(15, Yatzy.smallStraight(2, 3, 4, 5, 1));
assert.strictEqual(0, Yatzy.smallStraight(1, 2, 2, 4, 5));
});
});
describe('Large straight', () => {
it('scores 20', () => {
assert.strictEqual(20, Yatzy.largeStraight(6, 2, 3, 4, 5));
assert.strictEqual(20, Yatzy.largeStraight(2, 3, 4, 5, 6));
assert.strictEqual(0, Yatzy.largeStraight(1, 2, 2, 4, 5));
});
});
describe('Full house', () => {
it('scores the sum of the full house', () => {
assert.strictEqual(18, Yatzy.fullHouse(6, 2, 2, 2, 6));
assert.strictEqual(0, Yatzy.fullHouse(2, 3, 4, 5, 6));
});
});
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"declaration": false,
"outDir": "build",
"rootDir": ".",
"strict": true,
"noUnusedLocals": true,
"types": ["node", "mocha"],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": ["src", "test"],
"exclude": ["node_modules", "build"]
}
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