A very very silly implementation of the game Rock-Paper-Scissors
function game(guess){
return (Math.round(guess.charCodeAt()%110/1.2)+1-~~(Math.random()*3))%3-1
}
function gameDebuggable(g, debug){
var b = ~~(Math.random()*3)
, g = Math.round(g.charCodeAt()%110/1.2)-2
, result = (g-b+3)%3-1
if (debug){ debug(b,g,result) }
return result
}
function test(debug){
var n = debug ? 100 : 100000
, i = n
, won = 0
, lost = 0
, g
, opts = ['paper','rock', 'scissors']
function d(b,g,r){
console.log('comp', opts[b])
console.log('player', opts[g])
console.log('result', r)
console.log('------------------' )
}
while(--i>0){
g = gameDebuggable(opts[~~(Math.random()*3)], debug&&d)
if ( g > 0 ){
won++
} else if (g == 0) {
lost++
}
}
return won/n
}
console.log('should be very close to 0.33:',test(1))
Please take note, that I was just kidding around with this, so there are probably many shorter versions of the same functionality. I was not trying to find those.
The two game functions are essentially the same, only difference is that one of them accepts a function for debugging.
Written by Ashnur
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Paper
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#