Advance game logic to be player vs bot instead of bot vs bot.

This commit is contained in:
nodemixaholic 2024-03-10 20:36:48 +00:00
parent 74d5d4d6d9
commit 0926d5a0fc

20
rps.js
View file

@ -14,30 +14,24 @@ function playRPS() {
player1Choice = "Scissors";
}
if (Math.abs(player2 - 1) < Math.abs(player2 - 2) && Math.abs(player2 - 1) < Math.abs(player2 - 3)) {
player2Choice = "Rock";
} else if (Math.abs(player2 - 2) < Math.abs(player2 - 1) && Math.abs(player2 - 2) < Math.abs(player2 - 3)) {
player2Choice = "Paper";
} else {
player2Choice = "Scissors";
}
player2Choice = prompt("Rock, Paper, or Scissors?")
// Determine the winner based on game logic
let winner;
if (player1Choice === player2Choice) {
winner = "Tie";
} else if (player1Choice === "Rock") {
winner = player2Choice === "Scissors" ? "Player 1" : "Player 2";
winner = player2Choice === "Scissors" ? "Bot" : "Player";
} else if (player1Choice === "Paper") {
winner = player2Choice === "Rock" ? "Player 1" : "Player 2";
winner = player2Choice === "Rock" ? "Bot" : "Player";
} else { // player1Choice === "Scissors"
winner = player2Choice === "Paper" ? "Player 1" : "Player 2";
winner = player2Choice === "Paper" ? "Bot" : "Player";
}
// Print the results
console.log(`Player 1: ${player1Choice}`);
console.log(`Player 2: ${player2Choice}`);
console.log(`Winner: ${winner}`);
alert(`Bot: ${player1Choice}
Player: ${player2Choice}
Winner: ${winner}`);
}
// Play the game twice (once per player)