Rock, paper, scissors with my own creative twist.
- Code:
console.log(" Nuke, Foot, Cockroach!");
console.log(" Made By Shadow@BWH");
/*Variables and game setup*/
var played = 0;
var score1 = 0;
var score2 = 0;
var times = Number(prompt("How often do you want to play?"));
if (isNaN(times)) {
times = 1;
console.log("\nInvalid input!\nNumber of rounds set to 1!");
}
else console.log("\nYou chose to play "+ times +" times.");
/*FUNCTION COMPARING CHOICES*/
var compare=function(choice1,choice2) {
if (choice1===choice2) {
console.log("Its a tie. Choose again!");
}
else if (choice1==="nuke") {
if (choice2==="foot") {
console.log("Nuke blows up foot.");
console.log("Player 1 wins this round.");
score1+=1;
}
else if (choice2==="cockroach") {
console.log("cockroach survives nuke.");
console.log("The computer wins this round.");
score2+=1;
}
}
else if (choice1==="foot") {
if (choice2==="cockroach") {
console.log("Foot crushes cockroach.");
console.log("Player 1 wins this round.");
score1+=1;
}
else if (choice2==="nuke") {
console.log("Nuke blows up foot.");
console.log("The computer wins this round.");
score2+=1;
}
}
else if (choice1==="cockroach") {
if (choice2==="foot") {
console.log("Foot crushes cockroach.");
console.log("The computer wins this round.");
score2+=1;
}
else if (choice2==="nuke") {
console.log("Cockroach survives nuke.");
console.log("Player 1 wins this round.");
score1+=1;
}
}
};
/*END OF FUNCTION*/
do { // repeats game x times, as specified by player
played += 1;
console.log("\nRound "+ played+"/"+times);
do { // repeats code as long as game is tied
do { // repeats code until player enters proper input
if (userChoice==="rules") {
console.log("\nRules:\nNuke blows up foot.");
console.log("Foot crushes cockroach.");
console.log("Cockroach survives nuke.");
}
var userChoice = prompt("Do you choose nuke, foot or cockroach?","Type <rules> to see the rules.").toLowerCase();
} while (userChoice !="nuke" && userChoice !="foot" && userChoice !="cockroach");
var computerChoice = Math.random();
if (computerChoice < 0.2) {
computerChoice = "nuke";
} else if (computerChoice <= 0.4) {
computerChoice = "foot";
} else if (computerChoice <=0.6) {
}
else {
computerChoice = "cockroach";
}
console.log("\nYour choice: "+ userChoice);
console.log("Computer: " + computerChoice);
compare(userChoice,computerChoice);
} while (userChoice === computerChoice);
} while (played <= times-1); // times minus 1 because its a do-loop
console.log("\nFinal Score: "+ score1+":"+score2);
if (score1>score2) {
console.log("Player 1 is the overall winner!");
}
else if (score2>score1) {
console.log("The computer is the overall winner!");
}
else {
console.log("It's a tie after all.");
}