9 votes
Accepted

Find magic numbers for bitboards

This is a very well known problem in chess programming. You should consider use the numbers generated by Pradyumna Kannan. Dr Kannan had kindly produced open-source the magic numbers. It is being used ...
user avatar
  • 21.4k
8 votes

Representing Moves found via BitBoards, applying them, and removing them

This is a very deep subject, but here's a few basic thoughts that might get you going. A bitboard consists of 64 bits. If you designate one corner of the board as the most-significant bit and the ...
user avatar
  • 425
6 votes
Accepted

Chess Engine: Generate a Bitboard of Pinned Pieces

This task is nontrivial and cannot be obtained easily or setwise from the available bitboards. The following solution is an example for how such a task might be done for a generic slider - only ...
user avatar
  • 2,983
6 votes
Accepted

What's the right approach of storing moves generated using bitboards?

Q1: Should I generate all pawn moves at once? Yes. There's no reason not to do that. But... your code wouldn't work because you're returning a bitboard in your function. You should instead return a ...
user avatar
  • 21.4k
5 votes
Accepted

Representing Moves found via BitBoards, applying them, and removing them

My last chess engine used bitboards to store position information. It represented moves as a vector of simple move structures that looks something like this: struct move_info { char move[2]; ...
user avatar
5 votes
Accepted

How do I apply moves (bitboard) and continue searching? My current attempt is throwing a strange error!

The issue may be here: Board newBoard = board; This creates a new variable, but it doesn't actually create a new Board. board and newBoard are pointing to the exact same Board object. So, when your ...
user avatar
  • 15.1k
4 votes

Quickly converting board to bitboard representation using python-chess library

To convert a single bitboard to a numpy array: def bitboard_to_array(bb: int) -> np.ndarray: s = 8 * np.arange(7, -1, -1, dtype=np.uint64) b = (bb >> s).astype(np.uint8) b = np....
user avatar
4 votes

Quickly converting board to bitboard representation using python-chess library

python-chess already has the built in utilities to handle conversions. Here is an example: import chess board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1') WP = board....
user avatar
3 votes
Accepted

Bitboard chess engine design

I'm building a chess engine but I often find myself overwhelmed I can identify quite a lot with how you're currently feeling. So before I address your questions, I'd like to give some encouragement ...
user avatar
3 votes

Fastest way to generate castling moves in a computer chess engine

It's been a long time since I've made any efficiency mods, but this can be reduced to one if statement, but many would be easier to read. if !bitboard_occupied // easy to find by ORing with f1/g1 (or ...
user avatar
  • 5,123
3 votes
Accepted

Fastest way to parse a bitboard

I found some well hidden pages in the Chess Programming Wiki. The first one basically introduces your solution: https://www.chessprogramming.org/Bitboard_Serialization#Single_Bits However, the next ...
user avatar
  • 1,144
3 votes
Accepted

How do I complete this implementation of magic bitboards?

It is probably most efficient if I paste my code from my chess move generator on GitHub. int ROOK_ATTACK_SHIFTS[64]; Bitboard ROOK_ATTACK_MASKS[64]; Bitboard ROOK_ATTACKS[64][4096]; void ...
user avatar
3 votes

What's the right approach of storing moves generated using bitboards?

I really appreciate the response from our contributors, but still there is a question unanswered floating on air. How do I implement the “MOVE” structure? Based on Bitboard approach? In reality, ...
user avatar
  • 151
3 votes
Accepted

Are these magics wrong?

I think you're misunderstanding something here based on the way you formulated your question. Multiplying the blockers by the magic and the shifting does not give you the attack set. What it gives ...
user avatar
3 votes

Can engines use the current pawn structure to refine its search/eval?

Something very similar is doubtless encoded into AlphaZero and Leela's neural network evaluators, which are very positional. Most conventional Minimax engines, by contrast, are stronger at tactics ...
user avatar
  • 1,659
2 votes

Understanding magic bitboard

It's not about avoiding the border. It's about avoiding the last bit of the ray. Given this: 10000000 10000000 10000000 10000000 10000000 10000000 10000000 01111111 (Note that A1 should be zero as ...
user avatar
2 votes
Accepted

Building a chess using magic bitboard... how do I know if the movement is valid

Let me answer your questions for @MikeJones: You will need to store the generated moves in a bitboard. The following code example should work: unsigned int moves = .... // This gives a bitboard of ...
user avatar
  • 21.4k
2 votes

Building a chess using magic bitboard... how do I know if the movement is valid

Make a bitboard of all the white pieces. If the AND of the possible knight move and the white pieces is TRUE, then the move is illegal--attempting to capture your own piece. The simplest way is to ...
user avatar
  • 5,123
2 votes

Chess Engine: Generate a Bitboard of Pinned Pieces

Pinned pieces only exist when attackers are sliding pieces (bishop, rook, queen). Also pinned pieces can move along the attack ray to the king, you cannot exclude them from legal moves directly. For ...
user avatar
  • 121
2 votes

Fastest way to generate castling moves in a computer chess engine

I am not sure about the fastest way but I used pre-defined bitboards with 1:s on the bishop and knight squares (and queen if castling queen side) respectively and AND those together with the all ...
user avatar
  • 242
2 votes
Accepted

Move generation for sliding pieces and Hyperbola Quintessence

Your implementation looks incorrect to me. Here's the correct hyperbola quintessence implementation: Bitboard hyp_quint(Square square, Bitboard occ, Bitboard mask) { return (((mask & occ) - ...
user avatar
2 votes
Accepted

Search function for chess engines

One possible approach is to decrement the evaluation for a mate score. For instance: evaluation = -search(pos, depth - 1, -beta, -alpha); if (evaluation > 900) { evaluation--; } else if (...
user avatar
  • 2,532
1 vote
Accepted

Evaluation function in a bitboard based chess engine

Given there is a maximum of 16 pieces on the chessboard you could loop on the pieces instead of the 64 squares. In this way, the less pieces on the board the faster. Each piece contains the index of ...
user avatar
1 vote
Accepted

Bitboards and sliding piece attacks

You would use the same AND-ing of the board map and the attack bitmap to get a set of attacked positions, but you must then also cast a ray from any attacked piece back to the attacker to verify that ...
user avatar
1 vote
Accepted

Fastest way to generate castling moves in a computer chess engine

Castling checks seem easy to short-circuit to be efficient. For my own implementation (not blazing fast), I did the following checks in order: Generate pseudo-legal move: Castling rights: Booleans ...
user avatar
  • 4,830
1 vote

How to store moves for a magic bitboard system?

It sounds like you have all the tools to do the job, you just need to put it all together. And yes, you can use the magic numbers you found. When your engine initializes (or any time before you begin ...
user avatar
1 vote
Accepted

Magic Bitboards, Feeding in Randoms - Why have to find magic instead of just loading from disk?

Computing magics at statup is fine because it's done only once and not very slow. https://github.com/official-stockfish/Stockfish/blob/master/src/bitboard.cpp // init_magics() computes all rook ...
user avatar
  • 21.4k
1 vote

Representing Moves found via BitBoards, applying them, and removing them

Smallchess berated me but didn't understand my post. I can't put a comment yet, so I will detail my example with more step by step explanations in this new post : bmna6 contains 4 bits at 1, for the ...
user avatar
  • 366

Only top scored, non community-wiki answers of a minimum length are eligible