Created
August 21, 2022 15:10
-
-
Save mtwomey/1ffc15b1076e47865d8f7aa355979e72 to your computer and use it in GitHub Desktop.
Mage Night Dummy Deck Simulation
This file contains 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
"use strict"; | |
const { randomInt } = require("crypto"); | |
const log = false; | |
for (let i = 1; i <= 10; i++) { | |
console.log(`Running round: ${i}`); | |
runRound(i, 10000000); | |
} | |
function runRound(roundNumber, numRuns) { | |
const results = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0 }; | |
for (let n = 0; n < numRuns; n++) { | |
results[simulateRound(roundNumber)]++; | |
} | |
const percentages = Object.keys(results).reduce( | |
(a, v) => ({ ...a, [v]: (results[v] / numRuns) * 100 }), | |
{} | |
); | |
console.log(Object.values(percentages).join(", ")); | |
} | |
function simulateRound(roundNumber) { | |
const crystals = { | |
red: 2, | |
white: 1, | |
blue: 0, | |
green: 0, | |
}; | |
const deck = generateDummyDeck(); | |
// Add additional crystals and cards depending on the round number | |
for (let i = 0; i < roundNumber - 1; i++) { | |
crystals[getRandomColor()]++; | |
deck.push(getRandomColor()); | |
} | |
let turn = 1; | |
while (deck.length > 0) { | |
turn++; | |
const cardsDrawn = draw(deck, 3); | |
const additionalCardsDrawn = draw( | |
deck, | |
crystals[cardsDrawn[cardsDrawn.length - 1]] | |
); | |
log && | |
console.log( | |
`Cards Drawn: ${cardsDrawn | |
.join(", ") | |
.padEnd(20)} Additional Cards Drawn: ${additionalCardsDrawn.join( | |
", " | |
)}` | |
); | |
} | |
log && console.log("End of Turn."); | |
log && console.log("Turns: ", turn); | |
return turn; | |
} | |
function getRandomColor() { | |
return ["red", "white", "blue", "green"][randomInt(4)]; | |
} | |
function draw(deck, n) { | |
const cardsDrawn = []; | |
if (deck.length > 0) { | |
for (let i = 0; i < n; i++) { | |
cardsDrawn.push(deck.pop()); | |
if (deck.length === 0) break; | |
} | |
} | |
return cardsDrawn; | |
} | |
// Generate dummy deck of 16 cards, 4 of each color | |
function generateDummyDeck() { | |
const deck = []; | |
["green", "white", "red", "blue"].forEach((color) => { | |
for (let i = 0; i < 4; i++) { | |
deck.push(color); | |
} | |
}); | |
return shuffle(deck); | |
} | |
// Fisher-Yates Shuffle | |
function shuffle(array) { | |
let currentIndex = array.length; | |
let randomIndex; | |
// While there remain elements to shuffle. | |
while (currentIndex != 0) { | |
// Pick a remaining element. | |
randomIndex = randomInt(currentIndex); | |
currentIndex--; | |
// And swap it with the current element. | |
[array[currentIndex], array[randomIndex]] = [ | |
array[randomIndex], | |
array[currentIndex], | |
]; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment