Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Juego Javascipt Rock Paper Scissors, modo único o al mejor de tres

Soy nuevo en la codificación y javascript. Estoy trabajando en una tarea en la que tengo que crear un juego de piedra, papel o tijera que tenga 2 modos, un jugador y el mejor de tres. En el modo mejor de tres, debe crear un sistema para recordar la puntuación del usuario y el bot. Y necesita un bucle para ejecutar tantas rondas como sea posible hasta que haya un jugador que gane al menos dos rondas. Una vez que finaliza el juego, puedes preguntarle al jugador humano si quiere volver a jugar usando una función de confirmación(). Tengo el juego base, pero no sé cómo hacer que el juego se repita hasta que un jugador gane 2 rondas. Tampoco puedo averiguar cómo agregar una opción de reproducción. Si alguien puede ayudarme, por favor, se lo agradecería mucho.

 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(); let playerScore = 0; let computerScore = 0; 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() }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede pasar un parámetro a la función round() para indicarle cuántas ganancias se necesitan. Luego, coloca el código principal en un bucle:

 const round = (winsNeeded) => { let playerScore = 0; let computerScore = 0; while (playerScore < winsNeeded && computerScore < winsNeeded) { const res = play(); console.log(res) if (res[0] == 'P') playerScore++; else if (res[0] == 'C') computerScore++; console.log(`Score: you ${playerScore}, computer ${computerScore}`); } if (playerScore == winsNeeded) { console.log("Player won that round."); } else { console.log("Computer won that round."); } } while (true) { let cnt = 1; const mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode"); if (mode === '2') cnt = 2; round(cnt); const choice = prompt("Play again?"); if (choice[0].toLowerCase() != 'y') break; }
about 4 years ago · Juan Pablo Isaza Report

0

Por cada 2 rondas puedes comprobar el estado actual del juego. También se hicieron algunos cambios en la lógica del juego.

 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!"; } //little change in logic here---- if (computerChoice === "rock") { if (userChoice === "scissors") { return "Computer wins"; } else { if (userChoice === "paper") return "Player wins"; } } if (computerChoice === "paper") { if (userChoice === "rock") { return "Computer wins"; } else { if (userChoice === "scissors") return "Player wins"; } } if (computerChoice === "scissors") { if (userChoice === "paper") { return "Computer wins"; } else { if (userChoice === "rock") return "Player wins"; } } }; const round = () => { const res = play(); let playerScore = 0; let computerScore = 0; console.log(res) cnt-- wins[cnt] = res.startsWith("Player") ? 1 :res.includes("tie") ?-1:0; console.log(wins) if (cnt === 0) { const total = wins.filter((a) => a==1) console.log(`You beat the computer ${total.length} time${total.length===1?"":"s"}`) playAgain() }else if(cnt == 1){ // Check for every round var el2 = wins[2]; var el1 = wins[1]; if(el2 == el1 && el1!=-1){ const total = wins.filter((a) => a==1) console.log(`You beat the computer ${total.length} time${total.length===1?"":"s"}`) playAgain() } } if(pa){ setTimeout(round, 10) // else go again }else{ prompt("Thank you for playing") } } let pa = true; 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() function playAgain(){ if(confirm('Do you want to play again ?')){ pa = true 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() }else{ pa=false } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!