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

Move and Board need convenience methods to avoid using bitboard internals directly. #1

Open
DeybisMelendez opened this issue Aug 14, 2022 · 4 comments

Comments

@DeybisMelendez
Copy link
Contributor

I don't really understand how bitboards work, so I have no idea how to know which piece I'm moving or which player it belongs to.

I would really appreciate a little explanation or adding a method that allows you to obtain this information directly.

@dylhunn
Copy link
Owner

dylhunn commented Aug 15, 2022

You should be able to use Move.To() and Move.From() methods to check which squares are being used. API is defined here.

The best explanation of bitboards I know of is here on the Chess Programming Wiki.

You might consider adding some helper methods on Move or Board to make this easier -- e.g. a convenience method that takes a square ID and tells you what piece code is present.

@dylhunn dylhunn changed the title How can I get the part from Move? Move and Board need convenience methods to avoid using bitboard internals directly. Aug 15, 2022
@DeybisMelendez
Copy link
Contributor Author

Move.From() returns uint8, what operation should I do to get the Piece?

@DeybisMelendez
Copy link
Contributor Author

I wrote this:

import chess "github.com/dylhunn/dragontoothmg"
func GetPiece(move chess.Move, board *chess.Board) {
	squareMask := uint64(1) << move.From()
	Piece := chess.Nothing
	if board.Wtomove {
		if board.White.Knights&squareMask != 0 {
			Piece = chess.Knight
		} else if board.White.Pawns&squareMask != 0 {
			Piece = chess.Pawn
		} /// etc...
	} else { // etc...
	fmt.Println(Piece)
}

Is this correct?

@dylhunn
Copy link
Owner

dylhunn commented Aug 15, 2022

@DeybisMelendez

Move.From() returns uint8, what operation should I do to get the Piece?

In order to save space in the Move type, it doesn't actually contain the piece kind -- you need the Board for that. From the code:

// Data stored inside, from LSB
// 6 bits: destination square
// 6 bits: source square
// 3 bits: promotion

So you need to check all the bitboards for pieces at that square. What you're doing above looks right!

Be aware that using your helper method will incur a small performance penalty, since you're checking all 10 bitboards. However, each check is still O(1) constant time, so it's not that bad.

Feel free to send me a PR if you'd like to add this to the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants