Answers.Body: <p>At bare minimum, a working chess engine (with the classical structure) needs three things:</p>
  <ol>
  <li>A way to represent board states and make moves</li>
  <li>A way to evaluate positions without searching</li>
  <li>A way to search the game tree.</li>
  </ol>
  <hr />
  <p>If you are willing to ignore performance considerations and difficult rules (3 repetitions, 50 move rule, capture king to win, etc.) the first item is a relatively straightforward (if tedious) exercise in programming. There also exist <a href="https://python-chess.readthedocs.io/en/latest/" rel="noreferrer">chess libraries</a> which can do this for you.</p>
  <p>The simplest example of an evaluation function is just the material count. The base material count can then be modified---for instance, you might give a slight bonus for having knights closer to the center of the board, and a penalty in the corner. This material + modification evaluation function is workable.</p>
  <p>The simplest way to search the game tree is of course <a href="https://en.wikipedia.org/wiki/Minimax" rel="noreferrer">minimax</a>. Even with a decent evaluation function, this computer will still play quite poorly, and I would not personally consider it &quot;viable&quot;. To me, the absolute bare minimum is some implementation of <a href="https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning" rel="noreferrer">alpha-beta pruning</a>.</p>
  <hr />
  <p>Depending on the language and how bad the implementation is, the above-described engine might be 1000-1700 (ish) chess.com strength. It'll rarely miss tactics, but capitulate in any situation requiring &gt;4 move planning.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-25T00:51:20.983Z
Answers.LastActivityDate: 2024-12-25T00:51:20.983Z
Answers.UserDisplayName: Noah Caplinger
Answers.Body: <p>The first time I was able to understand the basics of a chess engine was by studying <a href="https://github.com/nguyenpham/FirstChess/blob/master/firstchess.c" rel="noreferrer">Firstchess</a> code (in C), by Pham Nguyen:</p>
  <p>This engine lacks castle and en passant, but other than that it has all the important stuff to know how a chess engine works.</p>
  <p>If you need an engine that plays proper chess (except the threefold repetition) you can try <a href="https://github.com/emdio/secondchess/blob/master/secondchess.c" rel="noreferrer">Secondchess</a>, a derivative from Firstchess, essentially adding the en passant and castle rules, by your humble servant.</p>
  <p>A third and more advanced option is <a href="http://www.tckerrigan.com/Chess/TSCP/" rel="noreferrer">TSCP</a>: Tom Kerrigan’s Simple Chess Program. This program is way more advanced than First/Secondchess; the code is better structured, eval and search are more advanced, etc.
  So you can take a look at all of them and use whatever better suits you.</p>
  <p>PS: the original code of Firstchess had a bug in its search; somewhere a minus sign had to be added. Also, another bug was somewhere related to promotion. I don't know whether this bugs are solved in the Firstchess code above, but they're for sure solved on Secondchess.</p>
  <p>PS': when studying Firstchess, take into account that a lot of the code is related to move generating: you don't need to understand exactly how this code works (though it'd be desirable), and just treat this part as a piece of code that just generates legal moves and you can just focus on the rest of the program.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-27T01:47:50.483Z
Answers.Text: According to chessprogramming wiki, encoding the rules including castling and en passant is the trickiest part of programming an engine. So any engine that does not encode all the rules of the game perfectly cannot be considered really working (but almost there).
Answers.UserDisplayName: qwr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-25T09:28:45.463Z
Answers.LastActivityDate: 2024-12-25T18:16:44.097Z
Answers.LastEditDate: 2024-12-25T18:16:44.097Z
Answers.UserDisplayName: emdio
Body: <p>Is there something like a &quot;Hello World&quot; or &quot;Minimum Viable Product&quot; for a chess engine programming? Something like a required minimum in order to create a decent chess program? Some kind of &quot;required minimum&quot; which would help me understand the essence of chess engine without diving into non-important details. Something like a XOR problem for neural networks?</p>
  <p>The closest I have found is the Bluefever Software tutorial <a href="https://www.youtube.com/watch?v=2eA0bD3wV3Q&amp;list=PLZ1QII7yudbe4gz2gh9BCI6VDA-xafLog" rel="noreferrer">https://www.youtube.com/watch?v=2eA0bD3wV3Q&amp;list=PLZ1QII7yudbe4gz2gh9BCI6VDA-xafLog</a> . Is this really an MVP or it could be something different?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-12-27T04:08:23.987Z
Comments.Text: This hinges on your definition of what constitutes a chess engine. A hello world program does not contain every language construct a developer will need (most notably: no input handling, no conditions, no iterations). The analogy of a chess engine would then be something that is minimally identifiable as chess but without requiring it to implement all rules of chess. That's a very subjective discussion on what the first criterion of a chess engine would be.
Comments.UserDisplayName: Flater
Tags: <engines><programming>
Title: Is there an MVP or "Hello world" for chess programming?
ThreadText: # Is there an MVP or "Hello world" for chess programming?
  
  (Asked by: username_0 on 2024-12-24)
  
  Is there something like a "Hello World" or "Minimum Viable Product" for a chess engine programming? Something like a required minimum in order to create a decent chess program? Some kind of "required minimum" which would help me understand the essence of chess engine without diving into non-important details. Something like a XOR problem for neural networks?
  
  The closest I have found is the Bluefever Software tutorial <https://www.youtube.com/watch?v=2eA0bD3wV3Q&list=PLZ1QII7yudbe4gz2gh9BCI6VDA-xafLog> . Is this really an MVP or it could be something different?
  
  **Tags:** engines, programming
  
  > **Comments:**
  > **username_1**: This hinges on your definition of what constitutes a chess engine. A hello world program does not contain every language construct a developer will need (most notably: no input handling, no conditions, no iterations). The analogy of a chess engine would then be something that is minimally identifiable as chess but without requiring it to implement all rules of chess. That's a very subjective discussion on what the first criterion of a chess engine would be.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2024-12-25. Score: 8
  
  At bare minimum, a working chess engine (with the classical structure) needs three things:
  
  1. A way to represent board states and make moves
  2. A way to evaluate positions without searching
  3. A way to search the game tree.
  
  ---
  
  If you are willing to ignore performance considerations and difficult rules (3 repetitions, 50 move rule, capture king to win, etc.) the first item is a relatively straightforward (if tedious) exercise in programming. There also exist [chess libraries](https://python-chess.readthedocs.io/en/latest/) which can do this for you.
  
  The simplest example of an evaluation function is just the material count. The base material count can then be modified---for instance, you might give a slight bonus for having knights closer to the center of the board, and a penalty in the corner. This material + modification evaluation function is workable.
  
  The simplest way to search the game tree is of course [minimax](https://en.wikipedia.org/wiki/Minimax). Even with a decent evaluation function, this computer will still play quite poorly, and I would not personally consider it "viable". To me, the absolute bare minimum is some implementation of [alpha-beta pruning](https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning).
  
  ---
  
  Depending on the language and how bad the implementation is, the above-described engine might be 1000-1700 (ish) chess.com strength. It'll rarely miss tactics, but capitulate in any situation requiring >4 move planning.
  
  
  ---
  
  ### Answer by username_3 on 2024-12-25. Score: 8
  
  The first time I was able to understand the basics of a chess engine was by studying [Firstchess](https://github.com/nguyenpham/FirstChess/blob/master/firstchess.c) code (in C), by Pham Nguyen:
  
  This engine lacks castle and en passant, but other than that it has all the important stuff to know how a chess engine works.
  
  If you need an engine that plays proper chess (except the threefold repetition) you can try [Secondchess](https://github.com/username_3/secondchess/blob/master/secondchess.c), a derivative from Firstchess, essentially adding the en passant and castle rules, by your humble servant.
  
  A third and more advanced option is [TSCP](http://www.tckerrigan.com/Chess/TSCP/): Tom Kerrigan’s Simple Chess Program. This program is way more advanced than First/Secondchess; the code is better structured, eval and search are more advanced, etc.
  So you can take a look at all of them and use whatever better suits you.
  
  PS: the original code of Firstchess had a bug in its search; somewhere a minus sign had to be added. Also, another bug was somewhere related to promotion. I don't know whether this bugs are solved in the Firstchess code above, but they're for sure solved on Secondchess.
  
  PS': when studying Firstchess, take into account that a lot of the code is related to move generating: you don't need to understand exactly how this code works (though it'd be desirable), and just treat this part as a piece of code that just generates legal moves and you can just focus on the rest of the program.
  
  > **Comments:**
  > **username_4**: According to chessprogramming wiki, encoding the rules including castling and en passant is the trickiest part of programming an engine. So any engine that does not encode all the rules of the game perfectly cannot be considered really working (but almost there).  
  
  ---
  
  