Skip to content

Instantly share code, notes, and snippets.

@pads
Created Nov 28, 2014
Embed
What would you like to do?
BrumJS Rock Paper Scissors Bot
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