Rock Paper Scissors javascript using matrix -
hello new here , taking javascript course @ university software development degree. running issue in rock paper scissors lizard spock final project. professor has given qunit tests can check our code not @ understanding how errors show me whats wrong don't understand how code work. far have created objects , gave them properties , methods. created matrix capture , retain variations of win conditions. don't understand how create final method need: getwinner. in our rubric states following:
"method evaluate won game. should update wins, losses, , rate of each player. should set "winner" property name of winning player."
here game.js file working on. @ bottom stumped how start getwinner function. have started not responding unit tests. can post code if need well. other file object.
var game = function(){ this.running = false; this.name = "rpsls"; this.players = []; this.addplayer = function(name){ var player = new player(name); this.players.push(player); return player; }; this.getplayer = function(number){ var player_number = number - 1; return this.players[player_number]; }; this.sethand = function(number, hand){ var p = this.getplayer(number); p.hand = hand; }; this.matrix = { rock: { scissors: true, lizard: true }, paper: { rock: true, spock: true }, scissors: { paper: true, lizard: true }, lizard: { spock: true, paper: true }, spock: { rock: true, scissors: true } }; this.getwinner = function(){ var player1 = this.getplayer(); var player2 = this.getplayer(); if ( this.matrix [ player1 ] == [ player2 ] ) { } else if ( this.matrix [player1] [player2] ) {}; }
};
thank reading through , trying help.
you mixing object
, array
. object
in question key-value list in curly braces (your matrix
variable). , set key in of such list calling name: matrix["key"] = value
. such key-value list can nested. deeply. see example json. parsing such nested lists complicated , error prone (there ready-to-use libraries json) not use them if not absolutely necessary.
i simplified ansatz extend used arrays , objects possible.
var game = function(){ // unused this.running = false; this.name = "rpsls"; // object (key-value list) hold players this.allplayers = {}; // object "player"" hold 1 player var player = function(){ this.name = ""; this.hand = ""; }; this.addplayer = function(pname){ // memory 1 new player var player = new player(); // set name of player player.name = pname; // put player-object player-list this.allplayers[pname] = player; return player; }; this.getplayer = function(pname){ return this.allplayers[pname]; }; this.sethand = function(pname, phand){ this.allplayers[pname].hand = phand; }; /* rock <-> paper <-> scissor <-> lizard <-> spock (vulcan) if column beats row -> 1 if column looses row -> -1 if tie -> 0 0 1 2 3 4 r p s l v 0 r 0 -1 1 1 -1 1 p 1 0 -1 -1 1 2 s -1 1 0 1 -1 3 l -1 1 -1 0 1 4 v 1 -1 1 -1 0 */ // translate name place in table above this.rpslvenum = { rock: 0, paper: 1, scissor: 2, lizard: 3, spock: 4 }; this.scheme = [ [ 0, -1, 1, 1, -1], [ 1, 0, -1, -1, 1], [-1, 1, 0, 1, -1], [-1, 1, -1, 0, 1], [ 1, -1, 1, -1, 0] ]; this.getwinner = function(){ var player1 = this.addplayer("kirk"); var player2 = this.addplayer("uhura"); this.sethand("kirk","scissor"); this.sethand("uhura","spock"); var result = this.scheme[this.rpslvenum[player1.hand]] [this.rpslvenum[player2.hand]]; switch(result){ case 0: return "it's tie"; case 1: return "player 1 had won"; case -1: return "player 2 had won"; default: throw new error("something unexpected happend in " + this.name);break; } }; }; var g = new game(); console.log(g.getwinner());
it lacks lot of obligatory checks&balances , quite unorganized hope suffices along now, otherwise…well…you know ask ;-)
Comments
Post a Comment