Rock, Paper, Scissors, Lizard, Spock in JavaScript -
i'm kind of new javascript. started learning it, , decided make 'rock, paper, scissors, lizard, spock' game. here code:
var userchoice = prompt("do choose rock, paper, scissors, lizard, or spock?") var computerchoice = math.random(); if (computerchoice < 0.2) { computerchoice = "rock"; } else if (computerchoice <= 0.4) { computerchoice = "paper"; } else if (computerchoice <= 0.6) { computerchoice = "scissors"; } else if (computerchoice <= 0.8) { computerchoice = "lizard"; } else { computerchoice = "spock"; } alert("the computer chose " + computerchoice); var compare = function(choice1, choice2){ if (choice1 === choice2) { alert("and... it's tie!"); } //if user chose rock... else if (choice1 === "rock") { if (choice2 === "scissors") { alert("rock wins!"); } else if (choice2 === "paper") { alert("paper wins!"); } else if (choice2 === "lizard") { alert("rock wins!"); } else { alert("spock wins!"); } } //if user chose paper... else if (choice1 === "paper") { if (choice2 === "scissors") { alert("scissors wins!"); } else if (choice2 === "rock") { alert("paper wins!"); } else if (choice2 === "lizard") { alert("lizard wins!"); } else { alert("paper wins!"); } } //if user chose scissors... else if (choice1 === "scissors") { if (choice2 === "paper") { alert("scissors wins!"); } else if (choice2 === "rock") { alert("rock wins!"); } else if (choice2 === "lizard") { alert("scissors wins!"); } else { alert("spock wins!"); } } //if user chose lizard... else if (choice1 === "lizard") { if (choice2 === "scissors") { alert("scissors wins!"); } else if (choice2 === "rock") { alert("rock wins!"); } else if (choice2 === "paper") { alert("lizard wins!"); } else { alert("lizard wins!"); } } //if user chose spock... else if (choice1 === "spock") { if (choice2 === "scissors") { alert("spock wins!"); } else if (choice2 === "rock") { alert("spock wins!"); } else if (choice2 === "lizard") { alert("lizard wins!"); } else { alert("paper wins!"); } } }; compare(userchoice, computerchoice); there 2 main things want add code don't know how to:
right now, if user inputs, example, 'rock' capital 'r', doesn't recognized 1 of 5 valid inputs (rock, paper, scissors, lizard, , spock). there way make if user inputs valid capital letter (or letters) still valid?
i want add whenever puts in invalid (e.g. "sloth") alert them input invalid , ask them, again, put in rock, paper, scissors, lizard, or spock.
lets go object oriented on this. reduce repitition in logic:
//set choices can beat //this hash table of objects can referecne name var choices = {rock : {name: "rock", defeats: ["scissors","lizard"]}, paper: {name: "paper", defeats: ["rock", "spock"]}, scissors: {name: "scissors", defeats: ["paper", "lizard"]}, lizard: {name: "lizard", defeats:["paper","spock"]}, spock: {name: "spock", defeats:["scissors","rock"]} }; //get computers choice var computerchoice = math.random(); if (computerchoice < 0.2) { computerchoice = "rock"; } else if (computerchoice <= 0.4) { computerchoice = "paper"; } else if (computerchoice <= 0.6) { computerchoice = "scissors"; } else if (computerchoice <= 0.8) { computerchoice = "lizard"; } else { computerchoice = "spock"; } //get users choice, normalising lower case var userchoice = prompt("do choose rock, paper, scissors, lizard, or spock?").tolowercase(); alert("the computer chose " + computerchoice); //check tie if(computerchoice == userchoice){ alert("it's tie"); //check valid choice }else if(choices[userchoice] === undefined){ alert("invalid choice"); }else{ //get chosen 1 object userchoice = choices[userchoice]; //check win /*var victory = false; for(var = 0; < userchoice.defeats.length; i++){ if(computerchoice == userchoice.defeats[i]) { victory = true; break; } }*/ //improved check, inspired mke spa guy var victory = userchoice.defeats.indexof(computerchoice) > -1; //display result if(victory) { alert("vitory! " + userchoice.name + " wins!") }else{ alert("defeat, " + computerchoice + " wins!"); } } thats it, , spocks' uncle.
demo full action : e.g: paper covers rock;
more reading:
https://developer.mozilla.org/en-us/docs/web/javascript/introduction_to_object-oriented_javascript
https://developer.mozilla.org/en-us/docs/web/javascript/guide/working_with_objects
Comments
Post a Comment