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

Add in_double_check() #320

Closed
wants to merge 3 commits into from
Closed

Conversation

fitztrev
Copy link
Contributor

@fitztrev fitztrev commented Apr 4, 2022

I was looking for a way to be able to detect "double checkmates" (example here)

I'll submit this as a draft PR for discussion, knowing that #319 will have a new development branch. If this gets approved, I will re-do against the FIX_ME branch.

@jhlywa
Copy link
Owner

jhlywa commented Apr 4, 2022

Hey Trevor, could you do the same thing with something a little more generic? Maybe simplify the concept into an attackers() API call? In the end it's a few more lines of code for your specific use case, but I think it results in greater flexibility for the library. Something like:

const kingSquare = somehow get the king square for the side to move 
const attackers = chess.attackers(kingSquare) /// [{piece: 'n', square: 'f3', color: 'b'}, ...]
const doubleAttack = attackers.length === 2

So we'd need a way to retrieve the square for the king, or more generically any piece of either color. Maybe return a list of squares for the piece/color in question.

This approach also gives us defenders() for free too (something like function defenders(square) { return attackers(square, swap(this.turn)) }), right?

Thoughts?

@fitztrev
Copy link
Contributor Author

fitztrev commented Apr 5, 2022

I like that approach.

In this example, would the bishop, queen, and e1 rook count as attackers of e5? And if so, would that work with the way square/attack mappings are currently setup? I'll have to dig into that part of the code more to understand how it works.

image

@jhlywa
Copy link
Owner

jhlywa commented Apr 5, 2022

Maybe something like attackers('e4', { xray: true }) for something like that?

Xray gets a little complicated if some of the attackers are pinned. I guess the pinned piece counts as an attacker but nothing "behind" it does? I'm ok punting on xray support for the time being.

@fitztrev
Copy link
Contributor Author

fitztrev commented Apr 9, 2022

I pushed some updates. Below would be a proposed sample use. Let me know if this is the right idea.

Given this position:
Screen Shot 2022-04-08 at 11 57 32 PM

// get white attackers of d5
chess.attackers('w', 'd5')

// result
[
  { square: 'f3', color: 'w', type: 'q' },
  { square: 'd1', color: 'w', type: 'r' },
]

@JacobEvelyn
Copy link

A project I'm working on would really benefit from this attackers()/defenders() API. (The xray feature would be nice but is not mandatory for me.) Let me know if there's anything I can do to help get this polished and merged.

@fitztrev fitztrev closed this Jan 26, 2024
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.

3 participants