2024-03-05 10:58:34 -06:00
|
|
|
// Function to play rock paper scissors
|
|
|
|
function playRPS() {
|
|
|
|
// Generate two random numbers between 1 and 3 (inclusive)
|
|
|
|
const player1 = Math.floor(Math.random() * 3) + 1;
|
|
|
|
const player2 = Math.floor(Math.random() * 3) + 1;
|
|
|
|
|
|
|
|
// Determine the choices based on the closest distance
|
|
|
|
let player1Choice, player2Choice;
|
|
|
|
if (Math.abs(player1 - 1) < Math.abs(player1 - 2) && Math.abs(player1 - 1) < Math.abs(player1 - 3)) {
|
|
|
|
player1Choice = "Rock";
|
|
|
|
} else if (Math.abs(player1 - 2) < Math.abs(player1 - 1) && Math.abs(player1 - 2) < Math.abs(player1 - 3)) {
|
|
|
|
player1Choice = "Paper";
|
|
|
|
} else {
|
|
|
|
player1Choice = "Scissors";
|
|
|
|
}
|
|
|
|
|
2024-03-10 15:36:48 -05:00
|
|
|
player2Choice = prompt("Rock, Paper, or Scissors?")
|
2024-03-05 10:58:34 -06:00
|
|
|
|
|
|
|
// Determine the winner based on game logic
|
|
|
|
let winner;
|
|
|
|
if (player1Choice === player2Choice) {
|
|
|
|
winner = "Tie";
|
|
|
|
} else if (player1Choice === "Rock") {
|
2024-03-10 15:36:48 -05:00
|
|
|
winner = player2Choice === "Scissors" ? "Bot" : "Player";
|
2024-03-05 10:58:34 -06:00
|
|
|
} else if (player1Choice === "Paper") {
|
2024-03-10 15:36:48 -05:00
|
|
|
winner = player2Choice === "Rock" ? "Bot" : "Player";
|
2024-03-05 10:58:34 -06:00
|
|
|
} else { // player1Choice === "Scissors"
|
2024-03-10 15:36:48 -05:00
|
|
|
winner = player2Choice === "Paper" ? "Bot" : "Player";
|
2024-03-05 10:58:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print the results
|
2024-03-10 15:36:48 -05:00
|
|
|
alert(`Bot: ${player1Choice}
|
|
|
|
Player: ${player2Choice}
|
|
|
|
Winner: ${winner}`);
|
2024-03-05 10:58:34 -06:00
|
|
|
}
|
|
|
|
|
2024-03-05 11:01:36 -06:00
|
|
|
// Play the game twice (once per player)
|
2024-03-05 10:58:34 -06:00
|
|
|
playRPS();
|