UNPKG

@kakegurui/shuffle

Version:

Shuffle utilities for Kakegurui Games

78 lines (77 loc) 2.07 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/index.ts function shuffle(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array; } __name(shuffle, "shuffle"); function randomNumber(start, end) { if (start > end) { throw new Error("Start value cannot be greater than end value."); } return Math.floor(Math.random() * (end - start + 1)) + start; } __name(randomNumber, "randomNumber"); function riffleShuffle(deck1, deck2) { const shuffledDeck = []; let deck1Index = 0; let deck2Index = 0; while (deck1Index < deck1.length && deck2Index < deck2.length) { if (Math.random() < 0.5) { shuffledDeck.push(deck1[deck1Index]); deck1Index++; } else { shuffledDeck.push(deck2[deck2Index]); deck2Index++; } } while (deck1Index < deck1.length) { shuffledDeck.push(deck1[deck1Index]); deck1Index++; } while (deck2Index < deck2.length) { shuffledDeck.push(deck2[deck2Index]); deck2Index++; } return shuffledDeck; } __name(riffleShuffle, "riffleShuffle"); function deal(cards, numCards) { if (numCards > cards.length) { throw new Error("Not enough cards to deal."); } const pile = []; for (let i = 0; i < numCards; i++) { pile.unshift(cards.shift()); } return pile; } __name(deal, "deal"); function cutDeck(cards, cuts) { for (let i = 0; i < cuts; i++) { const cutPoint = Math.floor(Math.random() * cards.length); const top = cards.slice(0, cutPoint); const bottom = cards.slice(cutPoint); cards = bottom.concat(top); } return cards; } __name(cutDeck, "cutDeck"); function gilbreathShuffle(cards, split) { const pile = deal(cards, split); return riffleShuffle(pile, cards); } __name(gilbreathShuffle, "gilbreathShuffle"); export { cutDeck, deal, gilbreathShuffle, randomNumber, riffleShuffle, shuffle }; //# sourceMappingURL=index.mjs.map