Answers.Body: <p>Let's break it down:</p>
  
  <ul>
  <li>You have a working Java chess board that you can use to move pieces</li>
  <li>You want a chess engine (<strong>engine</strong> is a more precise term than <strong>AI</strong>)</li>
  <li>You want to download Stockfish and use it</li>
  <li>You want to invent a new protocol or technology to communicate your chess board with the Stockfish engine</li>
  </ul>
  
  <p>To me, your question is really:</p>
  
  <ul>
  <li>How to connect an external chess engine such as Stockfish within my own chess GUI in Java?</li>
  </ul>
  
  <p>Useful old posts in this site:</p>
  
  <ul>
  <li><a href="https://chess.stackexchange.com/questions/9632/chess-engine-api/9633#9633">Chess Engine API</a></li>
  <li><a href="https://chess.stackexchange.com/questions/15704/what-is-the-optimal-way-to-use-stockfish-programmatically/15707#15707">What is the optimal way to use Stockfish programmatically?</a></li>
  </ul>
  
  <p>Basically, what you need to do is:</p>
  
  <ol>
  <li><p>You shouldn't invent your own technology, you should study the UCI protocol. <a href="http://wbec-ridderkerk.nl/html/UCIProtocol.html" rel="noreferrer">http://wbec-ridderkerk.nl/html/UCIProtocol.html</a> is a useful reference. I can get you started, do this:</p>
  
  <ol>
  <li>Download Stockfish</li>
  <li>Open Stockfish in a console/terminal, don't double-click on it with your mouse</li>
  <li>Type <code>ucinewgame</code> and then <code>go movetime 1000</code>. Do you see Stockfish's move?</li>
  <li>Read the protocol specification and learn how to analyze a position, and the output format.</li>
  </ol></li>
  <li><p>Write a UCI output parser. Parse whatever information you want.</p></li>
  <li>Use the information to make a move on your chess board.</li>
  </ol>
  
  <p>The only technical difficulty I can see here is to establish a "connection" between your GUI with the engine. This is done by creating a new process, running the engine.</p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/2006035/how-to-create-a-process-in-java">https://stackoverflow.com/questions/2006035/how-to-create-a-process-in-java</a> has information on how to do it.</p>
  </blockquote>
  
  <p>Once you have the new process, you can establish a simple standard input/output pipeline. <a href="https://stackoverflow.com/questions/27081918/java-how-to-send-a-value-to-child-process-using-outputstream">https://stackoverflow.com/questions/27081918/java-how-to-send-a-value-to-child-process-using-outputstream</a> has the details. Ask on Stackoverflow if you're not sure.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-10T04:21:09.463Z
Answers.Text: I will start working on it immediately and would definitely come back if I hit any roadblocks.
Answers.UserDisplayName: Shadow_Sphynx
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-10T04:49:27.913Z
Answers.Text: @Shadow_Sphynx Please start a new question if you have anything new to ask. Consider to accept this answer.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-03T09:56:26.543Z
Answers.Text: wow what a great answer!
Answers.UserDisplayName: Nathan Adams
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-10T03:01:12.780Z
Answers.LastActivityDate: 2017-02-10T03:07:06.007Z
Answers.LastEditDate: 2017-05-23T12:37:05.403Z
Answers.UserDisplayName: SmallChess
Body: <p>I wanted to make a 1 player chess game.</p>
  
  <p>The idea is:</p>
  
  <ul>
  <li>The move made by the player would be displayed on the GUI.</li>
  <li>It is sent to the Chess Engine in its own specific understood form.</li>
  <li>Then the result of the Chess engine would be taken and then made visible on the GUI.</li>
  </ul>
  
  <p>Currently I have made no progress on it and am on a dead end. My current program on Java lets 2 players play with each other. The program manages the rules that have to be followed and which moves are illegal. But for a single player game I wanted an AI for my game that can take inputs and give outputs. I searched online for various open source chess engines like Stockfish. But found nothing about using it in my program.</p>
  
  <p>I tried to work on to create my own engine but it was quite a challenge for me. Hence connecting it with an external AI is the only solution I think there is.</p>
  
Tags: <engines><software><stockfish>
Title: Connecting Chess Engine with a Java program
ThreadText: # Connecting Chess Engine with a Java program
  
  (Asked by: username_0 on 2017-02-10)
  
  I wanted to make a 1 player chess game.
  
  The idea is:
  
  * The move made by the player would be displayed on the GUI.
  * It is sent to the Chess Engine in its own specific understood form.
  * Then the result of the Chess engine would be taken and then made visible on the GUI.
  
  Currently I have made no progress on it and am on a dead end. My current program on Java lets 2 players play with each other. The program manages the rules that have to be followed and which moves are illegal. But for a single player game I wanted an AI for my game that can take inputs and give outputs. I searched online for various open source chess engines like Stockfish. But found nothing about using it in my program.
  
  I tried to work on to create my own engine but it was quite a challenge for me. Hence connecting it with an external AI is the only solution I think there is.
  
  **Tags:** engines, software, stockfish
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-02-10. Score: 9 | Accepted answer
  
  Let's break it down:
  
  * You have a working Java chess board that you can use to move pieces
  * You want a chess engine (**engine** is a more precise term than **AI**)
  * You want to download Stockfish and use it
  * You want to invent a new protocol or technology to communicate your chess board with the Stockfish engine
  
  To me, your question is really:
  
  * How to connect an external chess engine such as Stockfish within my own chess GUI in Java?
  
  Useful old posts in this site:
  
  * [Chess Engine API](https://chess.stackexchange.com/questions/9632/chess-engine-api/9633#9633)
  * [What is the optimal way to use Stockfish programmatically?](https://chess.stackexchange.com/questions/15704/what-is-the-optimal-way-to-use-stockfish-programmatically/15707#15707)
  
  Basically, what you need to do is:
  
  1. You shouldn't invent your own technology, you should study the UCI protocol. <http://wbec-ridderkerk.nl/html/UCIProtocol.html> is a useful reference. I can get you started, do this:
  
     1. Download Stockfish
     2. Open Stockfish in a console/terminal, don't double-click on it with your mouse
     3. Type `ucinewgame` and then `go movetime 1000`. Do you see Stockfish's move?
     4. Read the protocol specification and learn how to analyze a position, and the output format.
  2. Write a UCI output parser. Parse whatever information you want.
  3. Use the information to make a move on your chess board.
  
  The only technical difficulty I can see here is to establish a "connection" between your GUI with the engine. This is done by creating a new process, running the engine.
  
  > <https://stackoverflow.com/questions/2006035/how-to-create-a-process-in-java> has information on how to do it.
  
  Once you have the new process, you can establish a simple standard input/output pipeline. <https://stackoverflow.com/questions/27081918/java-how-to-send-a-value-to-child-process-using-outputstream> has the details. Ask on Stackoverflow if you're not sure.
  
  > **Comments:**
  > **username_0**: I will start working on it immediately and would definitely come back if I hit any roadblocks.  
  > **username_1**: @username_0 Please start a new question if you have anything new to ask. Consider to accept this answer.  
  > **username_2**: wow what a great answer!  
  
  ---
  
  