Created
April 5, 2018 06:04
-
-
Save MarcoWorms/933485b340e1babb67f88f8380e23bf9 to your computer and use it in GitHub Desktop.
Isolating the impurity of randomness in card games - Examples
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 thisIsADeck = [ | |
'this is a card', | |
3464562456, // this is also a card, | |
{ attack: `i'm a property of a card!` }, // another card | |
[ | |
`I'm an element inside a card.`, | |
`I'm not a card because I'm not in the root level`, | |
`The card in this case is the whole array containing these phrases`, | |
], | |
] |
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 deck = [ | |
{ name: 'Fireball', damage: 6, cost: 2 }, | |
{ name: 'Thunderstorm', damage: 10, cost: 3 }, | |
{ name: 'Ice Razors', damage: 30, cost: 7 }, | |
] |
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 { without } = require('ramda') | |
const card = deck[0] // 1. Card | |
const newDeck = without([card], deck) // 2. Remaining deck |
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 { head } = require('ramda') | |
const drawFromTop = draw(head) | |
const { card, newDeck } = drawFromTop(deck) |
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 draw = curry((cardPicker, deck) => { | |
const card = cardPicker(deck) | |
const newDeck = without(card, deck) | |
return { card, newDeck } | |
}) |
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
// http://ramdajs.com/docs/#pipe | |
const { pipe, curry } = require('ramda') | |
const addToTop = curry((card, deck) => [card, ...deck]) | |
const addToBottom = curry((card, deck) => [...deck, card]) | |
const addBoth = pipe( | |
addToTop({ name: 'Ice Wall', damage: 3, cost: 1 }), | |
addToBottom({ name: 'Earth Strike', damage: 6, cost: 3 }) | |
) | |
const newDeck = addBoth(deck) |
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 last = array => array[array.length - 1] | |
const { card, newDeck } = draw(last, deck) |
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 head = array => array[0] | |
const { card, newDeck } = draw(head, deck) |
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 draw = (cardPicker, deck) => { | |
// 1. We want to know what card we drew | |
const card = cardPicker(deck) | |
// 2. We want the new deck after the draw | |
const newDeck = without(card, deck) | |
return { card, newDeck } | |
} |
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 without = (element, array) => array.filter(e => e !== element) | |
// Watch out because on this one the "element" is not an array like the Ramda function. |
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 randomElement = array => | |
array[Math.floor(Math.random() * array.length)] | |
draw(randomElement, deck) |
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 seededRandom = seed => Math.abs(Math.sin(seed)) |
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 { pipe, sortBy, prop, pluck } = require('ramda') | |
const shuffle = (seed, deck) => { | |
const routine = pipe( | |
deck => deck.map((card, index) => ({ | |
rank: seededRandom(seed + index), | |
card, | |
})), | |
sortBy(prop('rank')), | |
pluck('card') | |
) | |
return routine(deck) | |
} |
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 seed = 1234 | |
const shuffledDeck = shuffle(seed, deck) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment