Skip to content

Instantly share code, notes, and snippets.

:shipit:
undefined

Marco Guaspari Worms MarcoWorms

:shipit:
undefined
View GitHub Profile
View shufflin.js
const seed = 1234
const shuffledDeck = shuffle(seed, deck)
View shuffle.js
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')
View seeded-random.js
const seededRandom = seed => Math.abs(Math.sin(seed))
View fiddling.js
// 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 })
View draw-curried-from-top.js
const { head } = require('ramda')
const drawFromTop = draw(head)
const { card, newDeck } = drawFromTop(deck)
View draw-curried.js
const draw = curry((cardPicker, deck) => {
const card = cardPicker(deck)
const newDeck = without(card, deck)
return { card, newDeck }
})
View draw-random.js
const randomElement = array =>
array[Math.floor(Math.random() * array.length)]
draw(randomElement, deck)
View draw-from-the-bottom.js
const last = array => array[array.length - 1]
const { card, newDeck } = draw(last, deck)
View draw-from-the-top.js
const head = array => array[0]
const { card, newDeck } = draw(head, deck)
View draw.js
const draw = (cardPicker, deck) => {
// 1. We want to know what card we drew
const card = cardPicker(deck)
// 2. We want the new deck without the drawn card
const newDeck = without(card, deck)
return { card, newDeck }
}
You can’t perform that action at this time.