Skip to content

Instantly share code, notes, and snippets.

@dealingwith
Created January 25, 2017 02:08
Show Gist options
  • Save dealingwith/8b0b8d9c520a2a9093f50c1eda25bf7a to your computer and use it in GitHub Desktop.
Save dealingwith/8b0b8d9c520a2a9093f50c1eda25bf7a to your computer and use it in GitHub Desktop.
Simulation of the game "Friends & Neighbors"
// 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