@jkuebart/yahtzee 0.0.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE +28 -0
- package/package.json +43 -1
- package/yahtzee.js +97 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Copyright 2026, Joachim Kuebart <joachim.kuebart@gmail.com>
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
|
4
|
+
modification, are permitted provided that the following conditions are met:
|
|
5
|
+
|
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
|
8
|
+
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
10
|
+
notice, this list of conditions and the following disclaimer in the
|
|
11
|
+
documentation and/or other materials provided with the
|
|
12
|
+
distribution.
|
|
13
|
+
|
|
14
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
15
|
+
contributors may be used to endorse or promote products derived
|
|
16
|
+
from this software without specific prior written permission.
|
|
17
|
+
|
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
21
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
22
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
23
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
24
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
25
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
26
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
27
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
28
|
+
POSSIBILITY OF SUCH DAMAGE.
|
package/package.json
CHANGED
|
@@ -1,4 +1,46 @@
|
|
|
1
1
|
{
|
|
2
|
+
"author": "Joachim Kuebart <joachim.kuebart@gmail.com>",
|
|
3
|
+
"bugs": {
|
|
4
|
+
"url": "https://gitlab.com/jkuebart/yahtzee/-/work_items"
|
|
5
|
+
},
|
|
6
|
+
"description": "A simple implementation of the Yahtzee game",
|
|
7
|
+
"devDependencies": {
|
|
8
|
+
"@commitlint/cli": "*",
|
|
9
|
+
"@commitlint/config-conventional": "*",
|
|
10
|
+
"@semantic-release/gitlab": "*",
|
|
11
|
+
"@semantic-release/npm": "*",
|
|
12
|
+
"cobertura": "*",
|
|
13
|
+
"semantic-release": "*"
|
|
14
|
+
},
|
|
15
|
+
"exports": "./yahtzee.js",
|
|
16
|
+
"files": [
|
|
17
|
+
"LICENCE",
|
|
18
|
+
"README.md",
|
|
19
|
+
"package.json",
|
|
20
|
+
"yahtzee.js"
|
|
21
|
+
],
|
|
22
|
+
"homepage": "https://gitlab.com/jkuebart/yahtzee",
|
|
23
|
+
"keywords": [
|
|
24
|
+
"game",
|
|
25
|
+
"yahtzee"
|
|
26
|
+
],
|
|
27
|
+
"license": "BSD-3-Clause",
|
|
28
|
+
"main": "yahtzee.js",
|
|
2
29
|
"name": "@jkuebart/yahtzee",
|
|
3
|
-
"
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://gitlab.com/jkuebart/yahtzee.git"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"commitlint": "commitlint",
|
|
39
|
+
"coverage": "node --experimental-test-coverage --test --test-reporter cobertura --test-reporter spec --test-reporter-destination cobertura.xml --test-reporter-destination stdout",
|
|
40
|
+
"release": "semantic-release",
|
|
41
|
+
"test:report": "node --test --test-reporter junit --test-reporter-destination junit.xml",
|
|
42
|
+
"test": "node --test"
|
|
43
|
+
},
|
|
44
|
+
"type": "module",
|
|
45
|
+
"version": "1.0.0"
|
|
4
46
|
}
|
package/yahtzee.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
export const SIDES = 6;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Enumerate all of the combinations.
|
|
5
|
+
* @param {number} n The number of dice.
|
|
6
|
+
* @return {Generator<number[]>} All combinations.
|
|
7
|
+
*/
|
|
8
|
+
export const rolls = function* (n) {
|
|
9
|
+
for (let i = 0; ; ++i) {
|
|
10
|
+
/** @type {number[]} */
|
|
11
|
+
const roll = [];
|
|
12
|
+
for (let v = i; 0 < v; v = Math.floor(v / SIDES)) {
|
|
13
|
+
roll.push(v % SIDES);
|
|
14
|
+
}
|
|
15
|
+
if (n < roll.length) {
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
while (roll.length < n) {
|
|
19
|
+
roll.push(0);
|
|
20
|
+
}
|
|
21
|
+
yield roll;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {number} start
|
|
27
|
+
* @param {number} count
|
|
28
|
+
* @return {Generator<number, void>}
|
|
29
|
+
*/
|
|
30
|
+
const sequence = function* (start, count) {
|
|
31
|
+
for (count += start; start !== count; ++start) {
|
|
32
|
+
yield start;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @template T
|
|
38
|
+
* @param {Set<T>} haystack
|
|
39
|
+
* @param {Iterable<T>} needles
|
|
40
|
+
*/
|
|
41
|
+
const isSubset = (haystack, needles) => [...needles].every(
|
|
42
|
+
(item) => haystack.has(item)
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @param {number[]} set
|
|
48
|
+
* @return {number[][]}
|
|
49
|
+
*/
|
|
50
|
+
const partition = (set) => set.toSorted().reduce(
|
|
51
|
+
/**
|
|
52
|
+
* @param {number[][]} acc
|
|
53
|
+
* @param {number} item
|
|
54
|
+
* @return {number[][]}
|
|
55
|
+
*/
|
|
56
|
+
(acc, item) => (
|
|
57
|
+
0 !== acc.length && item === acc.at(-1)[0]
|
|
58
|
+
? [...acc.slice(0, -1), [...acc.at(-1), item]]
|
|
59
|
+
: [...acc, [item]]
|
|
60
|
+
),
|
|
61
|
+
[]
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
*
|
|
66
|
+
* @param {number[]} roll The rolled values.
|
|
67
|
+
* @param {number} n A number.
|
|
68
|
+
* @return {boolean} Whether the values are "n" of a kind.
|
|
69
|
+
*/
|
|
70
|
+
export const ofAKind = (roll, n) => partition(roll).map((p) => p.length).some(
|
|
71
|
+
(k) => n <= k
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {number[]} roll The rolled values.
|
|
76
|
+
* @return {boolean} Whether the values are a full house.
|
|
77
|
+
*/
|
|
78
|
+
export const isFullHouse = (roll) => (
|
|
79
|
+
(partitions) => 2 === partitions.length && 2 <= partitions[0].length && partitions[0].length <= 3
|
|
80
|
+
)(partition(roll));
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {number[]} roll The rolled values.
|
|
84
|
+
* @param {number} length The length of the straight.
|
|
85
|
+
* @return {boolean} Whether the values are a small straight.
|
|
86
|
+
*/
|
|
87
|
+
export const isStraight = (roll, length) => [
|
|
88
|
+
...sequence(0, 1 + SIDES - length)
|
|
89
|
+
].some(
|
|
90
|
+
(start) => isSubset(new Set(roll), sequence(start, length))
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @param {number[]} roll The rolled values.
|
|
95
|
+
* @return {boolean} Whether the values are a Yahtzee.
|
|
96
|
+
*/
|
|
97
|
+
export const isYahtzee = (roll) => 1 === new Set(roll).size;
|