-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPPT.JS
More file actions
49 lines (42 loc) · 1.58 KB
/
Copy pathPPT.JS
File metadata and controls
49 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Variables para el marcador
let playerScore = 0;
let computerScore = 0;
// Función para que la computadora elija una opción al azar
function getComputerChoice() {
const choices = ["piedra", "papel", "tijeras"];
const randomIndex = Math.floor(Math.random() * choices.length);
return choices[randomIndex];
}
// Función para determinar el ganador
function determineWinner(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return "¡Empate!";
}
if (
(playerChoice === "piedra" && computerChoice === "tijeras") ||
(playerChoice === "papel" && computerChoice === "piedra") ||
(playerChoice === "tijeras" && computerChoice === "papel")
) {
playerScore++;
return "¡Ganaste!";
} else {
computerScore++;
return "Perdiste...";
}
}
// Actualizar el marcador
function updateScoreboard() {
document.getElementById("player-score").textContent = playerScore;
document.getElementById("computer-score").textContent = computerScore;
}
// Manejar la elección del jugador
function playGame(playerChoice) {
const computerChoice = getComputerChoice();
const resultMessage = determineWinner(playerChoice, computerChoice);
document.getElementById("resultado").textContent = `Elegiste: ${playerChoice}.
La computadora eligió: ${computerChoice}. ${resultMessage}`;
updateScoreboard();
}
document.getElementById("piedra").addEventListener("click", () => playGame("piedra"));
document.getElementById("papel").addEventListener("click", () => playGame("papel"));
document.getElementById("tijeras").addEventListener("click", () => playGame("tijeras"));