Code: Select all
#include "movegen.h"
#include "bitboard.h"
#include "func.h"
#include "move.h"
#include "names.h"
#include "const.h"
#define WHITE_OO_CASTLE 6
#define WHITE_OOO_CASTLE 112
inline BitMap whiteSinglePush(const UINT &sq, const BitMap &empty) {
return ((1 << sq) << 8) & empty;
}
inline BitMap whiteDoublePush(const UINT &sq, const BitMap &empty) {
BitMap singlePush = whiteSinglePush(sq, empty);
return (singlePush << 8) & empty & 0x00000000FF000000; // 0x00000000FF000000 = rank 4.
}
inline BitMap blackSinglePush(const BitMap &bpawns, const BitMap &empty) {
return (bpawns >> 8) & empty;
}
inline BitMap blackDoublePush(const BitMap &bpawns, const BitMap &empty) {
singlePush = blackSinglePush(bpawns, empty);
return (singlePush << 8) & empty & 0x000000FF00000000; // 0x000000FF00000000 = rank 5.
}
inline BitMap genBishopMoves(const UINT &square, const U64 &board, const U64 &target) {
BitMap bishopMoves = 0;
bishopMoves = arrDiagA8H1Moves[(square)][((board & arrDiagA8H1Mask[(square)]) * arrDiagA8H1Magics[(square)]) >> 57];
bishopMoves |= arrDiagA1H8Moves[(square)][((board & arrDiagA1H8Mask[(square)]) * arrDiagA1H8Magics[(square)]) >> 57];
bishopMoves &= target;
return bishopMoves;
}
inline BitMap genRookMoves(const UINT &square, const U64 &board, const U64 &target) {
BitMap rookMoves = 0;
rookMoves |= (arrRankMoves[(square)][((board & arrRankMask[(square)]) >> arrRankShifts[(square)])]);
rookMoves |= (arrFileMoves[(square)][((board & arrFileMask[(square)]) * arrFileMagics[(square)]) >> 57]);
rookMoves &= target;
return rookMoves;
}
inline BitMap genQueenMoves(const UINT &square, const U64 &board, const U64 &target) {
return (genRookMoves(square, board, target) | genBishopMoves(square, board, target));
}
void Bitboard::GenWhiteMoves() {
register int index, from, to;
register CMove tempMove; // Stores the temporary generated move, then copied into Moves
register BitMap targets, piece;
/********************************************
* *
* White Pawn Moves *
* *
*********************************************/
BitMap empty = (~Bitboard::occupiedSquares);
tempMove.clearMove();
targets = index = from = to = 0;
tempMove.setPiece(WHITEPAWN);
piece = Bitboard::whitePawns;
while(piece) {
from = lsb(piece);
tempMove.setFrom(from);
std::cout << "White Single Push, sq=" << from << " empty=" << empty << "returns " << whiteSinglePush(from, empty) << std::endl;
targets = whiteSinglePush(from, empty);
std::cout << "Targets now:" << targets << std::endl;
std::cout << "White Doble Push, sq=" << from << " empty=" << empty << "returns " << whiteDoublePush(from, empty) << std::endl;
targets |= whiteDoublePush(from, empty);
std::cout << "Targets now:" << targets << std::endl;
targets |= ((arrPawnAttacks[0][from]) & (Bitboard::blackPieces));
if(Bitboard::epSquare) {
if((from == (Bitboard::epSquare - 1)) || (from == (Bitboard::epSquare + 1))) {
tempMove.setTo(Bitboard::epSquare - 8);
tempMove.setFlags(WHITEPAWN);
tempMove.setCapture(WHITEPAWN);
Moves[index++].m_move = tempMove.m_move;
}
}
while(targets) {
to = lsb(targets);
tempMove.setTo(to);
if(to < 56) {
Moves[index++].m_move = tempMove.m_move;
} else { // I've lately started to use K&R Style fromatting
tempMove.setFlags(WHITEQUEEN);
Moves[index++].m_move = tempMove.m_move;
tempMove.setFlags(WHITEROOK);
Moves[index++].m_move = tempMove.m_move;
tempMove.setFlags(WHITEBISHOP);
Moves[index++].m_move = tempMove.m_move;
tempMove.setFlags(WHITEKNIGHT);
Moves[index++].m_move = tempMove.m_move;
}
// Remove the least significant bit:
targets &= (targets - 1);
}
piece &= (piece - 1);
}
/********************************************
* *
* White Knight Moves *
* *
*********************************************/
tempMove.clearMove();
tempMove.setPiece(WHITEKNIGHT);
piece = Bitboard::whiteKnights;
targets = 0;
while(piece) {
from = lsb(piece);
tempMove.setFrom(from);
targets |= (arrKnightAttacks[from] & (~Bitboard::whitePieces));
while(targets) {
to = lsb(targets);
tempMove.setTo(to);
tempMove.setCapture(Bitboard::square[to]);
Moves[index++].m_move = tempMove.m_move;
targets &= (targets - 1);
}
piece &= (piece - 1);
}
/********************************************
* *
* White King Moves *
* *
*********************************************/
tempMove.clearMove();
tempMove.setPiece(WHITEKING);
piece = Bitboard::whiteKing;
targets = 0;
while(piece) {
from = lsb(piece);
tempMove.setFrom(from);
targets |= (arrKingAttacks[from] & (~Bitboard::whitePieces));
while(targets) {
to = lsb(targets);
tempMove.setTo(to);
tempMove.setCapture(Bitboard::square[to]);
Moves[index++].m_move = tempMove.m_move;
targets &= (targets - 1);
}
piece &= (piece - 1);
}
/********************************************
* *
* White Bishop Moves *
* *
*********************************************/
tempMove.clearMove();
tempMove.setPiece(WHITEBISHOP);
piece = Bitboard::whiteBishops;
targets = 0;
while(piece) {
from = lsb(piece);
tempMove.setFrom(from);
targets = genBishopMoves(from, Bitboard::occupiedSquares, (~Bitboard::whitePieces));
while(targets) {
to = lsb(targets);
tempMove.setTo(to);
tempMove.setCapture(Bitboard::square[to]);
Moves[index++].m_move = tempMove.m_move;
targets &= (targets - 1);
}
piece &= (piece - 1);
}
/********************************************
* *
* White Rook Moves *
* *
*********************************************/
tempMove.clearMove();
tempMove.setPiece(WHITEROOK);
piece = Bitboard::whiteRooks;
targets = 0;
while(piece) {
from = lsb(piece);
tempMove.setFrom(from);
targets = genRookMoves(from, Bitboard::occupiedSquares, (~Bitboard::whitePieces));
while(targets) {
to = lsb(targets);
tempMove.setTo(to);
tempMove.setCapture(Bitboard::square[to]);
Moves[index++].m_move = tempMove.m_move;
targets &= (targets - 1);
}
piece &= (piece - 1);
}
/********************************************
* *
* White Queen Moves *
* *
*********************************************/
tempMove.clearMove();
tempMove.setPiece(WHITEQUEEN);
piece = Bitboard::whiteQueens;
targets = 0;
while(piece) {
from = lsb(piece);
tempMove.setFrom(from);
targets = genQueenMoves(from, Bitboard::occupiedSquares, (~Bitboard::whitePieces));
while(targets) {
to = lsb(targets);
tempMove.setTo(to);
tempMove.setCapture(Bitboard::square[to]);
Moves[index++].m_move = tempMove.m_move;
targets &= (targets - 1);
}
piece &= (piece - 1);
}
/********************************************
* *
* White Castling Moves *
* *
*********************************************/
// White O-O Castling Moves:
if((Bitboard::WhiteCastleOO) &&
(!((Bitboard::occupiedSquares) & WHITE_OO_CASTLE)) &&
(!(isBlackAttacking(2))))
{
Moves[index].setFrom(4);
Moves[index].setTo(6);
Moves[index].setPiece(WHITEKING);
Moves[index++].setFlags(WHITEKING);
}
// White O-O-O Castling Moves:
if((Bitboard::WhiteCastleOOO) &&
(!((Bitboard::occupiedSquares) & WHITE_OOO_CASTLE)) &&
(!(isBlackAttacking(32))))
{
Moves[index].setFrom(4);
Moves[index].setTo(2);
Moves[index].setPiece(WHITEKING);
Moves[index++].setFlags(WHITEKING);
}
Bitboard::noOfMoves = index;
}