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-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") {
|
|
|
|
|
if (c === "paper") {
|
|
|
|
|
return "player loses";
|
|
|
|
|
} else { //scissors
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 5; i++) {
|
|
|
|
|
const ps = prompt("Rock, Paper or Scissors?");
|
|
|
|
|
const cs = getComputerChoice();
|
|
|
|
|
|
|
|
|
|
play = playRound(ps, cs);
|
|
|
|
|
|
|
|
|
|
switch (play) {
|
|
|
|
|
case "player loses":
|
|
|
|
|
c += 1;
|
|
|
|
|
console.log("You lost!");
|
|
|
|
|
break;
|
|
|
|
|
case "player wins":
|
|
|
|
|
p += 1;
|
|
|
|
|
console.log("You won!");
|
|
|
|
|
break;
|
|
|
|
|
case "tie":
|
|
|
|
|
p += 1;
|
|
|
|
|
c += 1;
|
|
|
|
|
console.log("It's a tie!");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
console.log("Invalid selection. Please choose Rock, Paper or Scissors to play.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log("Computer won " + c + " games");
|
|
|
|
|
console.log("Player won " + p + " games");
|
|
|
|
|
|
2023-09-30 10:23:21 -06:00
|
|
|
//playRound(playerSelection, computerSelection)
|
|
|
|
|
|
|
|
|
|
//count how many times p and c have won
|
|
|
|
|
|
|
|
|
|
//return whether or not p has won
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-30 12:08:01 -06:00
|
|
|
console.log(game());
|