Skip to content

Instantly share code, notes, and snippets.

@bluefirex
Last active May 15, 2019 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluefirex/8f723bc238468d417f40c61dee9bb2b4 to your computer and use it in GitHub Desktop.
Save bluefirex/8f723bc238468d417f40c61dee9bb2b4 to your computer and use it in GitHub Desktop.
Demonetization Game (Game of 100)
// Disclaimer:
// This is not made to show off coding skills, but rather to give people an idea how you could simulate the Game of 100 or
// similar instances. If you don't know, what this is, refer to https://www.youtube.com/watch?v=kOnEEeHZp94 (Vsauce2) and/or
// https://www.youtube.com/watch?v=C5-I0bAuEUE (minutephysics)
// Helper for shuffling an array
Array.prototype.shuffle=function(){let t,h,r=this.length;if(0===r)return this;for(;--r;)t=Math.floor(Math.random()*(r+1)),h=this[r],this[r]=this[t],this[t]=h;return this};
// Helper for creating a fixed number of somethings with a range from 0 to count
let makeCollection = (count) => [...new Array(count)].map((x, i) => i)
// Show some output to see what it is doing
let showMessages = true
// Helper for outputting text
let output = (...args) => showMessages ? console.log(...args) : null
// How many trials does each YouTuber have?
let trialsPerYouTuber = 50
// How many times in a row do you want to play the game?
let numberOfGames = 1
// We haven't won a single game, yet
let gameWins = 0
// Each game
for (let g = 0; g < numberOfGames; g++) {
// Create the things involved in this game
let eggs = makeCollection(100).shuffle()
let youtubers = makeCollection(100)
// We haven't lost, yet
let found = true
// every YouTuber now does this:
for (let youtuber of youtubers) {
output('=== YouTuber: ' + youtuber)
// Chose the egg of your own number
var chosenEggs = [ youtuber ]
// I haven't found myself, yet
let foundHimself = false
// I have limited trials
for (let i = 0; i < trialsPerYouTuber; i++) {
// Choose the last egg's content I have remembered
let egg = eggs[chosenEggs[chosenEggs.length - 1]]
// If I found myself
if (egg == youtuber) {
output('... WIN! 🎉 (' + (i + 1) + ' trials)')
output(' Path: ' + JSON.stringify(chosenEggs))
foundHimself = true
break
}
// Otherwise, keep digging
chosenEggs.push(egg)
}
// If I haven't found myself after all trials
if (!foundHimself) {
// We lost the game
found = false
output('... LOSS 😢')
break
}
// Empty line for beauty reasons
output()
}
output()
output('====')
output()
if (found) {
output('You won the game!')
gameWins++
} else {
output('You\'ve lost :(')
}
output()
}
if (numberOfGames > 1) {
output()
output('====')
output()
console.log(`${gameWins} / ${numberOfGames} won, ${gameWins / numberOfGames * 100}%`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment