2 stdin,一次只有一次,其他很多

时间:2022-11-15 20:05:56

So I need to prompt the user first for some info one time before I start the game. The game is the 2nd stdin which then just keeps looping till the end of the game, asking for input, doing its thing, etc. until someone wins.

因此,在开始游戏之前,我需要首先提示用户一些信息。游戏是第二个stdin然后只是循环直到游戏结束,要求输入,做它的事情等等,直到有人获胜。

I can't get the one time vs continuous working:

我不能得到一次与连续工作:

let firstRun

让firstRun

process.openStdin().on('data', (name) => {

    if(firstRun){
      console.log("Please enter your first name");
      Computer.move();
      firstRun = false;
    }

    process.openStdin().on('data', (move) => {
      Human.move(move);
    });
});

the problem is I notice that the outside stdin runs, gets a res but then it's passing its res to the inner stdin which I don't want...

问题是我注意到外面的stdin运行,得到一个res然后它将它的res传递给我不想要的内部stdin ...

I'm sure I'm probably not going about this right but I don't know what else to try.

我敢肯定我可能不会这样做,但我不知道还有什么可以尝试。

Let me give you some new code I tried

让我给你一些我试过的新代码

const play = async () => {
  await promptForName();
  await runGame();
}

function promptForName(){
  return new Promise((resolve, reject) => {

    console.log("enter a your name:")

    process.openStdin().on('data', (name) => {
      console.log(`symbol entered: ${name}`)

      process.stdin.removeListener("data", promptForName);
      resolve()
    });
  })
}

function runGame(){
  return new Promise((resolve, reject) => {
    process.openStdin().on('data', (move) => {
      console.log(`move: ${move}`)

      Human.move(move);
      resolve()
    });
  })
}

When I ran this, the first runs ok it seems, second runs but I get no value for move and am not prompted for any input either

当我运行它时,第一次运行似乎正常,第二次运行但我没有得到移动的值,也没有提示任何输入

2 个解决方案

#1


1  

There is only 1 stdin. You should attach a listener to it and then remove it after the first move.

只有1个标准差。你应该附加一个监听器,然后在第一次移动后将其删除。

// Keep track of the computer listener
const computerListener = process.stdin.on("data", name => {
    console.log("Please enter your first name");
    Computer.move();

    // Remove it after the first time
    process.stdin.removeListener("data", computerListener);

    // Set up a new listener
    process.stdin.on("data", move => {
      Human.move(move);
    });
});

Alternatively, you could use .once().

或者,您可以使用.once()。

// Keep track of the computer listener
const computerListener = process.stdin.once("data", name => {
    console.log("Please enter your first name");
    Computer.move();

    // Set up a new listener
    process.stdin.on("data", move => {
      Human.move(move);
    });
});

#2


1  

You can use else

你可以用别的

process.openStdin().on('data', (name) => {

    if(firstRun){
      console.log("Please enter your first name");
      Computer.move();
      firstRun = false;
    }else{ 
      Human.move(move);
    }

});

#1


1  

There is only 1 stdin. You should attach a listener to it and then remove it after the first move.

只有1个标准差。你应该附加一个监听器,然后在第一次移动后将其删除。

// Keep track of the computer listener
const computerListener = process.stdin.on("data", name => {
    console.log("Please enter your first name");
    Computer.move();

    // Remove it after the first time
    process.stdin.removeListener("data", computerListener);

    // Set up a new listener
    process.stdin.on("data", move => {
      Human.move(move);
    });
});

Alternatively, you could use .once().

或者,您可以使用.once()。

// Keep track of the computer listener
const computerListener = process.stdin.once("data", name => {
    console.log("Please enter your first name");
    Computer.move();

    // Set up a new listener
    process.stdin.on("data", move => {
      Human.move(move);
    });
});

#2


1  

You can use else

你可以用别的

process.openStdin().on('data', (name) => {

    if(firstRun){
      console.log("Please enter your first name");
      Computer.move();
      firstRun = false;
    }else{ 
      Human.move(move);
    }

});