Created
September 21, 2024 03:33
-
-
Save alexchristianqr/1ad3105464dd4bc9b13fd75610a07b3a to your computer and use it in GitHub Desktop.
piedra-papel-tijera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const opcionesJuego = { | |
piedra: { | |
piedra: 3, // Empatar | |
tijera: 2, // Perder | |
papel: 1, // Ganar | |
}, | |
papel: { | |
papel: 3, // Empatar | |
tijera: 1, // Ganar | |
piedra: 2 // Perder | |
}, | |
tijera: { | |
tijera: 3, // Empatar | |
piedra: 1, // Ganar | |
papel: 2, // Perder | |
} | |
} | |
function seleccionPC(){ | |
return Math.floor(Math.random() * 3); // Retorna un indice | |
} | |
function jugar(opcionUsuario) { | |
const opcionPC = seleccionPC(); | |
const posiblesRespuestas = opcionesJuego[opcionUsuario]; | |
const respuestaPC = Object.values(posiblesRespuestas)[opcionPC]; | |
let label = null | |
for(let index in Object.values(posiblesRespuestas) ){ | |
if(Object.values(posiblesRespuestas)[index] === respuestaPC){ | |
label = Object.keys(posiblesRespuestas)[index] | |
} | |
} | |
alert("La PC dijo: "+ label) | |
switch(respuestaPC){ | |
case 1:// Ganar | |
alert("Perdiste"); | |
break; | |
case 2: // Perder | |
alert("Ganaste") | |
break; | |
case 3: // Empatar | |
alert("Empataste") | |
break; | |
} | |
const otraVez = confirm("¿Quieres volver a jugar?"); | |
if(otraVez) start(); | |
} | |
function start() { | |
const opcionUsuario = prompt("Ingresa una opción: piedra, papel o tijera"); | |
const esUnaLlave = Object.keys(opcionesJuego).includes(opcionUsuario); | |
if((opcionUsuario === "" || opcionUsuario) && !esUnaLlave) { | |
alert("Error: No es una opción válida."); | |
start() | |
} | |
if(opcionUsuario && esUnaLlave){ | |
jugar(opcionUsuario); | |
} | |
} | |
start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment