lundi 4 juillet 2016

Can I define arbitrary rules(something bigger than another) for variables that i can later compare in if/else statements in JavaScript?

I'm writing a rock-paper-scissors-like game in JavaScript. I have it somehow working but it's done more like writing out all the cases rather than making it do comparisons. Looks something like this:

var weapons = ["bomb", "knife", "pistol"];


var player = prompt("Choose your weapon: bomb, knife or a pistol?");
console.log("You picked a " + player + ".");

var  = weapons[Math.floor(Math.random() * weapons.length)];
console.log(" picked a " +  + ".");


while (player == ) {
    var player = prompt("You chose " + player + " and he chose " +  + 
    " - the same weapon. Draw is not an option, so " +
    "try again! Pick bomb, knife or pistol.");
    console.log('...');
    var  = weapons[Math.floor(Math.random() * weapons.length)];
    console.log(" picked a " +  + ".")
}

if (player == "bomb" &&  == "knife") 
    console.log( `...`);

else if (player == "bomb" &&  == "pistol")
    console.log(`...`);

else if (player == "knife" &&  == "pistol")
    console.log('...');

else if (player == "knife" &&  == "bomb")
    console.log(`...`);

else if (player == "pistol" &&  == "bomb")
    console.log(`...`);

else if (player == "pistol" &&  == "knife")
    console.log(`...`);

else
    console.log(`...`);

Is there a way to store rules like (bomb > knife && bomb < pistol) into something and then pass it over to if/else for comparisons? Or maybe I shouldn't do it even if it's possible?

Aucun commentaire:

Enregistrer un commentaire