Skip to content
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

Merged
merged 4 commits into from
May 10, 2024
Merged

#186: Add attackers() #456

merged 4 commits into from
May 10, 2024

Conversation

kylebatucal
Copy link
Contributor

@kylebatucal kylebatucal commented Apr 17, 2024

Changes

  • Added attackers(), a new public method that returns a list of attacking squares, as mentioned in How to find defended and attacked pieces? #186. The method follows all the same rules as isAttacked().
  • Modified _attacked() to take in an optional boolean parameter, verbose. When verbose is false, it behaves like normal (unchanged). When verbose is true, it returns a list of attacking squares.
  • Created test cases for attackers().
  • Updated the README.md.

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):

const chess = new Chess()

chess.attackers('f3', WHITE)
// -> ['e2', 'g2', 'g1'] (we can attack empty squares)

chess.attackers('f6', BLACK)
// -> ['g8', 'e7', 'g7'] (side to move (e.g. the value returned by .turn) is ignored)

chess.attackers('e2', WHITE)
// -> ['d1', 'e1', 'f1', 'g1'] (we can attack our own pieces)

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
Copy link
Owner

jhlywa commented May 9, 2024

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)?

@kylebatucal
Copy link
Contributor Author

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
can attack the given square. This function takes an optional parameter which can
change which color the pieces should belong to.

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 jhlywa merged commit 52f7579 into jhlywa:master May 10, 2024
3 checks passed
@hkilaru
Copy link

hkilaru commented May 19, 2024

@jhlywa hoping to use this new method :) could you publish the changes?

@kylebatucal
Copy link
Contributor Author

@hkilaru Hey Harish, this method is in the main release already. Make sure you update your repo!

@hkilaru
Copy link

hkilaru commented May 20, 2024

@kylebatucal saw it's merged, but don't the changes need to be published to npm? I'm on ^1.0.0-beta.8, but the last publish was 3 months ago: https://www.npmjs.com/package/chess.js/v/1.0.0-beta.8

@chao-mu
Copy link

chao-mu commented Aug 16, 2024

I'm wondering the same thing

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants