2023-09-30 10:23:21 -06:00
|
|
|
function getComputerChoice(){
|
2023-09-30 10:36:53 -06:00
|
|
|
let compAnswer = Math.floor(Math.random() * 3) + 1;
|
|
|
|
|
|
|
|
|
|
if (compAnswer === 1) {
|
|
|
|
|
return "rock";
|
|
|
|
|
} else if (compAnswer === 2) {
|
|
|
|
|
return "paper";
|
|
|
|
|
} else {
|
|
|
|
|
return "scissors"
|
|
|
|
|
}
|
2023-09-30 10:23:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function playRound(playerSelection, computerSelection){
|
2023-09-30 10:47:14 -06:00
|
|
|
p = playerSelection.toLowerCase();
|
|
|
|
|
c = computerSelection.toLowerCase();
|
2023-09-30 10:23:21 -06:00
|
|
|
|
2023-09-30 10:52:40 -06:00
|
|
|
if (p === c) {
|
2023-11-07 12:45:15 -07:00
|
|
|
//console.log("It's a tie!")
|
2023-09-30 10:47:14 -06:00
|
|
|
return "tie";
|
2023-09-30 10:52:40 -06:00
|
|
|
} else if (p === "rock") {
|
2023-09-30 10:47:14 -06:00
|
|
|
if (c === "paper") {
|
|
|
|
|
return "player loses";
|
|
|
|
|
} else { //scissors
|
2023-09-30 10:52:40 -06:00
|
|
|
return "player wins";
|
2023-09-30 10:47:14 -06:00
|
|
|
}
|
2023-09-30 10:52:40 -06:00
|
|
|
} else if (p === "paper") {
|
2023-09-30 10:47:14 -06:00
|
|
|
if (c === "scissors") {
|
|
|
|
|
return "player loses";
|
|
|
|
|
} else { //rock
|
2023-09-30 10:52:40 -06:00
|
|
|
return "player wins";
|
2023-09-30 10:47:14 -06:00
|
|
|
}
|
2023-09-30 10:52:40 -06:00
|
|
|
} else if (p === "scissors") {
|
2023-09-30 12:33:29 -06:00
|
|
|
if (c === "rock") {
|
2023-09-30 10:52:40 -06:00
|
|
|
return "player loses";
|
2023-09-30 12:33:29 -06:00
|
|
|
} else { //paper
|
2023-09-30 10:52:40 -06:00
|
|
|
return "player wins";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return "not a valid entry";
|
|
|
|
|
}
|
2023-09-30 10:23:21 -06:00
|
|
|
}
|
|
|
|
|
|
2023-09-30 12:08:01 -06:00
|
|
|
function game() {
|
|
|
|
|
let p = 0;
|
|
|
|
|
let c = 0;
|
|
|
|
|
|
2023-11-07 12:19:52 -07:00
|
|
|
while (p < 5 && c < 5) {
|
2023-09-30 12:33:29 -06:00
|
|
|
const ps = prompt("Rock, Paper, or Scissors?");
|
2023-09-30 12:08:01 -06:00
|
|
|
const cs = getComputerChoice();
|
|
|
|
|
|
|
|
|
|
play = playRound(ps, cs);
|
|
|
|
|
|
|
|
|
|
switch (play) {
|
|
|
|
|
case "player loses":
|
|
|
|
|
c += 1;
|
2023-11-07 12:45:15 -07:00
|
|
|
console.log("You lose!", cs, " beats", ps, ". You: ", p, "Computer: ", c);
|
2023-09-30 12:08:01 -06:00
|
|
|
break;
|
|
|
|
|
case "player wins":
|
|
|
|
|
p += 1;
|
2023-11-07 12:45:15 -07:00
|
|
|
console.log("You Win!", ps, " beats", cs, ". You: ", p, "Computer: ", c);
|
2023-09-30 12:08:01 -06:00
|
|
|
break;
|
|
|
|
|
case "tie":
|
2023-11-07 12:45:15 -07:00
|
|
|
console.log("It's a tie!", " You: ", p, "Computer: ", c);
|
2023-09-30 12:08:01 -06:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
console.log("Invalid selection. Please choose Rock, Paper or Scissors to play.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-30 12:59:56 -06:00
|
|
|
if (p !== c) {
|
|
|
|
|
let winner = (p > c) ? true : false;
|
2023-09-30 10:23:21 -06:00
|
|
|
|
2023-09-30 12:59:56 -06:00
|
|
|
if (winner) {
|
2023-09-30 13:21:04 -06:00
|
|
|
return "You won the game " + p + " to " + c + "! Great job!" ;
|
2023-09-30 12:59:56 -06:00
|
|
|
} else {
|
2023-09-30 13:21:04 -06:00
|
|
|
return "You lost the game " + p + " to " + c + ". Better luck next time!";
|
2023-09-30 12:59:56 -06:00
|
|
|
}
|
2023-09-30 13:21:04 -06:00
|
|
|
} else if (c === 0) {
|
|
|
|
|
return "Uh... I don't think you played the game right. Refresh the page and try that again."
|
2023-09-30 12:59:56 -06:00
|
|
|
} else {
|
2023-09-30 13:21:04 -06:00
|
|
|
return "Whoa, you tied! Refresh for a rematch!";
|
2023-09-30 12:59:56 -06:00
|
|
|
}
|
2023-09-30 10:23:21 -06:00
|
|
|
}
|
|
|
|
|
|
2023-09-30 12:08:01 -06:00
|
|
|
console.log(game());
|