Answers.Body: <blockquote>
  <p>I'm building a chess engine but I often find myself overwhelmed</p>
  </blockquote>
  <p>I can identify quite a lot with how you're currently feeling. So before I address your questions, I'd like to give some encouragement if I may.</p>
  <p>I started working on writing a chess engine back in February of this year, so about 10 months ago. I still have the code from that first attempt I ever made at writing an engine. It was a mess, and I had no clue what I was doing, even though at that point I had been programming for a couple of years.</p>
  <p>But one thing I will always give my past self credit for is that I learned a lot from that first attempt, and the second attempt was better...and the third, and the fourth, and the fifth, and so on. If I remember correctly I actually started over about <em>10 times</em> before I finally was able to create a solid codebase for the engine that I could start building from.</p>
  <p>But every time I started over, my goal got a little closer, and I made sure I learned from my mistakes. So even if you're at the point where your code has just become too buggy or too messy to continue working from, that's alright. Learn from your mistakes this go-round, take some time to lick your wounds, create a new blank slate, and start again. If you keep persevering like this, I believe eventually you'll have something you can be very proud of.</p>
  <p>With that out of the way, let me address the questions in your post:</p>
  <blockquote>
  <p>What are the best approaches to designing a chess engine? What
  classes/structures should it have? How are they connected to each
  other?</p>
  </blockquote>
  <p>Here's how to think about things conceptually. A chess engine has three basic parts:</p>
  <ul>
  <li>Move generation: this phase generates all of the moves for a given position</li>
  <li>Search: this phase uses the move generator to generate all of the possible moves for the root position, and for each of those nodes generate all of their moves, and so on recursively until a certain depth is reached, or a checkmate or draw occurs. If the position isn't a checkmate or draw, then the engine needs some way of figuring out if the position it stopped at is good or bad for it, so this is where the evaluation phase comes in.</li>
  <li>Evaluation: This is the phase that looks at a given chess position and tries to assign an integer value for how good the position is for black or white.</li>
  </ul>
  <p>Hopefully, you can start to see the connection between these three phases. The search is the heartbeat of any engine, and it makes calls to the move generation phase and the evaluation phase to do its job correctly.</p>
  <p>Now, that's the 30,000 ft overview. As you've discovered, however, the implementation specifics are the hard part. Here is how I recommend you start.</p>
  <ul>
  <li>Move generator: You said you've already been working on this so that's good. I would recommend using a mailbox design like <a href="https://www.chessprogramming.org/10x12_Board" rel="nofollow noreferrer">here</a>. You'll need some different functions to generate moves for the different pieces as well. If this still seems daunting, there's are a lot of places you can find some pseudo-code. The CPW has some, but also search up &quot;mailbox chess engines github&quot; in google and look through some of the code you find to get some ideas. Once you have <a href="https://www.chessprogramming.org/Pseudo-Legal_Move" rel="nofollow noreferrer">legal or pseduo-legal</a> move generator, test it <em>throughly</em> using <a href="https://www.chessprogramming.org/Perft" rel="nofollow noreferrer">PERFT</a>, on <a href="https://github.com/algerbrex/blunder/blob/main/testdata/perftsuite.epd" rel="nofollow noreferrer">multiple positions</a>. If that last link's format is confusing, read a little about <a href="https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation" rel="nofollow noreferrer">FEN strings</a> too.</li>
  <li>Search: You don't have to start off fancy here. You can just have a single function, copying the code here: <a href="https://www.chessprogramming.org/Negamax" rel="nofollow noreferrer">https://www.chessprogramming.org/Negamax</a>. Try to read a little to understand it. Then add some code in to clean things up to <em>your</em> liking. If you need to see some actually, feel free to look at an earlier version of my engine's search code <a href="https://github.com/algerbrex/blunder/blob/cd8e182d1b566af4aa8d0584ec91fbbcbf75e783/ai/search.go#L58" rel="nofollow noreferrer">here</a>.</li>
  <li>Evaluation: again, starting off, simpler is better. Just count material. Let pawns be worth say 100 points, bishops and knights worth 300, rooks worth 500, and queens worth 900. And then count how many of each piece each side has, and multiply that by the value of the piece. As I'm sure you can tell, this is quite crude, as chess is much more than just material. But it's an excellent place to start!</li>
  </ul>
  <p>Once you have these three parts written, implement a little bit of the <a href="http://wbec-ridderkerk.nl/html/UCIProtocol.html" rel="nofollow noreferrer">UCI protocol</a> and a <a href="http://mediocrechess.blogspot.com/2007/01/guide-time-management.html" rel="nofollow noreferrer">time-management scheme</a> so you can use your engine in a nice GUI, like Cute Chess or Arena. Again, if you need to see my code, there are many open-source engines on GitHub and other places. And always feel free to look at any code from my engine as well.</p>
  <p>At this point, you should have a working chess engine. Will it beat a grandmaster? Probably not. It probably won't even be beating you very well. But the more important point is that you'll have somewhere you can <em>build from</em>.</p>
  <p>Once you have the above implemented, start slow, and start reading about some ways to improve things. The next step could be to improve your move ordering and add a quiescence search phase. And after that, you can look into using piece-square tables, to start teaching your engine what squares are good for each piece type. And eventually, you can move on to techniques used in modern beasts like Stockfish or Komodo like pruning, and reductions, and extensions.</p>
  <p>But again, <em>take things <strong>slow</strong></em>, and start off simple. You can always come back later and improve things. But if you start off trying to do every little thing, you'll likely end up with a very messy and buggy program that will be no fun to continue working on.</p>
  <p>Hopefully, this helped to get you started down the right path. If you have any questions, feel free to fire away. I'd love to help out, and will try to answer when I have free time.</p>
  <p>P.S: Make an account at talkchess.com. There are many smart people, many much smarter than myself, who can also give wonderful advice about how to begin the rewarding yet sometimes challenging process of writing a novel chess engine. I can't wait to see your engine rated on the <a href="http://ccrl.chessdom.com/ccrl/404/" rel="nofollow noreferrer">CCRL</a> soon! :-)</p>
  <p><strong>Edit:</strong> First, I realize I didn't make it clear, but if you wanted to look through the code, my engines code is posted here: <a href="https://github.com/algerbrex/blunder" rel="nofollow noreferrer">https://github.com/algerbrex/blunder</a> (hopefully this isn't marked as self-promotion.)</p>
  <p>Second, I forgot to recommend some YouTube series that I highly recommend you watch if you need more help with a certain part of your engine:</p>
  <ul>
  <li><a href="https://www.youtube.com/watch?v=bGAfaepBco4&amp;list=PLZ1QII7yudbc-Ky058TEaOstZHVbT-2hg" rel="nofollow noreferrer">Programming a chess engine in C</a></li>
  <li><a href="https://www.youtube.com/watch?v=2eA0bD3wV3Q&amp;list=PLZ1QII7yudbe4gz2gh9BCI6VDA-xafLog" rel="nofollow noreferrer">Programming a chess engine in Javascript</a></li>
  <li><a href="https://www.youtube.com/watch?v=l3xzDDbQq3c&amp;list=PLmN0neTso3JyHJ4YqTo4IT8LPm4laNhZ5&amp;index=3" rel="nofollow noreferrer">Toyfish series</a> (A play on the name of the current strongest engine in the world, <a href="https://github.com/official-stockfish/Stockfish" rel="nofollow noreferrer">Stockfish</a>)</li>
  <li><a href="https://www.youtube.com/watch?v=QUNP-UjujBM&amp;list=PLmN0neTso3Jxh8ZIylk74JpwfiWNI76Cs" rel="nofollow noreferrer">Bitboard engine in C</a></li>
  </ul>
  <p>Both creators are great programmers and even better teachers. Hopefully, those help as well!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-15T05:06:47.407Z
Answers.LastActivityDate: 2021-11-15T22:47:09.733Z
Answers.LastEditDate: 2021-11-15T22:47:09.733Z
Answers.UserDisplayName: Chris
Body: <p>What are the best approaches to designing a chess engine? What classes/structures should it have? How are they connected to each other? I'm building a chess engine but I often find myself overwhelmed. I've learned from numerous resources (such as chess programming wiki) the algorithms for move generation, and have implemented them myself, but the design and structure of the whole thing is just generally a mess. If you've ever made a chess engine, how did you approach this?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-11-13T19:51:19.650Z
Comments.Text: My first approach was to understand [firstchess](https://github.com/nguyenpham/FirstChess/blob/master/firstchess.c). It's written in C, and has the very basics of a chess engine. It lacks castle and en passant, but for the goal of understanding how a chess engine works it's not important (and it simplifies the code a lot).
Comments.UserDisplayName: emdio
Tags: <engines><computer-chess><bitboard><chess-ai>
Title: Bitboard chess engine design
ThreadText: # Bitboard chess engine design
  
  (Asked by: username_0 on 2021-11-13)
  
  What are the best approaches to designing a chess engine? What classes/structures should it have? How are they connected to each other? I'm building a chess engine but I often find myself overwhelmed. I've learned from numerous resources (such as chess programming wiki) the algorithms for move generation, and have implemented them myself, but the design and structure of the whole thing is just generally a mess. If you've ever made a chess engine, how did you approach this?
  
  **Tags:** engines, computer-chess, bitboard, chess-ai
  
  > **Comments:**
  > **username_1**: My first approach was to understand [firstchess](https://github.com/nguyenpham/FirstChess/blob/master/firstchess.c). It's written in C, and has the very basics of a chess engine. It lacks castle and en passant, but for the goal of understanding how a chess engine works it's not important (and it simplifies the code a lot).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-11-15. Score: 3 | Accepted answer
  
  > I'm building a chess engine but I often find myself overwhelmed
  
  I can identify quite a lot with how you're currently feeling. So before I address your questions, I'd like to give some encouragement if I may.
  
  I started working on writing a chess engine back in February of this year, so about 10 months ago. I still have the code from that first attempt I ever made at writing an engine. It was a mess, and I had no clue what I was doing, even though at that point I had been programming for a couple of years.
  
  But one thing I will always give my past self credit for is that I learned a lot from that first attempt, and the second attempt was better...and the third, and the fourth, and the fifth, and so on. If I remember correctly I actually started over about *10 times* before I finally was able to create a solid codebase for the engine that I could start building from.
  
  But every time I started over, my goal got a little closer, and I made sure I learned from my mistakes. So even if you're at the point where your code has just become too buggy or too messy to continue working from, that's alright. Learn from your mistakes this go-round, take some time to lick your wounds, create a new blank slate, and start again. If you keep persevering like this, I believe eventually you'll have something you can be very proud of.
  
  With that out of the way, let me address the questions in your post:
  
  > What are the best approaches to designing a chess engine? What
  > classes/structures should it have? How are they connected to each
  > other?
  
  Here's how to think about things conceptually. A chess engine has three basic parts:
  
  * Move generation: this phase generates all of the moves for a given position
  * Search: this phase uses the move generator to generate all of the possible moves for the root position, and for each of those nodes generate all of their moves, and so on recursively until a certain depth is reached, or a checkmate or draw occurs. If the position isn't a checkmate or draw, then the engine needs some way of figuring out if the position it stopped at is good or bad for it, so this is where the evaluation phase comes in.
  * Evaluation: This is the phase that looks at a given chess position and tries to assign an integer value for how good the position is for black or white.
  
  Hopefully, you can start to see the connection between these three phases. The search is the heartbeat of any engine, and it makes calls to the move generation phase and the evaluation phase to do its job correctly.
  
  Now, that's the 30,000 ft overview. As you've discovered, however, the implementation specifics are the hard part. Here is how I recommend you start.
  
  * Move generator: You said you've already been working on this so that's good. I would recommend using a mailbox design like [here](https://www.chessprogramming.org/10x12_Board). You'll need some different functions to generate moves for the different pieces as well. If this still seems daunting, there's are a lot of places you can find some pseudo-code. The CPW has some, but also search up "mailbox chess engines github" in google and look through some of the code you find to get some ideas. Once you have [legal or pseduo-legal](https://www.chessprogramming.org/Pseudo-Legal_Move) move generator, test it *throughly* using [PERFT](https://www.chessprogramming.org/Perft), on [multiple positions](https://github.com/algerbrex/blunder/blob/main/testdata/perftsuite.epd). If that last link's format is confusing, read a little about [FEN strings](https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation) too.
  * Search: You don't have to start off fancy here. You can just have a single function, copying the code here: <https://www.chessprogramming.org/Negamax>. Try to read a little to understand it. Then add some code in to clean things up to *your* liking. If you need to see some actually, feel free to look at an earlier version of my engine's search code [here](https://github.com/algerbrex/blunder/blob/cd8e182d1b566af4aa8d0584ec91fbbcbf75e783/ai/search.go#L58).
  * Evaluation: again, starting off, simpler is better. Just count material. Let pawns be worth say 100 points, bishops and knights worth 300, rooks worth 500, and queens worth 900. And then count how many of each piece each side has, and multiply that by the value of the piece. As I'm sure you can tell, this is quite crude, as chess is much more than just material. But it's an excellent place to start!
  
  Once you have these three parts written, implement a little bit of the [UCI protocol](http://wbec-ridderkerk.nl/html/UCIProtocol.html) and a [time-management scheme](http://mediocrechess.blogspot.com/2007/01/guide-time-management.html) so you can use your engine in a nice GUI, like Cute Chess or Arena. Again, if you need to see my code, there are many open-source engines on GitHub and other places. And always feel free to look at any code from my engine as well.
  
  At this point, you should have a working chess engine. Will it beat a grandmaster? Probably not. It probably won't even be beating you very well. But the more important point is that you'll have somewhere you can *build from*.
  
  Once you have the above implemented, start slow, and start reading about some ways to improve things. The next step could be to improve your move ordering and add a quiescence search phase. And after that, you can look into using piece-square tables, to start teaching your engine what squares are good for each piece type. And eventually, you can move on to techniques used in modern beasts like Stockfish or Komodo like pruning, and reductions, and extensions.
  
  But again, *take things **slow***, and start off simple. You can always come back later and improve things. But if you start off trying to do every little thing, you'll likely end up with a very messy and buggy program that will be no fun to continue working on.
  
  Hopefully, this helped to get you started down the right path. If you have any questions, feel free to fire away. I'd love to help out, and will try to answer when I have free time.
  
  P.S: Make an account at talkchess.com. There are many smart people, many much smarter than myself, who can also give wonderful advice about how to begin the rewarding yet sometimes challenging process of writing a novel chess engine. I can't wait to see your engine rated on the [CCRL](http://ccrl.chessdom.com/ccrl/404/) soon! :-)
  
  **Edit:** First, I realize I didn't make it clear, but if you wanted to look through the code, my engines code is posted here: <https://github.com/algerbrex/blunder> (hopefully this isn't marked as self-promotion.)
  
  Second, I forgot to recommend some YouTube series that I highly recommend you watch if you need more help with a certain part of your engine:
  
  * [Programming a chess engine in C](https://www.youtube.com/watch?v=bGAfaepBco4&list=PLZ1QII7yudbc-Ky058TEaOstZHVbT-2hg)
  * [Programming a chess engine in Javascript](https://www.youtube.com/watch?v=2eA0bD3wV3Q&list=PLZ1QII7yudbe4gz2gh9BCI6VDA-xafLog)
  * [Toyfish series](https://www.youtube.com/watch?v=l3xzDDbQq3c&list=PLmN0neTso3JyHJ4YqTo4IT8LPm4laNhZ5&index=3) (A play on the name of the current strongest engine in the world, [Stockfish](https://github.com/official-stockfish/Stockfish))
  * [Bitboard engine in C](https://www.youtube.com/watch?v=QUNP-UjujBM&list=PLmN0neTso3Jxh8ZIylk74JpwfiWNI76Cs)
  
  Both creators are great programmers and even better teachers. Hopefully, those help as well!
  
  
  ---
  
  