简体   繁体   English

如何让我的 function 在石头、胡椒、剪刀游戏中等待 Javascript 中的按钮按下?

[英]How I make my function in a rock, pepper, scissors game wait for a button press in Javascript?

So I have this code(I added the whole JS code but the part I want help is in the function game) that simply runs 5 rounds of a game of rock, papper, scissors with a press of a button in my html file.所以我有这个代码(我添加了整个 JS 代码,但我需要帮助的部分在 function 游戏中)只需在我的 html 文件中按一个按钮即可运行 5 轮摇滚、纸、剪刀游戏。

The thing is that each round I want the function to wait for a button click of any of the three buttons (rock, papper or scissors)问题是每一轮我都希望 function 等待三个按钮(石头、纸或剪刀)中的任何一个按钮点击

I will appriciate any help, thank you and have a nice day!我会给予任何帮助,谢谢你,祝你有美好的一天!

let rock = "Rock";
let papper = "Papper";
let scissors = "Scissors";
let wins = 0;
let playerSelection;

const btnrock = document.getElementById('btn1');
const btnpapper = document.getElementById('btn2');
const btnscissors = document.getElementById('btn3');
const btnplay = document.getElementById('game')

function getPlayerChoice() {
  
  btnrock.addEventListener('click', () => {
  
    playerSelection = rock;
    return playerSelection;

  });
  
  btnpapper.addEventListener('click', () => {
  
    playerSelection = papper;
    return playerSelection;
  
  });
  
  btnscissors.addEventListener('click', () => {
  
    playerSelection = scissors;
    return playerSelection;

  });

}


btnplay.addEventListener('click', game)



function getComputerChoice() {

  let min = Math.ceil(0);
  let max = Math.floor(2);
  let random = Math.floor(Math.random() * (max - min + 1) + min); 

  if (random == 0)
  {
    return rock;
  }
  if (random == 1){
    return papper;
  }
  if (random == 2){
    return scissors;
  }
}




function playRound(playerSelection,pcChoice) {


  if(pcChoice == rock && playerSelection == papper) {
    wins++;
    return "Player Wins!";
  }

  else if (pcChoice == rock && playerSelection == scissors) {
    return "Player Loses!"
  }

  else if (pcChoice == scissors && playerSelection == rock){ wins++; return "Player Wins!"}

  else if (pcChoice == scissors && playerSelection == papper){return "Player Loses!"}

  else if (pcChoice == papper && playerSelection == rock){return "Player Loses!"}

  else if (pcChoice == papper && playerSelection == scissors){wins++; return "Player Wins!"}

  else {return "It's a Draw!"}
}


function game() {

  for(let i = 0; i < 5; i++){
    
    const pcChoice = getComputerChoice();

    playerSelection = getPlayerChoice(); // I want this to wait
    console.log("PC "+ pcChoice );
    console.log('Player '+playerSelection);
    let roundwinner = playRound(playerSelection,pcChoice);
    console.log(roundwinner);

    if(i === 2 && wins == 0){
      break;
    }

    if(i === 3 && wins <= 1) {
      break;
    }

    if(wins==3){
      break;
    }

  }

  if(wins >= 3) {
    console.log("You are the winner of this match")
  }
  else {console.log('You lost the game');}
}

getPlayerChoice doesn't really get the player's choice - it adds event listeners. getPlayerChoice 并没有真正获得玩家的选择——它添加了事件监听器。 Run it 5 times and you get 5 listeners hooked up to the same event.运行 5 次,你会得到 5 个监听器连接到同一个事件。 I don't think you can wait for an event to fire in a loop like you're currently trying to.我认为您不能像当前尝试的那样等待事件在循环中触发。 This would be a perfectly fine design in a console app when you want some input from the user, but in JS when working with events you have to leverage the events to execute your game's logic.当您需要用户的一些输入时,这在控制台应用程序中将是一个非常好的设计,但在 JS 中处理事件时,您必须利用事件来执行游戏的逻辑。

In other words, you might want to get rid of the for loop and consider moving that logic to event chain with a listener that first adds the user's input to the board and then executes the follow logic - eg gets the computer choice or determines whether the game is finished or not.换句话说,您可能想要摆脱 for 循环并考虑将该逻辑移动到事件链,并使用一个侦听器,该侦听器首先将用户的输入添加到板,然后执行后续逻辑 - 例如获取计算机选择或确定是否游戏结束与否。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM