Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #6 - JavaScript #5911

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// * Crea un programa que calcule quien gana más partidas al piedra,
// * papel, tijera, lagarto, spock.
// * - El resultado puede ser: "Player 1", "Player 2", "Tie" (empate)
// * - La función recibe un listado que contiene pares, representando cada jugada.
// * - El par puede contener combinaciones de "🗿" (piedra), "📄" (papel),
// * "✂️" (tijera), "🦎" (lagarto) o "🖖" (spock).
// * - Ejemplo. Entrada: [("🗿","✂️"), ("✂️","🗿"), ("📄","✂️")]. Resultado: "Player 2".
// * - Debes buscar información sobre cómo se juega con estas 5 posibilidades.

const rules = {
"🦎": ["🖖", "📄"],
"🗿": ["🦎", "✂️"],
"✂️": ["📄", "🦎"],
"🖖": ["✂️", "🗿"],
"📄": ["🗿", "🖖"]
}

const rockPaperScissorsLizardSpock = (games) => {

let player1 = 0
let player2 = 0

for (const game of games) {
let player1_game = game[0]
let player2_game = game[1]

if (player1_game !== player2_game) {
if (rules[player1_game].includes(player2_game)) {
player1++
} else if (rules[player2_game].includes(player1_game)) {
player2++
}
}
}
return (player1 === player2) ? 'Tie' : (player1 > player2) ? 'Player 1 Win' : 'Player 2 Win'
}

console.log(rockPaperScissorsLizardSpock([["🦎", "📄"], ["📄", "🖖"], ["📄", "✂️"]]))
console.log(rockPaperScissorsLizardSpock([["✂️", "🖖"], ["🖖", "✂️"], ["✂️", "🦎"]]))
console.log(rockPaperScissorsLizardSpock([["📄", "✂️"], ["📄", "📄"], ["🖖", "✂️"]]))
console.log(rockPaperScissorsLizardSpock([["🖖", "🦎"], ["🗿", "✂️"], ["🦎", "✂️"]]))