Last active
August 29, 2015 14:10
-
-
Save skinofstars/5b7672dc4d2faff28224 to your computer and use it in GitHub Desktop.
BrumJS - rock paper scissors
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
// BrumJS - Rock Paper Scissors bots game | |
// http://www.meetup.com/Brum-JS/events/218927668/ | |
// https://github.com/mancjs/rock-paper-scissors | |
// water beats dynamite | |
// dynamite beats everything (max 5 use) | |
// for keeping track of some stuff | |
var game = {}; | |
game.me = {}; | |
game.them = {}; | |
game.me.dynamite_remain = 5; | |
game.them.dynamite_remain = 5; | |
var moves = ['rock', 'paper', 'scissors']; | |
/** | |
* First run, lets you know who you're playing | |
* | |
* @param String opponent | |
*/ | |
var init = function(opponent) { | |
game.them.name = opponent; | |
}; | |
/** | |
* Game loop calls this 50 times | |
* | |
* @return String 'rock', 'paper', 'scissors', 'water', 'dynamite' | |
*/ | |
var play = function() { | |
// ME dynamite | |
// do we have any dynamite? | |
if (game.me.dynamite_remain > 0) { | |
// one in 10 chance of dynamite | |
if (randomMove(10) === 5) { | |
game.me.dynamite_remain--; | |
return 'dynamite'; | |
} | |
} | |
// THEM dynamite | |
// only play water if they have dynamite remaining | |
if (game.them.dynamite_remain > 0) { | |
// one in 10 chance of water.. i have no idea what the optimal is | |
if (randomMove(20) === 5) { | |
return 'water'; | |
} | |
} | |
var pos = randomMove(3); | |
return moves[pos]; | |
}; | |
/** | |
* The called in game loop. | |
* | |
* @param Object res - who won, who played what. keyed to player names. | |
*/ | |
var result = function(res) { | |
// res.winner = 'foo'; | |
// res.foo = 'paper'; | |
// res.bar = 'rock'; | |
if (res[game.them.name] === 'dynamite') { | |
game.them.dynamite_remain--; | |
} | |
}; | |
function randomMove(size) { | |
if (typeof(size) === 'undefined') { | |
size = 3 | |
}; | |
return parseInt(Math.random() * 100) % size; | |
} | |
// TESTS - uncomment to run | |
// just avoids basic syntax errors by excercising code. | |
// var last_hand = {}; | |
// last_hand.winner = 'foo'; | |
// last_hand.foo = 'paper'; | |
// last_hand.bar = 'rock'; | |
// init('foo'); | |
// var games = 50; | |
// do { | |
// console.log(play()); | |
// | |
// result(last_hand); | |
// | |
// games--; | |
// } | |
// while (games > 0); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment