Answers.Body: <p>It was me who suggested you to come here because you'd need to understand what's happening. The guys on Stackoverflow wouldn't have the knolwedge to answer you.</p>
  
  <p>Firstly, this is definitely your fault since Stockfish wouldn't give you <code>a2a3</code> unless you have screwed up something. But why a2a3? That's because Stockfish always try to move a pawn before anything else. The way how the bitboard is encoded, it'd start its search from a1 to h1, then a2 to h2, a3 to h3 etc. The a2 white pawn is the first pawn it sees, so it tries to move it.</p>
  
  <p>Now we've established it's your fault, however, it's not possible for us to determine the cause because we don't have your source code. But I can give you some hints, you'll need to experiment yourself.</p>
  
  <ol>
  <li><p>You have killed the standard input too early. I don't quite understand why you would do <code>stockfish.stdin.end();</code>. Stockfish assumes the standard input/output works, always works.</p></li>
  <li><p>You have failed to setup the bitboard. I'm confident you'd get the error unless you let Stockfish to setup it's internal data structure. Have you done it? The code for doing it should be in the main() function, did you run it? You can't just run the UCI loop directly, it wouldn't work.</p></li>
  <li><p>How did you host the engine? Did you host it in the same address space as the server? Note that the engine assumes it's in a different address space because this is how a desktop GUI would work.</p></li>
  <li><p>Print out everything you send to Stockfish and everything you receive from the engine. <strong>Don't</strong> assume your commands would work, <strong>print</strong> everything to a text file. Run the commands in a command line. Stockfish could give you nonsense if one of your commands is not right. You'll have to verify it by trying yourself, don't assume anything.</p></li>
  <li><p>If you still can't figure out what's happening. <strong>Put</strong> a breakpoint in the UCI loop and follow it. There has to be something, a condition that makes your search terminate immediately. Don't be afraid, you should be able to debug it yourself if you follow the code step-by-step.</p></li>
  <li><p>Have you done <code>ucinewgame</code>?</p></li>
  </ol>
  
  <p>Most likely it's (2), a common mistake in chess programming. If you don't run the setup code correctly, the engine wouldn't be able to run the nega-max algorithm. No nega-max => nothing to search => simply return you the first pawn move.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-02T11:00:18.297Z
Answers.Text: Looks like my mistake was that I forgot to add a \n at the end of my `stockfish.stdin.write()`
  
  That and removing the `stockfish.stdin.end()` were the mistakes I did, and after correcting them the web service worked fine. I'll accept the answer once it specifically states that :)
Answers.UserDisplayName: Loupax
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-02T11:34:29.110Z
Answers.Text: @Loupax You can always edit the answer.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-27T23:29:55.613Z
Answers.LastActivityDate: 2016-04-28T00:01:34.167Z
Answers.LastEditDate: 2016-04-28T00:01:34.167Z
Answers.UserDisplayName: SmallChess
Body: <p><em>Copied this question from <a href="https://stackoverflow.com/questions/36826038/why-am-i-getting-different-behaviour-between-using-stockfish-as-a-child-node-pro?noredirect=1#comment61355417_36826038">stackoverflow</a>. Not 100% sure it belongs here but we'll see...</em></p>
  
  <p>I am building a web interface for the <a href="https://stockfishchess.org/" rel="nofollow noreferrer" title="stockfish">stockfish</a> chess engine using Node, and I noticed that the behaviour of the exact same commands behaves differently.</p>
  
  <p>Here are the UCI commands I use when calling stockfish from the command line:</p>
  
  <pre><code>position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
  go movetime 15000
  </code></pre>
  
  <p>The engine then responds as expected: Does analysis for 15 seconds while outputting every step on stdout, and ends with a suggestion in the following format:</p>
  
  <pre><code>bestmove e2e4 ponder e7e6
  </code></pre>
  
  <p>Here is the code that makes the same call as a node js child process:</p>
  
  <pre><code>app.get('/next_move/:fen', (req, res) =&gt; {
      const stockfish = spawn('bin/stockfish');
      stockfish.stdin.write(`position fen ${req.params.fen}\ngo movetime 15000`);
      console.info(`Sending UCI command to stockfish: position fen ${req.params.fen}\ngo movetime 15000`);
      stockfish.stdin.end();
  
      stockfish.stdout.on('error', (error)=&gt;{
          console.error(`${error}`);
          res.status(500).send('Internal server error');
          stockfish.kill();
      });
  
      stockfish.stdout.on('message', (data)=&gt;{
          console.log(`Message: ${data}`);
      });
  
      stockfish.stdout.on('data', (data) =&gt;{
          let output = data.toString();
          // Ignore the first line in stdout
          if (output.substring(0, 9) === 'Stockfish') {
              return;
          }
          console.info(`Stockfish output: ${output}`);
          if (output.substring(0, 8) === 'bestmove') {
              res.json({'bestMove': output.substring(8).trim()});
          } else {
              res.status(400).send('Bad FEN string');
          }
          stockfish.kill();
      });
  });
  </code></pre>
  
  <p>After I make this code run via web, get a result almost instantly, and also get a different output from stockfish:</p>
  
  <pre><code>bestmove a2a3
  </code></pre>
  
  <p>Why is this happening? Is this related to Nodejs or to Stockfish? </p>
  
