Created
January 25, 2017 02:08
-
-
Save dealingwith/8b0b8d9c520a2a9093f50c1eda25bf7a to your computer and use it in GitHub Desktop.
Simulation of the game "Friends & Neighbors"
This file contains hidden or 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
// 14 chips | |
// 9 squares on board | |
// 5 on the stop sign | |
var lostcount = 0; | |
var wincount = 0; | |
var board = [0,1,2,3,4,5,6,7,8]; | |
var iterations = 1000000; | |
for (var rungame = 0; rungame < iterations; rungame++) { | |
var chips = [0,1,2,3,4,5,6,7,8,9,10,11,12,13]; | |
var lose = 0; | |
for (var i = 1; i < 14; i++) { | |
// console.log(chips.length); | |
var randomindex = Math.floor(Math.random() * chips.length); | |
var chip = chips.splice(randomindex, 1); | |
// console.log(chip[0]); | |
if (!board.includes(chip[0])) lose++; | |
// console.log(lose); | |
if (lose == 5) { | |
// console.log("lost", i, lose); | |
lostcount++; | |
break; | |
} | |
} | |
if (lose < 5) { | |
wincount++; | |
} | |
} | |
console.log("won: ", wincount); | |
console.log("lost: ", lostcount); | |
console.log(wincount / iterations * 100 + "%"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment