Answers.Body: <p><a href="http://support.stockfishchess.org/discussions/questions/1422-generation-of-next-moves-for-all-pieces" rel="nofollow">http://support.stockfishchess.org/discussions/questions/1422-generation-of-next-moves-for-all-pieces</a></p>
  
  <p>You should use Tord (the person who wrote Stockfish)'s C++ move generation code. It's easy, simple, fast and good. It uses magic bitboard.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T00:52:37.743Z
Answers.LastActivityDate: 2015-01-06T02:27:57.203Z
Answers.LastEditDate: 2015-01-06T02:27:57.203Z
Answers.UserDisplayName: SmallChess
Answers.Body: <p>I think it is unreasonable to ask for this as a stand-alone program because its implementation will be very dependent on the design of your program as a whole. In particular, it completely depends on how you are storing each position: how are you representing the board and pieces? How do you keep track of where each piece is after each move? How do you know which squares a piece <em>can</em> move to (is there a piece there already? Is there a piece blocking the bishop's movement as it goes from c1 to f4?)? </p>
  
  <p>Of course, once you have a implemented all of the above <em>perfectly</em> then the problem of generating the moves that each piece can make becomes quite easy: simply iterate over every piece and for every piece iterate over all of its valid moves (how you store moves is, again, entirely up to you).</p>
  
  <p>Finally, <a href="https://chessprogramming.wikispaces.com/" rel="nofollow">this</a> is a great resource that gives a good overview of some important topics in chess programming.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T00:51:45.090Z
Answers.Text: -1 this makes no sense at all. Move generation is a very simple routine in programming.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T02:14:55.377Z
Answers.Text: @StudentT Did I say anywhere that generating moves is not simple? My post was addressing the challenge of integrating this simple task into a complete chess program which is considerably more challenging. Also, could you elaborate on why my post "makes no sense at all"?
Answers.UserDisplayName: sardoj
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T02:27:47.790Z
Answers.Text: 1. It's a reasonable request to ask for a stand-alone program. 2. The poster requests for a C++ solution while the link is not being very helpful
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T02:52:21.040Z
Answers.Text: @StudentT In the comments of the link that you posted the replies reference the move generation in the Stockfish codebase -- how does that disprove my statement that you can't expect move generation in a single program? Having all your move-gen code in a file called movegen.cpp does not make it a "stand-alone" program because that code will completely depend on the rest of your program. That is the point of my post: how can we provide OP with move-gen code when we don't know how the rest of his program is designed?
Answers.UserDisplayName: sardoj
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T02:55:11.833Z
Answers.Text: Of course you could. movegen.cpp is a engine-optimized source file. But the code written by Tord also provided in the link is actually a standard-alone C++ library for move generation and validation. It's not related to the engine.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-01T19:22:28.290Z
Answers.Text: @StudentT You don't get the point, he is right. Move generation logic depends on board and piece representations.
Answers.UserDisplayName: ferit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-02T01:40:45.180Z
Answers.Text: @Sailbot Do you really know what you're talking about? I wrote the request was fine. I didn't say about the dependence.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-02T14:57:06.113Z
Answers.Text: @StudentT I'm completely sure what I'm talking about. I'm a programmer.
Answers.UserDisplayName: ferit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-04T16:25:10.197Z
Answers.LastActivityDate: 2015-01-04T16:25:10.197Z
Answers.UserDisplayName: sardoj
Answers.Body: <p><a href="https://chess.stackexchange.com/questions/9706/representing-moves-found-via-bitboards-applying-them-and-removing-them">Representing Moves found via BitBoards, applying them, and removing them</a> provides a good overview of move generation using the fastest method.</p>
  
  <p>I use a different method relying on three arrays.</p>
  
  <p>key[] = {1, 2, 4, 8, 16, 32, 64, 128, ...} // bit test aligned to movement.</p>
  
  <p>movement[] = {1, -1, 8, -8, -9, -7, 9, 7, -17, -15, -10. -6, 6, 10, 15, 17}</p>
  
  <p>movementkey[64] = {...} // bit set if move is possible.</p>
  
  <p>This array is setup in such a way that if the bit is set, the corresponding movement is allowed.  For example, if the third bit is set in the movementkey array, represented easily by key[2], then movement[2] is a legal square.</p>
  
  <p>Loop through the pieces. </p>
  
  <p>P: check based upon color</p>
  
  <p>N: loop through last eight indexes.</p>
  
  <p>B: call Slider function with index value of 4.</p>
  
  <p>R: call slider function with index value of 0.</p>
  
  <p>Q: call slider function twice, with 0 and 4.</p>
  
  <p>K: simple one square movement.</p>
  
  <p>check for EP, and castling.</p>
  
  <p>The Slider function:</p>
  
  <p>tosquare = fromsquare </p>
  
  <p>loop from index to index + 4</p>
  
  <p>if bit is set fromsquare += movement</p>
  
  <p>if empty addmove and continue</p>
  
  <p>else</p>
  
  <pre><code>if NotOwnPiece addmove
  break
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-01T12:27:03.900Z
Answers.LastActivityDate: 2016-04-01T12:27:03.900Z
Answers.LastEditDate: 2017-04-13T12:48:53.737Z
Answers.UserDisplayName: Mike Jones
Body: <p>I am looking for an efficient (fast) program to generate next moves for all pieces.</p>
  
  <p>I need it in C++, because I will program in C++.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-04T12:36:07.563Z
Comments.Text: What do you want this for? Are you attempting to write your own chess engine? The move generation code is one of the easier parts of writing an engine: if you can't do that part, there's very little chance that you'll be able write the rest of the search routine. It's like saying, "Hey, I need to fly this plane. Can somebody taxi it to the end of the runway for me?"
Comments.UserDisplayName: David Richerby
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-04T15:03:08.343Z
Comments.Text: https://chessprogramming.wikispaces.com
Comments.UserDisplayName: Pete Becker
Tags: <engines><software>
Title: An efficient program, which generates all next moves for all pieces in C/C++
ThreadText: # An efficient program, which generates all next moves for all pieces in C/C++
  
  (Asked by: username_0 on 2015-01-04)
  
  I am looking for an efficient (fast) program to generate next moves for all pieces.
  
  I need it in C++, because I will program in C++.
  
  **Tags:** engines, software
  
  > **Comments:**
  > **username_1**: What do you want this for? Are you attempting to write your own chess engine? The move generation code is one of the easier parts of writing an engine: if you can't do that part, there's very little chance that you'll be able write the rest of the search routine. It's like saying, "Hey, I need to fly this plane. Can somebody taxi it to the end of the runway for me?"  
  > **username_2**: https://chessprogramming.wikispaces.com  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2015-01-06. Score: 0 | Accepted answer
  
  <http://support.stockfishchess.org/discussions/questions/1422-generation-of-next-moves-for-all-pieces>
  
  You should use Tord (the person who wrote Stockfish)'s C++ move generation code. It's easy, simple, fast and good. It uses magic bitboard.
  
  
  ---
  
  ### Answer by username_4 on 2015-01-04. Score: 3
  
  I think it is unreasonable to ask for this as a stand-alone program because its implementation will be very dependent on the design of your program as a whole. In particular, it completely depends on how you are storing each position: how are you representing the board and pieces? How do you keep track of where each piece is after each move? How do you know which squares a piece *can* move to (is there a piece there already? Is there a piece blocking the bishop's movement as it goes from c1 to f4?)?
  
  Of course, once you have a implemented all of the above *perfectly* then the problem of generating the moves that each piece can make becomes quite easy: simply iterate over every piece and for every piece iterate over all of its valid moves (how you store moves is, again, entirely up to you).
  
  Finally, [this](https://chessprogramming.wikispaces.com/) is a great resource that gives a good overview of some important topics in chess programming.
  
  > **Comments:**
  > **username_3**: -1 this makes no sense at all. Move generation is a very simple routine in programming.  
  > **username_4**: @StudentT Did I say anywhere that generating moves is not simple? My post was addressing the challenge of integrating this simple task into a complete chess program which is considerably more challenging. Also, could you elaborate on why my post "makes no sense at all"?  
  > **username_3**: 1. It's a reasonable request to ask for a stand-alone program. 2. The poster requests for a C++ solution while the link is not being very helpful  
  > **username_4**: @StudentT In the comments of the link that you posted the replies reference the move generation in the Stockfish codebase -- how does that disprove my statement that you can't expect move generation in a single program? Having all your move-gen code in a file called movegen.cpp does not make it a "stand-alone" program because that code will completely depend on the rest of your program. That is the point of my post: how can we provide OP with move-gen code when we don't know how the rest of his program is designed?  
  > **username_3**: Of course you could. movegen.cpp is a engine-optimized source file. But the code written by Tord also provided in the link is actually a standard-alone C++ library for move generation and validation. It's not related to the engine.  
  > **username_5**: @StudentT You don't get the point, he is right. Move generation logic depends on board and piece representations.  
  > **username_3**: @Sailbot Do you really know what you're talking about? I wrote the request was fine. I didn't say about the dependence.  
  > **username_5**: @StudentT I'm completely sure what I'm talking about. I'm a programmer.  
  
  ---
  
  ### Answer by username_6 on 2016-04-01. Score: 1
  
  [Representing Moves found via BitBoards, applying them, and removing them](https://chess.stackexchange.com/questions/9706/representing-moves-found-via-bitboards-applying-them-and-removing-them) provides a good overview of move generation using the fastest method.
  
  I use a different method relying on three arrays.
  
  key[] = {1, 2, 4, 8, 16, 32, 64, 128, ...} // bit test aligned to movement.
  
  movement[] = {1, -1, 8, -8, -9, -7, 9, 7, -17, -15, -10. -6, 6, 10, 15, 17}
  
  movementkey[64] = {...} // bit set if move is possible.
  
  This array is setup in such a way that if the bit is set, the corresponding movement is allowed. For example, if the third bit is set in the movementkey array, represented easily by key[2], then movement[2] is a legal square.
  
  Loop through the pieces.
  
  P: check based upon color
  
  N: loop through last eight indexes.
  
  B: call Slider function with index value of 4.
  
  R: call slider function with index value of 0.
  
  Q: call slider function twice, with 0 and 4.
  
  K: simple one square movement.
  
  check for EP, and castling.
  
  The Slider function:
  
  tosquare = fromsquare
  
  loop from index to index + 4
  
  if bit is set fromsquare += movement
  
  if empty addmove and continue
  
  else
  
  ```
  if NotOwnPiece addmove
  break
  
  ```
  
  
  ---
  
  