Last active
August 29, 2015 14:24
-
-
Save jeriko/869a5ce110d1ca1c7c12 to your computer and use it in GitHub Desktop.
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
var api = { | |
move: Move, | |
reset: Reset, | |
read: function(selector) { return parseInt($(selector).innerHTML); } | |
}; | |
var scores = { | |
man: function() { return api.read('#manscore'); }, | |
machine: function() { return api.read('#machscore'); } | |
}; | |
var percentages = { | |
man: function() { return api.read('#manper'); }, | |
machine: function() { return api.read('#machper'); } | |
}; | |
var tactic = { | |
makeMove: function() { | |
var moveValue = Math.round(Math.random()); | |
api.move(moveValue); | |
} | |
}; | |
var round = { | |
playPerfectly: function() { | |
api.reset(); | |
while(scores.machine() == 0) { tactic.makeMove(); }; | |
return { playsCount: scores.man() }; | |
}, | |
playReasonably: function(percentageThreshold) { | |
api.reset(); | |
while(percentages.machine() <= percentageThreshold) { tactic.makeMove(); } | |
return { playsCount: scores.man() } | |
} | |
}; | |
var player = { | |
playUntilStreak: function(streakGoal) { | |
var lastStreakCount = 0; | |
var attemptCount = 0; | |
while(lastStreakCount < streakGoal) { | |
attemptCount += 1; | |
lastStreakCount = round.playPerfectly().playsCount; | |
}; | |
return { | |
attempts: attemptCount, | |
winningPlaysCount: lastStreakCount, | |
}; | |
}, | |
playForThreshold: function(percentageThreshold, maxAttemptsCount) { | |
var plays = [] | |
for(var i = 0; i < maxAttemptsCount; i++) { | |
plays.push(round.playReasonably().playsCount); | |
} | |
return { | |
winningPlaysAttemptsCount: plays | |
} | |
} | |
}; | |
var api = { | |
move: Move, | |
reset: Reset, | |
read: function(selector) { return parseInt($(selector).innerHTML); } | |
}; | |
var scores = { | |
man: function() { return api.read('#manscore'); }, | |
machine: function() { return api.read('#machscore'); } | |
}; | |
var percentages = { | |
man: function() { return api.read('#manper'); }, | |
machine: function() { return api.read('#machper'); } | |
}; | |
var tactic = { | |
makeMove: function() { | |
var moveValue = Math.round(Math.random()); | |
api.move(moveValue); | |
} | |
}; | |
var round = { | |
playPerfectly: function() { | |
api.reset(); | |
while(scores.machine() == 0) { tactic.makeMove(); }; | |
return { playsCount: scores.man() }; | |
}, | |
playReasonably: function(percentageThreshold) { | |
api.reset(); | |
while(percentages.machine() <= percentageThreshold) { tactic.makeMove(); } | |
return { playsCount: scores.man() } | |
} | |
}; | |
var player = { | |
playUntilStreak: function(streakGoal) { | |
var lastStreakCount = 0; | |
var attemptCount = 0; | |
while(lastStreakCount < streakGoal) { | |
attemptCount += 1; | |
lastStreakCount = round.playUntilStreak().playsCount; | |
}; | |
return { | |
attempts: attemptCount, | |
winningPlaysCount: lastStreakCount, | |
}; | |
}, | |
playForThreshold: function(percentageThreshold, iterations) { | |
var plays = [] | |
for(var i = 0; i < iterations; i++) { | |
var play = round.playReasonably(percentageThreshold).playsCount; | |
plays.push(play); | |
} | |
return { | |
winningPlaysAttemptsCount: plays | |
} | |
} | |
}; | |
var percentageThreshold = 50; | |
var iterations = 100; | |
var playAttempts = player.playForThreshold(percentageThreshold, iterations).winningPlaysAttemptsCount; | |
var playsTotal = 0; | |
for(var i = 0; i < playAttempts.length; i++) { | |
playsTotal += playAttempts[i]; | |
} | |
var playsAverage = playsTotal / playAttempts.length; | |
console.log("Played " + iterations + " games until machine was at " + percentageThreshold + "%"); | |
console.log("Average number of wins: " + playsAverage); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment