0

so i'm creating a game like Connect 4 which ask an input from a user but the problem i'm facing is that i used callback(readline.question) function inside a while loop whenever i start the code it start infinite loop without asking a question from a user. how i can pause it for a while until user answer? I've to solve this without using async/await.

 function fetchColumn(player, callback) {
   io.question(`Player ${player}, which Column? `, line => {
        console.log(`You requested "${line}"`);
        chosen_column = line;
        callback(); 
    });
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
while (game_over == 0) {
    connect4.printBoard();
    fetchColumn(current_player,()=>{
    
        console.log(`you entered ${chosen_column}`);
        
        if (chosen_column != 'Q' && chosen_column != 'q') {
            move_status = connect4.place_piece(chosen_column, current_player);
            x_in_a_row_status = connect4.x_in_a_row(current_player);
            
            if (move_status == 0) {
// Further code-------

This is what i'm getting in terminal.

Player 1, which Column? 
A B C D E F G 
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . 
Player 1, which Column? 
A B C D E F G
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

----------Keep repeating----------
2
  • What is io.question? Commented Nov 26, 2022 at 11:37
  • its readline.question used to take input from user. Commented Nov 26, 2022 at 12:09

1 Answer 1

1

If you want to call an asynchronous function in a loop, you can either use while in combination with await:

function fetchColumnAsync(player) {
  return new Promise(function(resolve, reject) {
    io.question(`Player ${player}, which Column? `, line => {
      console.log(`You requested "${line}"`);
      chosen_column = line;
      resolve(); 
    });
  });
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
while (game_over == 0) {
  connect4.printBoard();
  await fetchColumnAsync(current_player);
  console.log(`you entered ${chosen_column}`);
  ...
}

or recursion in combination with a callback function:

function loop() {
  if (game_over == 0) {
    connect4.printBoard();
    fetchColumn(current_player, () => {
      console.log(`you entered ${chosen_column}`);
      ...
      loop();
    });
  }
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
loop();

But you cannot combine while with a callback, because the second iteration of the while loop starts synchronously, before the callback function can be invoked asynchronously. In other words, it gives an infinite loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.