Created
January 27, 2023 20:22
-
-
Save rileyjshaw/7d385d0f2af83de395849612bf020801 to your computer and use it in GitHub Desktop.
Playing Oliver Darkshire’s “Potato” a million times
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
// Game rules: https://twitter.com/deathbybadger/status/1567425842526945280 | |
const resultCounter = { | |
whisked: 0, | |
burrowed: 0, | |
eaten: 0, | |
}; | |
const gameState = {}; | |
const roll = () => Math.floor(Math.random() * 6); | |
const gardenTree = [ | |
() => ++gameState.potatoes, | |
() => { | |
++gameState.potatoes; | |
++gameState.destiny; | |
}, | |
() => { | |
++gameState.destiny; | |
++gameState.orcs; | |
}, | |
() => { | |
++gameState.orcs; | |
gameState.potatoes = Math.max(0, gameState.potatoes - 1); | |
}, | |
() => (gameState.potatoes = Math.max(0, gameState.potatoes - 1)), | |
() => (gameState.potatoes += 2), | |
]; | |
const knockTree = [ | |
() => ++gameState.orcs, | |
() => ++gameState.destiny, | |
() => { | |
++gameState.orcs; | |
++gameState.destiny; | |
}, | |
() => { | |
gameState.potatoes = Math.max(0, gameState.potatoes - 1); | |
gameState.orcs += 2; | |
}, | |
() => ++gameState.destiny, | |
() => (gameState.potatoes += 2), | |
]; | |
function addDanger() { | |
++gameState.danger; | |
} | |
const startTree = [gardenTree, gardenTree, knockTree, knockTree, addDanger, addDanger]; | |
function check() { | |
if (gameState.orcs >= 10) { | |
// At the last minute, hurl in the back garden! | |
while (gameState.potatoes >= gameState.danger && gameState.orcs >= 10) { | |
gameState.potatoes -= gameState.danger; | |
--gameState.orcs; | |
} | |
if (gameState.orcs >= 10) { | |
++resultCounter.eaten; | |
} else { | |
currentTree = startTree; | |
return false; | |
} | |
} else if (gameState.destiny >= 10) { | |
++resultCounter.whisked; | |
} else if (gameState.potatoes >= 10) { | |
++resultCounter.burrowed; | |
} else { | |
currentTree = startTree; | |
return false; | |
} | |
return true; | |
} | |
function resetGame() { | |
currentTree = startTree; | |
gameState.orcs = 0; | |
gameState.potatoes = 0; | |
gameState.destiny = 0; | |
gameState.danger = 1; | |
} | |
function playGame() { | |
resetGame(); | |
while (true) { | |
const roll = Math.floor(Math.random() * 6); | |
const next = currentTree[roll]; | |
if (typeof next === 'function') { | |
next(); | |
if (check()) return; | |
} else { | |
currentTree = next; | |
} | |
} | |
} | |
for (let i = 0; i < 1000000; ++i) playGame(); | |
console.log(resultCounter); |
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
{ | |
"burrowed": 222276, | |
"eaten": 554215, | |
"whisked": 223509, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment