Created
November 28, 2014 09:17
-
-
Save pads/19065e2869f0ba9db6f2 to your computer and use it in GitHub Desktop.
BrumJS Rock Paper Scissors Bot
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 hands = [ | |
'rock', | |
'paper', | |
'scissors', | |
'water', | |
'dynamite' | |
]; | |
var opponents = [] | |
var Opponent = function(name) { | |
this.name = name; | |
this.theirPlays = { | |
rock: 0, | |
paper: 0, | |
scissors: 0, | |
water: 0, | |
dynamite: 0 | |
}; | |
this.ourPlays = { | |
rock: 0, | |
paper: 0, | |
scissors: 0, | |
water: 0, | |
dynamite: 0 | |
} | |
this.totalPlays = 0; | |
}; | |
Opponent.prototype.rememberTheirPlay = function(result) { | |
var theirPlay = result[this.name]; | |
this.theirPlays[theirPlay] += 1; | |
}; | |
Opponent.prototype.rememberOurPlay = function(play) { | |
this.ourPlays[play] += 1; | |
this.totalPlays++; | |
}; | |
var currentOpponent; | |
var init = function(opponentName) { | |
currentOpponent = new Opponent(opponentName); | |
}; | |
var play = function() { | |
if((50 - currentOpponent.totalPlays) <= (5 - currentOpponent.ourPlays.dynamite) ) { | |
currentOpponent.rememberOurPlay('dynamite'); | |
return "dynamite"; | |
} | |
if((50 - currentOpponent.totalPlays) <= (2 - currentOpponent.theirPlays.dynamite) ) { | |
currentOpponent.rememberOurPlay('water'); | |
return "water"; | |
} | |
var ourHand = selectHand(); | |
if(currentOpponent.ourPlays.dynamite === 5) { | |
while(ourHand === 'dynamite') { | |
ourHand = selectHand(); | |
} | |
}; | |
currentOpponent.rememberOurPlay(ourHand); | |
if(currentOpponent.name === 'test') { | |
console.log("'" + ourHand + "'"); | |
if(ourHand == '') { alert('BLANK'); } | |
} | |
return ourHand; | |
}; | |
var result = function(result) { | |
currentOpponent.rememberTheirPlay(result); | |
}; | |
function selectHand() { | |
var ourHand = ''; | |
var randomNumber = getRandomNumber(); | |
if (randomNumber < 10) { | |
ourHand = 'dynamite'; | |
} else if(randomNumber < 23) { | |
ourHand = 'rock'; | |
} else if (randomNumber < 36) { | |
ourHand = 'paper'; | |
} else if (randomNumber < 50) { | |
ourHand = 'scissors'; | |
} else { | |
ourHand = 'water'; | |
} | |
return ourHand; | |
} | |
function getRandomNumber() { | |
var min = 0; | |
var max = 50; | |
return Math.floor(Math.random() * (max - min)) + min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment