Skip to content

Commit

Permalink
Port Yacht exercise (#799)
Browse files Browse the repository at this point in the history
* Port Yacht exercise

* Make Yacht exercise to be unlocked by Bob
  • Loading branch information
ovidiu141 authored and SleeplessByte committed Jan 15, 2020
1 parent 0a89931 commit 1b6ecdb
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,19 @@
"searching",
"text_formatting"
]
},
{
"slug": "yacht",
"uuid": "77d0ab59-3824-4a79-9db3-e0bb35c6fc63",
"core": false,
"unlocked_by": "bob",
"difficulty": 4,
"topics": [
"arrays",
"conditionals",
"filtering",
"games"
]
}
]
}
26 changes: 26 additions & 0 deletions exercises/yacht/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"root": true,
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings"
],
"rules": {
"linebreak-style": "off",

"import/extensions": "off",
"import/no-default-export": "off",
"import/no-unresolved": "off",
"import/prefer-default-export": "off"
}
}
72 changes: 72 additions & 0 deletions exercises/yacht/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Score a single throw of dice in *Yacht*

The dice game [Yacht](https://en.wikipedia.org/wiki/Yacht_(dice_game)) is from
the same family as Poker Dice, Generala and particularly Yahtzee, of which it
is a precursor. In the game, five dice are rolled and the result can be entered
in any of twelve categories. The score of a throw of the dice depends on
category chosen.

## Scores in Yacht

| Category | Score | Description | Example |
| -------- | ----- | ----------- | ------- |
| Ones | 1 × number of ones | Any combination | 1 1 1 4 5 scores 3 |
| Twos | 2 × number of twos | Any combination | 2 2 3 4 5 scores 4 |
| Threes | 3 × number of threes | Any combination | 3 3 3 3 3 scores 15 |
| Fours | 4 × number of fours | Any combination | 1 2 3 3 5 scores 0 |
| Fives | 5 × number of fives| Any combination | 5 1 5 2 5 scores 15 |
| Sixes | 6 × number of sixes | Any combination | 2 3 4 5 6 scores 6 |
| Full House | Total of the dice | Three of one number and two of another | 3 3 3 5 5 scores 19 |
| Four of a Kind | Total of the four dice | At least four dice showing the same face | 4 4 4 4 6 scores 16 |
| Little Straight | 30 points | 1-2-3-4-5 | 1 2 3 4 5 scores 30 |
| Big Straight | 30 points | 2-3-4-5-6 | 2 3 4 5 6 scores 30 |
| Choice | Sum of the dice | Any combination | 2 3 3 4 6 scores 18 |
| Yacht | 50 points | All five dice showing the same face | 4 4 4 4 4 scores 50 |

If the dice do not satisfy the requirements of a category, the score is zero.
If, for example, *Four Of A Kind* is entered in the *Yacht* category, zero
points are scored. A *Yacht* scores zero if entered in the *Full House* category.

## Task
Given a list of values for five dice and a category, your solution should return
the score of the dice for that category. If the dice do not satisfy the requirements
of the category your solution should return 0. You can assume that five values
will always be presented, and the value of each will be between one and six
inclusively. You should not assume that the dice are ordered.

## Setup

Go through the setup instructions for Javascript to install the necessary
dependencies:

[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)

## Requirements

Install assignment dependencies:

```bash
$ npm install
```

## Making the test suite pass

Execute the tests with:

```bash
$ npm test
```

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xtest` to
`test`.

## Source

James Kilfiger, using wikipedia [https://en.wikipedia.org/wiki/Yacht_(dice_game)](https://en.wikipedia.org/wiki/Yacht_(dice_game))

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have
completed the exercise.
14 changes: 14 additions & 0 deletions exercises/yacht/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
presets: [
[
'@babel/env',
{
targets: {
node: 'current',
},
useBuiltIns: false,
},

],
],
};
96 changes: 96 additions & 0 deletions exercises/yacht/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const getScoreForTheYachtCategory = dices => {
const isYacht = new Set(dices).size === 1;
return isYacht ? 50 : 0;
};

const mapDicesToCounterArray = dices => {
let counterArray = [0, 0, 0, 0, 0, 0];
for (let item of dices) {
counterArray[item - 1]++;
}
return counterArray;
};

const getNoOfAppearances = (dices, diceValue) => {
return dices.filter(value => value === diceValue).length;
};

const getSumOfDices = dices => {
return dices.reduce((a, b) => a + b, 0);
};

const getScoreForTheFourOfAKindCategory = dices => {
const counterArray = mapDicesToCounterArray(dices);
for (let i = 0; i < counterArray.length; i++) {
if (counterArray[i] >= 4) {
return 4 * (i + 1);
}
}
return 0;
};

const getScoreForTheLittleStraightCategory = dices => {
const counterArray = mapDicesToCounterArray(dices);
const isLittleStraight = arrayIsFilledWithValue(counterArray, 0, counterArray.length - 1, 1);
return isLittleStraight ? 30 : 0;
};

const getScoreForTheBigStraightCategory = dices => {
const counterArray = mapDicesToCounterArray(dices);
const isBigStraight = arrayIsFilledWithValue(counterArray, 1, counterArray.length, 1);
return isBigStraight ? 30 : 0;
};

const arrayIsFilledWithValue = (array, startPos, endPos, value) => {
for (let i = startPos; i < endPos; i++) {
if (array[i] != value) {
return false;
}
}
return true;
};

const getScoreForTheFullHouseCategory = dices => {
const counterArray = mapDicesToCounterArray(dices);
let hasTwoOfAKind = false;
let hasThreeOfAKind = false;
for (let item of counterArray) {
if (item === 2) {
hasTwoOfAKind = true;
} else if (item === 3) {
hasThreeOfAKind = true;
}
}

return hasTwoOfAKind && hasThreeOfAKind ? getSumOfDices(dices) : 0;
};

export const score = (dices, category) => {
switch (category) {
case 'yacht':
return getScoreForTheYachtCategory(dices);
case 'ones':
return getNoOfAppearances(dices, 1);
case 'twos':
return 2 * getNoOfAppearances(dices, 2);
case 'threes':
return 3 * getNoOfAppearances(dices, 3);
case 'fours':
return 4 * getNoOfAppearances(dices, 4);
case 'fives':
return 5 * getNoOfAppearances(dices, 5);
case 'sixes':
return 6 * getNoOfAppearances(dices, 6);
case 'full house':
return getScoreForTheFullHouseCategory(dices);
case 'four of a kind':
return getScoreForTheFourOfAKindCategory(dices);
case 'little straight':
return getScoreForTheLittleStraightCategory(dices);
case 'big straight':
return getScoreForTheBigStraightCategory(dices);
case 'choice':
return getSumOfDices(dices);
}
return 0;
};
36 changes: 36 additions & 0 deletions exercises/yacht/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "exercism-javascript",
"version":"1.2.0",
"description": "Exercism exercises in Javascript.",
"author": "Katrina Owen",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/javascript"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@types/jest": "^24.0.16",
"@types/node": "^12.6.8",
"babel-eslint": "^10.0.2",
"babel-jest": "^24.8.0",
"eslint": "^6.1.0",
"eslint-plugin-import": "^2.18.2",
"jest": "^24.8.0"
},
"jest": {
"modulePathIgnorePatterns": [
"package.json"
]
},
"scripts": {
"test": "jest --no-cache ./*",
"watch": "jest --no-cache --watch ./*",
"lint": "eslint .",
"lint-test": "eslint . && jest --no-cache ./* "
},
"license": "MIT",
"dependencies": {}
}
8 changes: 8 additions & 0 deletions exercises/yacht/yacht.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// This is only a SKELETON file for the 'Yacht' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const score = () => {
throw new Error("Remove this statement and implement this function");
};
115 changes: 115 additions & 0 deletions exercises/yacht/yacht.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { score } from './yacht'

describe('Yacht', () => {
test('Yacht', () => {
expect(score([5, 5, 5, 5, 5], 'yacht')).toEqual(50);
});

xtest('Not Yacht', () => {
expect(score([1, 3, 3, 2, 5], 'yacht')).toEqual(0);
});

xtest('Ones', () => {
expect(score([1, 1, 1, 3, 5], 'ones')).toEqual(3);
});

xtest('Ones, out of order', () => {
expect(score([3, 1, 1, 5, 1], 'ones')).toEqual(3);
});

xtest('No ones', () => {
expect(score([4, 3, 6, 5, 5], 'ones')).toEqual(0);
});

xtest('Twos', () => {
expect(score([2, 3, 4, 5, 6], 'twos')).toEqual(2);
});

xtest('Fours', () => {
expect(score([1, 4, 1, 4, 1], 'fours')).toEqual(8);
});

xtest('Yacht counted as threes', () => {
expect(score([3, 3, 3, 3, 3], 'threes')).toEqual(15);
});

xtest('Yacht of 3s counted as fives', () => {
expect(score([3, 3, 3, 3, 3], 'fives')).toEqual(0);
});

xtest('Sixes', () => {
expect(score([2, 3, 4, 5, 6], 'sixes')).toEqual(6);
});

xtest('Full house two small, three big', () => {
expect(score([2, 2, 4, 4, 4], 'full house')).toEqual(16);
});

xtest('Full house three small, two big', () => {
expect(score([5, 3, 3, 5, 3], 'full house')).toEqual(19);
});

xtest('Two pair is not a full house', () => {
expect(score([2, 2, 4, 4, 5], 'full house')).toEqual(0);
});

xtest('Four of a kind is not a full house', () => {
expect(score([1, 4, 4, 4, 4], 'full house')).toEqual(0);
});

xtest('Yacht is not a full house', () => {
expect(score([2, 2, 2, 2, 2], 'full house')).toEqual(0);
});

xtest('Four of a Kind', () => {
expect(score([6, 6, 4, 6, 6], 'four of a kind')).toEqual(24);
});

xtest('Yacht can be scored as Four of a Kind', () => {
expect(score([3, 3, 3, 3, 3], 'four of a kind')).toEqual(12);
});

xtest('Full house is not Four of a Kind', () => {
expect(score([3, 3, 3, 5, 5], 'four of a kind')).toEqual(0);
});

xtest('Little Straight', () => {
expect(score([3, 5, 4, 1, 2], 'little straight')).toEqual(30);
});

xtest('Little Straight as Big Straight', () => {
expect(score([1, 2, 3, 4, 5], 'big straight')).toEqual(0);
});

xtest('Four in order but not a little straight', () => {
expect(score([1, 1, 2, 3, 4], 'little straight')).toEqual(0);
});

xtest('No pairs but not a little straight', () => {
expect(score([1, 2, 3, 4, 6], 'little straight')).toEqual(0);
});

xtest('Minimum is 1, maximum is 5, but not a little straight', () => {
expect(score([1, 1, 3, 4, 5], 'little straight')).toEqual(0);
});

xtest('Big Straight', () => {
expect(score([4, 6, 2, 5, 3], 'big straight')).toEqual(30);
});

xtest('Big Straight as little straight', () => {
expect(score([6, 5, 4, 3, 2], 'little straight')).toEqual(0);
});

xtest('No pairs but not a big straight', () => {
expect(score([6, 5, 4, 3, 1], 'big straight')).toEqual(0);
});

xtest('Choice', () => {
expect(score([3, 3, 5, 6, 6], 'choice')).toEqual(23);
});

xtest('Yacht as choice', () => {
expect(score([2, 2, 2, 2, 2], 'choice')).toEqual(10);
});
});

0 comments on commit 1b6ecdb

Please sign in to comment.