Answers.Body: <p>It may help to keep a list of each piece and the squares it attacks for the position. </p>
  
  <p>Then when a move occurs, you can reduce the work calculating for the pieces that don't move. Non-moving kings, knights, and pawns will attack the same squares as before. Non-moving queens, bishops, and rooks only need to be recalculated if a piece moved to or away from a square they attacked.</p>
  
  <p>Then you can update the attack squares of the pieces that actually moved.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-27T15:05:12.880Z
Answers.Text: Caveat: if you capture en passant you have to take into account that a square the moving pawn never occupied is now empty (the square of the captured pawn).
Answers.UserDisplayName: 11684
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-25T14:00:49.413Z
Answers.LastActivityDate: 2019-09-25T14:00:49.413Z
Answers.UserDisplayName: D Krueger
Body: <p>I have made a chess engine, and it seems from cProfile that my most expensive function is the one that calculates attack squares for both players. Here is the cProfile results for a perft function I wrote that goes 4 ply deep: <a href="https://i.sstatic.net/TADwV.jpg" rel="nofollow noreferrer">https://i.sstatic.net/TADwV.jpg</a></p>
  
  <p>The reason why my calculate attack squares function is so expensive is because every time a move is made in perft, the position is a new one, and attack squares for both players have to be recalculated. My question is this: Is there a more efficient way to be calculating attack squares for both players? Is there a way I can avoid having to recalculate attack squares for all 32 pieces every time a move is made in perft? Thank you.</p>
  
  <p><a href="https://repl.it/@kasparov/ImportantNotedCoderesource" rel="nofollow noreferrer">https://repl.it/@kasparov/ImportantNotedCoderesource</a>
  My code for anyone who's curious</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-09-25T13:01:32.657Z
Comments.Text: These questions, while tangentially chess-related, seem like you would get more response in a programming forum.
Comments.UserDisplayName: PhishMaster
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-09-28T23:16:11.610Z
Comments.Text: You could use the list of existing attack squares from the previous position, and then update it accordingly with just the changes the last move made on the new position. This is more efficient than recalculating entirely.
Comments.UserDisplayName: Inertial Ignorance
Tags: <engines><performance>
Title: What is the most efficient way to calculate attack squares in a chess engine?
ThreadText: # What is the most efficient way to calculate attack squares in a chess engine?
  
  (Asked by: username_0 on 2019-09-25)
  
  I have made a chess engine, and it seems from cProfile that my most expensive function is the one that calculates attack squares for both players. Here is the cProfile results for a perft function I wrote that goes 4 ply deep: <https://i.sstatic.net/TADwV.jpg>
  
  The reason why my calculate attack squares function is so expensive is because every time a move is made in perft, the position is a new one, and attack squares for both players have to be recalculated. My question is this: Is there a more efficient way to be calculating attack squares for both players? Is there a way I can avoid having to recalculate attack squares for all 32 pieces every time a move is made in perft? Thank you.
  
  <https://repl.it/@username_0/ImportantNotedCoderesource>
  My code for anyone who's curious
  
  **Tags:** engines, performance
  
  > **Comments:**
  > **username_1**: These questions, while tangentially chess-related, seem like you would get more response in a programming forum.  
  > **username_2**: You could use the list of existing attack squares from the previous position, and then update it accordingly with just the changes the last move made on the new position. This is more efficient than recalculating entirely.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2019-09-25. Score: 2
  
  It may help to keep a list of each piece and the squares it attacks for the position.
  
  Then when a move occurs, you can reduce the work calculating for the pieces that don't move. Non-moving kings, knights, and pawns will attack the same squares as before. Non-moving queens, bishops, and rooks only need to be recalculated if a piece moved to or away from a square they attacked.
  
  Then you can update the attack squares of the pieces that actually moved.
  
  > **Comments:**
  > **username_4**: Caveat: if you capture en passant you have to take into account that a square the moving pawn never occupied is now empty (the square of the captured pawn).  
  
  ---
  
  