1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187const COLORS = ['red', 'blue', 'black', 'orange'];
const COLOR_ORDER = new Map(COLORS.map((color, index) => [color, index]));
function parseTileId(id) {
if (typeof id !== 'string') {
return null;
}
if (id.startsWith('joker-')) {
return { id, color: 'joker', value: 0, joker: true };
}
const parts = id.split('-');
if (parts.length !== 3) {
return null;
}
const color = parts[0];
const value = Number(parts[1]);
const copy = Number(parts[2]);
if (!COLORS.includes(color)) {
return null;
}
if (!Number.isInteger(value) || value < 1 || value > 13) {
return null;
}
if (!Number.isInteger(copy) || (copy !== 0 && copy !== 1)) {
return null;
}
return { id, color, value, joker: false };
}
function isValidGroup(tiles) {
if (tiles.length < 3) {
return false;
}
if (isValidSet(tiles)) {
return true;
}
return isValidRun(tiles);
}
function isValidSet(tiles) {
if (tiles.length > 4) {
return false;
}
const nonJokers = tiles.filter((tile) => !tile.joker);
if (nonJokers.length === 0) {
return true;
}
const value = nonJokers[0].value;
if (nonJokers.some((tile) => tile.value !== value)) {
return false;
}
const colors = new Set(nonJokers.map((tile) => tile.color));
return colors.size === nonJokers.length;
}
function isValidRun(tiles) {
const nonJokers = tiles.filter((tile) => !tile.joker);
if (nonJokers.length === 0) {
return true;
}
const color = nonJokers[0].color;
if (nonJokers.some((tile) => tile.color !== color)) {
return false;
}
const values = nonJokers.map((tile) => tile.value).sort((a, b) => a - b);
for (let i = 1; i < values.length; i += 1) {
if (values[i] === values[i - 1]) {
return false;
}
}
const jokerCount = tiles.length - nonJokers.length;
let neededJokers = 0;
for (let i = 1; i < values.length; i += 1) {
neededJokers += values[i] - values[i - 1] - 1;
}
if (neededJokers > jokerCount) {
return false;
}
const remaining = jokerCount - neededJokers;
const min = values[0];
const max = values[values.length - 1];
const roomToExtend = (min - 1) + (13 - max);
return roomToExtend >= remaining;
}
function maxConsecutiveSum(length) {
const start = 14 - length;
return (length * (2 * start + length - 1)) / 2;
}
function maxRunValue(tiles) {
const nonJokers = tiles.filter((tile) => !tile.joker);
const length = tiles.length;
if (nonJokers.length === 0) {
return maxConsecutiveSum(length);
}
const values = nonJokers.map((tile) => tile.value).sort((a, b) => a - b);
const minValue = values[0];
const maxValue = values[values.length - 1];
const startMin = Math.max(1, maxValue - length + 1);
const startMax = Math.min(minValue, 14 - length);
if (startMin > startMax) {
return null;
}
const start = startMax;
return (length * (2 * start + length - 1)) / 2;
}
function groupMeldValue(tiles) {
if (isValidSet(tiles)) {
const nonJokers = tiles.filter((tile) => !tile.joker);
if (nonJokers.length === 0) {
return 13 * tiles.length;
}
return nonJokers[0].value * tiles.length;
}
if (isValidRun(tiles)) {
return maxRunValue(tiles) || 0;
}
return 0;
}
function handPoints(hand) {
return hand.reduce((sum, tile) => sum + (tile.joker ? 30 : tile.value), 0);
}
function normalizeGroupTiles(tiles) {
if (!tiles || tiles.length === 0) {
return [];
}
if (isValidSet(tiles)) {
const nonJokers = tiles.filter((tile) => !tile.joker);
const jokers = tiles.filter((tile) => tile.joker);
nonJokers.sort((a, b) => (COLOR_ORDER.get(a.color) ?? 99) - (COLOR_ORDER.get(b.color) ?? 99));
return [...nonJokers, ...jokers];
}
if (isValidRun(tiles)) {
const nonJokers = tiles.filter((tile) => !tile.joker).sort((a, b) => a.value - b.value);
const jokers = tiles.filter((tile) => tile.joker).slice();
if (nonJokers.length === 0) {
return jokers;
}
let min = nonJokers[0].value;
let max = nonJokers[nonJokers.length - 1].value;
const tileByValue = new Map(nonJokers.map((tile) => [tile.value, tile]));
const values = [];
for (let v = min; v <= max; v += 1) {
values.push(v);
}
const missing = values.filter((v) => !tileByValue.has(v));
missing.forEach((value) => {
if (jokers.length) {
tileByValue.set(value, jokers.shift());
}
});
while (jokers.length && max < 13) {
max += 1;
tileByValue.set(max, jokers.shift());
}
while (jokers.length && min > 1) {
min -= 1;
tileByValue.set(min, jokers.shift());
}
const ordered = [];
for (let v = min; v <= max; v += 1) {
if (tileByValue.has(v)) {
ordered.push(tileByValue.get(v));
}
}
ordered.push(...jokers);
return ordered;
}
return tiles.slice();
}
module.exports = {
COLORS,
parseTileId,
isValidGroup,
isValidSet,
isValidRun,
groupMeldValue,
handPoints,
normalizeGroupTiles
};