From 2aebd781fbb10138f56f55e8b25349708dffdcc4 Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Thu, 28 Mar 2024 20:00:33 +0100 Subject: [PATCH] GenPawnMoves function --- source/board.cpp | 1 + source/board.h | 2 +- source/main.cpp | 4 +++- source/move.h | 15 +++++++++++++++ source/movegen.cpp | 28 ++++++++++++++++++++++++++++ source/movegen.h | 19 +++++-------------- 6 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 source/move.h diff --git a/source/board.cpp b/source/board.cpp index 124e0f0..75cee8d 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -1,4 +1,5 @@ #include "board.h" +#include "movegen.h" #include void Board::Init() { diff --git a/source/board.h b/source/board.h index 074bc21..ba4c3f7 100644 --- a/source/board.h +++ b/source/board.h @@ -2,7 +2,7 @@ #include #include "bitboards.h" #include "types.h" -#include "movegen.h" +#include "move.h" #include class Board { diff --git a/source/main.cpp b/source/main.cpp index a4fb97e..7aab976 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -8,7 +8,9 @@ int main() { initSliderAttacks(); Board board; - board.PrintBoard(); + GenerateMoves(board); + + board.ListMoves(); return 0; } \ No newline at end of file diff --git a/source/move.h b/source/move.h new file mode 100644 index 0000000..1f061b0 --- /dev/null +++ b/source/move.h @@ -0,0 +1,15 @@ +#pragma once +#include "bitboards.h" + +struct Move { +public: + SQUARE moveFrom; + SQUARE moveTo; + + Move(){} + + Move(const SQUARE from, const SQUARE to) + : moveFrom(from), moveTo(to) {} + + void PrintMove(); +}; \ No newline at end of file diff --git a/source/movegen.cpp b/source/movegen.cpp index 8ea7109..b4d3adc 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -271,4 +271,32 @@ Bitboard getQueenAttack(int square, U64 occupancy) { rookOccupancy >>= (64 - rookRelevantBits[square]); return rookAttacks[square][rookOccupancy] | bishopAttacks[square][bishopOccupancy]; +} + +void GenPawnMoves(Board &board, bool color) { + Bitboard pawns = board.colors[color] & board.pieces[Pawn]; + + while (pawns.GetBoard()) { + int square = pawns.getLS1BIndex(); + int direction = color ? south : north; + + Bitboard attacks = pawnAttacks[color][square] & board.colors[!color]; + attacks |= (Bitboard::GetSquare(square + direction) + | Bitboard::GetSquare(square + 2 * direction)) & ~(board.occupied); + + while (attacks.GetBoard()) { + int targetSquare = attacks.getLS1BIndex(); + + board.AddMove(Move(SQUARE(square), SQUARE(targetSquare))); + + attacks.PopBit(targetSquare); + } + + pawns.PopBit(square); + } +} + +void GenerateMoves(Board &board) { + GenPawnMoves(board, White); + GenPawnMoves(board, Black); } \ No newline at end of file diff --git a/source/movegen.h b/source/movegen.h index c5886ff..38de265 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -1,18 +1,7 @@ #pragma once #include "bitboards.h" - -struct Move { -public: - SQUARE moveFrom; - SQUARE moveTo; - - Move(){} - - Move(const SQUARE from, const SQUARE to) - : moveFrom(from), moveTo(to) {} - - void PrintMove(); -}; +#include "move.h" +#include "board.h" // bishop relevant occupancy bit count constexpr int bishopRelevantBits[64] = { @@ -213,4 +202,6 @@ void initSliderAttacks(); Bitboard getBishopAttack(int square, U64 occupancy); Bitboard getRookAttack(int square, U64 occupancy); -Bitboard getQueenAttack(int square, U64 occupancy); \ No newline at end of file +Bitboard getQueenAttack(int square, U64 occupancy); + +void GenerateMoves(Board &board); \ No newline at end of file