Tags: <engines><stockfish><programming><uci>
Title: Why am I getting different behaviour between using stockfish as a child node process and a CLI called process?
ThreadText: # Why am I getting different behaviour between using stockfish as a child node process and a CLI called process?
  
  (Asked by: username_0 on 2016-04-27)
  
  *Copied this question from [stackoverflow](https://stackoverflow.com/questions/36826038/why-am-i-getting-different-behaviour-between-using-stockfish-as-a-child-node-pro?noredirect=1#comment61355417_36826038). Not 100% sure it belongs here but we'll see...*
  
  I am building a web interface for the [stockfish](https://stockfishchess.org/ "stockfish") chess engine using Node, and I noticed that the behaviour of the exact same commands behaves differently.
  
  Here are the UCI commands I use when calling stockfish from the command line:
  
  ```
  position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
  go movetime 15000
  
  ```
  
  The engine then responds as expected: Does analysis for 15 seconds while outputting every step on stdout, and ends with a suggestion in the following format:
  
  ```
  bestmove e2e4 ponder e7e6
  
  ```
  
  Here is the code that makes the same call as a node js child process:
  
  ```
  app.get('/next_move/:fen', (req, res) => {
      const stockfish = spawn('bin/stockfish');
      stockfish.stdin.write(`position fen ${req.params.fen}\ngo movetime 15000`);
      console.info(`Sending UCI command to stockfish: position fen ${req.params.fen}\ngo movetime 15000`);
      stockfish.stdin.end();
  
      stockfish.stdout.on('error', (error)=>{
          console.error(`${error}`);
          res.status(500).send('Internal server error');
          stockfish.kill();
      });
  
      stockfish.stdout.on('message', (data)=>{
          console.log(`Message: ${data}`);
      });
  
      stockfish.stdout.on('data', (data) =>{
          let output = data.toString();
          // Ignore the first line in stdout
          if (output.substring(0, 9) === 'Stockfish') {
              return;
          }
          console.info(`Stockfish output: ${output}`);
          if (output.substring(0, 8) === 'bestmove') {
              res.json({'bestMove': output.substring(8).trim()});
          } else {
              res.status(400).send('Bad FEN string');
          }
          stockfish.kill();
      });
  });
  
  ```
  
  After I make this code run via web, get a result almost instantly, and also get a different output from stockfish:
  
  ```
  bestmove a2a3
  
  ```
  
  Why is this happening? Is this related to Nodejs or to Stockfish?
  
  **Tags:** engines, stockfish, programming, uci
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-04-27. Score: 3
  
  It was me who suggested you to come here because you'd need to understand what's happening. The guys on Stackoverflow wouldn't have the knolwedge to answer you.
  
  Firstly, this is definitely your fault since Stockfish wouldn't give you `a2a3` unless you have screwed up something. But why a2a3? That's because Stockfish always try to move a pawn before anything else. The way how the bitboard is encoded, it'd start its search from a1 to h1, then a2 to h2, a3 to h3 etc. The a2 white pawn is the first pawn it sees, so it tries to move it.
  
  Now we've established it's your fault, however, it's not possible for us to determine the cause because we don't have your source code. But I can give you some hints, you'll need to experiment yourself.
  
  1. You have killed the standard input too early. I don't quite understand why you would do `stockfish.stdin.end();`. Stockfish assumes the standard input/output works, always works.
  2. You have failed to setup the bitboard. I'm confident you'd get the error unless you let Stockfish to setup it's internal data structure. Have you done it? The code for doing it should be in the main() function, did you run it? You can't just run the UCI loop directly, it wouldn't work.
  3. How did you host the engine? Did you host it in the same address space as the server? Note that the engine assumes it's in a different address space because this is how a desktop GUI would work.
  4. Print out everything you send to Stockfish and everything you receive from the engine. **Don't** assume your commands would work, **print** everything to a text file. Run the commands in a command line. Stockfish could give you nonsense if one of your commands is not right. You'll have to verify it by trying yourself, don't assume anything.
  5. If you still can't figure out what's happening. **Put** a breakpoint in the UCI loop and follow it. There has to be something, a condition that makes your search terminate immediately. Don't be afraid, you should be able to debug it yourself if you follow the code step-by-step.
  6. Have you done `ucinewgame`?
  
  Most likely it's (2), a common mistake in chess programming. If you don't run the setup code correctly, the engine wouldn't be able to run the nega-max algorithm. No nega-max => nothing to search => simply return you the first pawn move.
  
  > **Comments:**
  > **username_0**: Looks like my mistake was that I forgot to add a \n at the end of my `stockfish.stdin.write()`  That and removing the `stockfish.stdin.end()` were the mistakes I did, and after correcting them the web service worked fine. I'll accept the answer once it specifically states that :)  
  > **username_1**: @username_0 You can always edit the answer.  
  
  ---
  
  