UNPKG

@kakegurui/shuffle

Version:

Shuffle utilities for Kakegurui Games

106 lines (105 loc) 3.19 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { cutDeck: () => cutDeck, deal: () => deal, gilbreathShuffle: () => gilbreathShuffle, randomNumber: () => randomNumber, riffleShuffle: () => riffleShuffle, shuffle: () => shuffle }); module.exports = __toCommonJS(src_exports); 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"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { cutDeck, deal, gilbreathShuffle, randomNumber, riffleShuffle, shuffle }); //# sourceMappingURL=index.js.map