Skip to content

Instantly share code, notes, and snippets.

@thykka
Last active September 8, 2020 20:33
Show Gist options
  • Save thykka/62456049087e8b4887fdcb7d4e497704 to your computer and use it in GitHub Desktop.
Save thykka/62456049087e8b4887fdcb7d4e497704 to your computer and use it in GitHub Desktop.
SCRIPT-8
const Hands = {
...Object.fromEntries(
['Ones', 'Twos', 'Threes', 'Fours', 'Fives', 'Sixes'].map((handName, index) => ([
handName, {
value: hand => sumOfDiceWithValue(hand, index + 1),
matcher: hand => handHasValue(hand, index + 1)
}
]))
),
'One pair': {
value: () => 0, // TODO,
matcher: hand => hasNSameDice(hand, 2)
},
'Two pairs': { // TODO
value: () => 0,
matcher: () => false
},
'Three of a kind': {
value: () => 0, // TODO
matcher: hand => hasNSameDice(hand, 3)
},
'Four of a kind': {
value: () => 0, // TODO
matcher: hand => hasNSameDice(hand, 4)
},
'Small Straight': {
value: () => 15,
matcher: hand => handEquals(hand, [1, 2, 3, 4, 5])
},
'Large Straight': {
value: () => 20,
matcher: hand => handEquals(hand, [2, 3, 4, 5, 6])
},
'Full House': { // TODO
value: () => 0,
matcher: () => false
},
'Chance': {
value: hand => sum(hand),
matcher: () => true
},
'Yatzy': {
value: () => 50,
matcher: hand => Object.keys(countHand(hand)).length === 1
},
}
function sumOfDiceWithValue(hand, value) {
return sum(hand.filter(dice => dice === value))
}
function handHasValue(hand, value) {
return hand.filter(dice => dice === value).length > 0
}
function hasNSameDice(hand, n) {
return !!Object.values(countHand(hand)).find(sums => sums >= n)
}
function handEquals(source, target) {
return source.sort().every((dice, i) => dice === target[i]);
}
function sum(numbers) {
return numbers.reduce((sum, num) => sum + num, 0);
}
function countHand(hand) {
return hand.reduce((sums, dice) => {
sums[dice] = (sums[dice] || 0) + 1;
return sums;
}, {})
}
function getHandPossibilities(dealtHand) {
return Object.fromEntries(
Object.entries(Hands).map(([handName, hand]) => ([handName, {
match: hand.matcher(dealtHand),
value: hand.value(dealtHand)
}]))
)
}
init = state => {
}
update = (state, input, elapsed) => {
const testHand = [1, 2, 2, 3, 3]
log(testHand)
log(getHandPossibilities(testHand))
}
draw = state => {
}
{
"iframeVersion": "0.1.280",
"lines": [
94,
0,
0,
0,
0,
0,
0,
0
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment