-
Notifications
You must be signed in to change notification settings - Fork 913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#186: Add attackers() #456
Conversation
Hi @kylebatucal and thanks for the PR! This is a nice improvement to the library. Do you think you can make the new method use a default value for the color (defaulting it to the current side to move)? |
Thanks for the response @jhlywa! I have gone ahead and added your suggestion to the PR. I have also updated the test cases and the README: README.md.attackers(square, [ color ])Returns a list of squares that have pieces belonging to the side to move that const chess = new Chess()
chess.attackers('f3')
// -> ['e2', 'g2', 'g1'] (empty squares can be attacked)
chess.attackers('e2')
// -> ['d1', 'e1', 'f1', 'g1'] (we can attack our own pieces)
chess.attackers('f6')
// -> [] (squares not attacked by the side to move will return an empty list)
chess.move('e4')
chess.attackers('f6')
// -> ['g8', 'e7', 'g7'] (return value changes depending on side to move)
chess.attackers('f3', WHITE)
// -> ['g2', 'd1', 'g1'] (side to move can be ignored by specifying a color)
chess.load('4k3/4n3/8/8/8/8/4R3/4K3 w - - 0 1')
chess.attackers('c6', BLACK)
// -> ['e7'] (pieces still attack a square even if they are pinned) |
@jhlywa hoping to use this new method :) could you publish the changes? |
@hkilaru Hey Harish, this method is in the main release already. Make sure you update your repo! |
@kylebatucal saw it's merged, but don't the changes need to be published to npm? I'm on |
I'm wondering the same thing |
Changes
verbose
. Whenverbose
is false, it behaves like normal (unchanged). Whenverbose
is true, it returns a list of attacking squares.Any previous code that calls _attacked() should remain unaffected since the
verbose
parameter is optional. Running all the test cases before and after modifying _attacked() shows no difference in performance.Examples (as shown in the README.md):