Last active
August 29, 2015 14:15
-
-
Save tennisonchan/b6d33f701bd3d1657772 to your computer and use it in GitHub Desktop.
example for OO js in 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
var Player = function(name, match_table){ | |
this.name = name; | |
this.rounds = []; | |
this.output_options = []; | |
this.score = 0; | |
this.init = function () { | |
for(var opt in match_table){ | |
this.output_options.push(opt); | |
} | |
} | |
this.play = function(roundId){ | |
// console.log('Player '+this.name+' round num'+roundId); | |
var output = this.output_options[Math.floor(Math.random()* this.output_options.length)]; | |
this.rounds[roundId] = output; | |
return output; | |
} | |
this.win = function(){ | |
// console.log(this.name + ' won'); | |
this.score++; | |
} | |
this.draw = function(){ | |
// console.log(this.name + ' draw'); | |
} | |
this.lose = function(){ | |
// console.log(this.name + ' lose'); | |
} | |
this.init(); | |
} | |
var Game = function(numOfPlayers, numOfRounds) { | |
// this.players = []; | |
var _this = {}, | |
roundId = 0; | |
_this.players = []; | |
_this.nameList = ['elle', 'tenny', 'malcolm', 'ali', 'richard']; | |
_this.records = []; | |
_this.draw_counts = 0; | |
_this.match_table = { | |
rock: { | |
scissors: 1, | |
rock: 0, | |
paper: -1 | |
}, | |
paper: { | |
rock: 1, | |
paper: 0, | |
scissors: -1 | |
}, | |
scissors: { | |
paper: 1, | |
scissors: 0, | |
rock: -1 | |
} | |
} | |
function init () { | |
console.log('Game.init'); | |
for(var i=0;i<numOfPlayers;i++){ | |
_this.players[i] = new Player(_this.nameList[i], _this.match_table); | |
} | |
for(var i=0;i<numOfRounds;i++){ | |
round(); | |
roundId ++; | |
} | |
console.log('% of draw: '+ _this.draw_counts*100/numOfRounds); | |
} | |
function compare (outputs){ | |
var winner; | |
for(var i=0, len=outputs.length;i<len;i++){ | |
var output = outputs[i]; | |
var match = _this.match_table[output]; | |
var result = null; | |
for(var j=0,len=outputs.length; j<len;j++){ | |
if(i===j) continue; | |
if (result === null) { | |
result = match[ outputs[j] ]; | |
} else { | |
result &= match[ outputs[j] ]; | |
} | |
} | |
if (result > 0) { | |
_this.players[i].win(); | |
winner = _this.players[i].name; | |
} else if (result < 0) { | |
_this.players[i].lose(); | |
} else if (result === 0) { | |
_this.players[i].draw(); | |
} | |
} | |
return winner; | |
} | |
function round () { | |
var outputs = [], winner; | |
for(var i=0, len=_this.players.length;i<len;i++){ | |
var player = _this.players[i]; | |
outputs[i] = player.play(roundId); | |
} | |
_this.records.push(outputs); | |
winner = compare(outputs); | |
if(winner){ | |
console.log('The winner is ', winner); | |
} else { | |
console.log('Draw'); | |
_this.draw_counts++; | |
} | |
} | |
init(); | |
return _this; | |
} | |
window.game = new Game(2, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment