Estoy construyendo un juego de piedra, papel o tijera con Javascript, hice un modo de 1 ronda del juego y funcionó, ahora quiero agregar un segundo modo "al mejor de tres rondas", pero después de probar muchas cosas, mi código se desordenó y no puede averiguar qué hacer exactamente.
trato de agregar un count = 0; count < 3; count = count + 1 pero no sé exactamente dónde ponerlo
¿Puede alguien ayudarme por favor?
var mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode"); if (mode === '1') { oneRound; } if (mode === '2') { bestOfThree; } // set Computer Choice var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if (computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } // Compare value for oneRound mode var oneRound = function(computerChoice, userChoice) { if (computerChoice === userChoice) { return "The result is tie!"; } if (computerChoice === "rock") { if (userChoice === "scissors") { return "Computer wins"; } else { if (userChoice === "paper") return "Player wins"; } } if (computerChoice === "paper") { if (userChoice === "scissors") { return "Computer wins"; } else { if (userChoice === "rock") return "Player wins"; } } if (computerChoice === "scissors") { if (userChoice === "rock") { return "Computer wins"; } else { if (userChoice === "scissors") return "Player wins"; } } }; console.log("Player Choice: " + userChoice); console.log("Computer Choice: " + computerChoice); console.log(oneRound(computerChoice, userChoice));mira esto
Uso setTimeout para permitir que la pantalla muestre el resultado.
const play = () => { // set Computer Choice var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if (computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } console.log("Player Choice: " + userChoice); console.log("Computer Choice: " + computerChoice); if (computerChoice === userChoice) { return "The result is tie!"; } if (computerChoice === "rock") { if (userChoice === "scissors") { return "Computer wins"; } else { if (userChoice === "paper") return "Player wins"; } } if (computerChoice === "paper") { if (userChoice === "scissors") { return "Computer wins"; } else { if (userChoice === "rock") return "Player wins"; } } if (computerChoice === "scissors") { if (userChoice === "rock") { return "Computer wins"; } else { if (userChoice === "scissors") return "Player wins"; } } }; const round = () => { const res = play(); console.log(res) cnt-- wins[cnt] = res.startsWith("Player") ? 1 : 0; if (cnt === 0) { const total = wins.reduce((a, b) => a + b) console.log(`You beat the computer ${total} time${total===1?"":"s"}`) return } setTimeout(round, 10) // else go again } let cnt = 1, wins = []; const mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode"); if (mode === '2') { cnt = 3; } round()