Skip to content

Instantly share code, notes, and snippets.

@shuuki
Last active March 2, 2017 07:20
Show Gist options
  • Save shuuki/9fd8e291641690e184f26abeff6e0567 to your computer and use it in GitHub Desktop.
Save shuuki/9fd8e291641690e184f26abeff6e0567 to your computer and use it in GitHub Desktop.
realish deck shuffling
function print(x) {
console.log(x)
}
var shuffle = {}
shuffle.cut = function(deck) {
let pile = deck,
deck_a = [],
deck_b = [],
cut = Math.floor((Math.random() * pile.length / 3) + (pile.length / 3)),
index = 0
for (let x = 0; x < cut; x++) {
deck_a.push(pile[x])
index = index + 1
}
for (let y = index; y < pile.length; y++) {
deck_b.push(pile[y])
}
return [
deck_a,
deck_b
]
}
shuffle.combine = function(cut) {
let pile = []
pile = cut[1].concat(cut[0])
return pile
}
shuffle.riffle = function(cut) {
let deck = cut,
pile = [],
size = deck[0].length + deck[1].length
while (size > 0) {
let roll = Math.floor(Math.random() * 1.99)
card = deck[roll].pop()
if (card !== undefined) {
pile.push(card)
size = size - 1
}
}
return pile
}
// test deck
var tester = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
// cut the deck
print(shuffle.cut(tester))
// cut the deck and recombine
print(shuffle.combine(shuffle.cut(tester)))
// cut the deck and riffle combine
print(shuffle.riffle(shuffle.cut(tester)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment