Skip to content

Instantly share code, notes, and snippets.

@manuelojeda
Created September 24, 2020 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelojeda/b237cdb5c22f7628112c54f60738390f to your computer and use it in GitHub Desktop.
Save manuelojeda/b237cdb5c22f7628112c54f60738390f to your computer and use it in GitHub Desktop.
class Juego {
opciones = [
{
objeto: 'Piedra',
vence: 'Tijeras',
empate: 'Piedra'
},
{
objeto: 'Papel',
vence: 'Piedra',
empate: 'Papel'
},
{
objeto: 'Tijeras',
vence: 'Papel',
empate: 'Tijeras'
},
]
msg = `
Elige entre, piedra papel o tijeras,
Piedra = 0,
Papel = 1,
Tijeras = 2
`
iniciar () {
const eleccion = window.prompt(this.msg)
this.calcular(eleccion)
}
calcular (humano) {
const cpu = this.getRandom()
if (this.opciones[humano].vence === this.opciones[cpu].objeto) {
alert(`
'Haz ganado!'
Tu: ${this.opciones[humano].objeto}
CPU: ${this.opciones[cpu].objeto}
`)
} else if (this.opciones[humano].objeto === this.opciones[cpu].objeto) {
alert(`
'Haz Empatado!'
Tu: ${this.opciones[humano].objeto}
CPU: ${this.opciones[cpu].objeto}
`)
} else if (this.opciones[cpu].vence === this.opciones[humano].objeto) {
alert(`
'Haz perdido!'
Tu: ${this.opciones[humano].objeto}
CPU: ${this.opciones[cpu].objeto}
`)
}
}
getRandom () {
return Math.floor(Math.random() * (3 - 0)) + 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment