Created
April 29, 2015 18:56
-
-
Save radiodario/1b54cd500c3e84bcab67 to your computer and use it in GitHub Desktop.
Higher-Lower game
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
// Fisher-Yates Shuffle. | |
function shuffle(array) { | |
var currentIndex = array.length, temporaryValue, randomIndex ; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} | |
// calculate P(lower|value + seen) | |
function probabilityLowerCardGiven(value, seen, deck) { | |
var lowerCardsRemaining = 4 * (value - 1); | |
// check the ones that have been played | |
for (var i = 0; i < seen.length; i++) { | |
if (seen[i] < value) { | |
lowerCardsRemaining--; | |
} | |
} | |
return (lowerCardsRemaining / deck.length); | |
} | |
// calculate P(higher|value + seen) | |
function probabilityHigherCardGiven(value, seen, deck) { | |
var higherCardsRemaining = 4 * (13 - value); | |
// check the ones that have been played | |
for (var i = 0; i < seen.length; i++) { | |
if (seen[i] > value) { | |
higherCardsRemaining--; | |
} | |
} | |
return (higherCardsRemaining / deck.length); | |
} | |
function playGame() { | |
var deck = shuffle([ | |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // hearts | |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // clubs | |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // diamonds | |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // spades | |
]); | |
var seen = []; | |
var score = 0; | |
var card = deck.pop(); | |
var next = deck.pop(); | |
var pLo, pHi; | |
// go over the whole deck | |
while (deck.length > 0) { | |
pLo = probabilityLowerCardGiven(card, seen, deck); | |
pHi = probabilityHigherCardGiven(card, seen, deck); | |
// play rationally. | |
// we go lower | |
if (pLo > pHi) { | |
// test if we guessed lower | |
if (next < card) { | |
score++; | |
} | |
} | |
// we go higher | |
else if (pLo < pHi) { | |
if (next > card) score++; | |
} | |
seen.push(card); | |
card = next; | |
next = deck.pop(); | |
} | |
return score; | |
} | |
function ComputeAverageScore() { | |
var numGames = 10000; | |
var sumScore = 0; | |
var scores = []; | |
var thisScore; | |
for (var i = 0; i < numGames; i++) { | |
thisScore = playGame(); | |
sumScore += thisScore; | |
scores.push(thisScore); | |
} | |
console.log(scores, sumScore, scores.length); | |
console.log('average score is ', sumScore/scores.lenght); | |
return { | |
avgScore: sumScore/numGames, | |
scores: scores | |
}; | |
} | |
var scores = ComputeAverageScore(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment