2023-11-07 14:52:42 -07:00
|
|
|
|
2023-11-07 20:33:42 -07:00
|
|
|
let pScore = 0;
|
|
|
|
|
let cScore = 0;
|
|
|
|
|
let ps; // Define the external variable for user's choice
|
|
|
|
|
const buttons = document.querySelectorAll('button');
|
|
|
|
|
|
2023-11-07 21:24:44 -07:00
|
|
|
|
2023-11-07 20:33:42 -07:00
|
|
|
buttons.forEach((button) => {
|
|
|
|
|
button.addEventListener('click', () => {
|
|
|
|
|
ps = button.id; // Assign the button's id to the external variable
|
|
|
|
|
game(); // Start the game when the user makes a choice
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-07 21:24:44 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-11-07 21:35:33 -07:00
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-30 10:23:21 -06:00
|
|
|
}
|
|
|
|
|
|
2023-11-07 21:35:33 -07:00
|
|
|
|
2023-11-07 20:28:51 -07:00
|
|
|
function game() {
|
2023-11-07 20:33:42 -07:00
|
|
|
if (pScore < 5 && cScore < 5) {
|
2023-09-30 12:08:01 -06:00
|
|
|
const cs = getComputerChoice();
|
|
|
|
|
|
|
|
|
|
play = playRound(ps, cs);
|
|
|
|
|
|
|
|
|
|
switch (play) {
|
|
|
|
|
case "player loses":
|
2023-11-07 20:33:42 -07:00
|
|
|
cScore += 1;
|
|
|
|
|
console.log("You lose!", cs, " beats", ps, ". You: ", pScore, "Computer: ", cScore);
|
2023-09-30 12:08:01 -06:00
|
|
|
break;
|
|
|
|
|
case "player wins":
|
2023-11-07 20:33:42 -07:00
|
|
|
pScore += 1;
|
|
|
|
|
console.log("You Win!", ps, " beats", cs, ". You: ", pScore, "Computer: ", cScore);
|
2023-09-30 12:08:01 -06:00
|
|
|
break;
|
|
|
|
|
case "tie":
|
2023-11-07 20:33:42 -07:00
|
|
|
console.log("It's a tie!", " You: ", pScore, "Computer: ", cScore);
|
2023-09-30 12:08:01 -06:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
console.log("Invalid selection. Please choose Rock, Paper or Scissors to play.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 21:24:44 -07:00
|
|
|
if (pScore === 5 || cScore === 5) {
|
|
|
|
|
console.log(endGame());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 21:35:33 -07:00
|
|
|
|
2023-11-07 21:24:44 -07:00
|
|
|
function endGame(){
|
2023-11-07 21:35:33 -07:00
|
|
|
let winner = (pScore > cScore) ? true : false;
|
2023-09-30 10:23:21 -06:00
|
|
|
|
2023-11-07 21:35:33 -07:00
|
|
|
if (winner) {
|
|
|
|
|
return "You won the game " + pScore + " to " + cScore + "! Great job!" ;
|
2023-09-30 12:59:56 -06:00
|
|
|
} else {
|
2023-11-07 21:35:33 -07:00
|
|
|
return "You lost the game " + pScore + " to " + cScore + ". Better luck next time!";
|
2023-09-30 12:59:56 -06:00
|
|
|
}
|
2023-11-07 21:35:33 -07:00
|
|
|
|
2023-11-07 21:24:44 -07:00
|
|
|
}
|