Last active
September 8, 2020 20:33
-
-
Save thykka/62456049087e8b4887fdcb7d4e497704 to your computer and use it in GitHub Desktop.
SCRIPT-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"iframeVersion": "0.1.280", | |
"lines": [ | |
94, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0 | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment