Chess Engine
C++ chess engine with movegen, bitboards, and Arduino-friendly docs
Loading...
Searching...
No Matches
move_generator.cpp File Reference
#include <iostream>
#include "move_generator.h"
#include "make_move.h"
#include "../util/attack.h"
#include "../util/setup.h"
Include dependency graph for move_generator.cpp:

Go to the source code of this file.

Functions

static void AddQuietMove (const S_BOARD *board, int move, S_MOVELIST *moveList)
static void AddCaptureMove (const S_BOARD *board, int move, S_MOVELIST *moveList)
static void AddEnPassantMove (const S_BOARD *board, int move, S_MOVELIST *moveList)
static void AddWhitePawnCapMove (const S_BOARD *board, int fromSq, int toSq, int captured, S_MOVELIST *moveList)
static void AddWhitePawnMove (const S_BOARD *board, int fromSq, int toSq, S_MOVELIST *moveList)
static void AddBlackPawnCapMove (const S_BOARD *board, int fromSq, int toSq, int captured, S_MOVELIST *moveList)
static void AddBlackPawnMove (const S_BOARD *board, int fromSq, int toSq, S_MOVELIST *moveList)
void InitMvvLva ()
 Initialize the MVV-LVA capture scoring table.
int moveExists (S_BOARD *board, const int move)
 Check if a given move exists in the current position.
void generateAllMoves (const S_BOARD *board, S_MOVELIST *moveList)
 Generate all pseudo-legal moves for the side to move.
void generateAllCaptures (const S_BOARD *board, S_MOVELIST *moveList)
 Generate all pseudo-legal capture moves (including en passant).

Variables

const int LoopSlidePce [8] = { wB, wR, wQ, 0, bB, bR, bQ, 0 }
 Sliding piece loop order for move generation.
const int LoopNonSlidePce [6] = { wN, wK, 0, bN, bK, 0 }
 Non-sliding piece loop order for move generation.
const int LoopSlideIndex [2] = { 0, 4 }
 Indices into LoopSlidePce for white and black.
const int LoopNonSlideIndex [2] = { 0, 3 }
 Indices into LoopNonSlidePce for white and black.
const int pieceDirection [13][8]
 Direction offsets for each piece type.
const int numberDirections [13] = { 0, 0, 8, 4, 4, 8, 8, 0, 8, 4, 4, 8, 8 }
 Number of valid directions for each piece type.
const int VictimScore [13] = { 0,100,200,300,400,500,600,100,200,300,400,500,600 }
 Victim scoring table for MVV-LVA.
static int MvvLvaScores [13][13]

Function Documentation

◆ AddBlackPawnCapMove()

void AddBlackPawnCapMove ( const S_BOARD * board,
int fromSq,
int toSq,
int captured,
S_MOVELIST * moveList )
static

Definition at line 109 of file move_generator.cpp.

References AddCaptureMove(), ASSERT, bB, bN, bQ, bR, EMPTY, isBoardStateValid(), MOVE, pieceValueidEmpty(), RANK_2, rankIndex120, and SqOnBoard().

Referenced by generateAllCaptures(), and generateAllMoves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddBlackPawnMove()

void AddBlackPawnMove ( const S_BOARD * board,
int fromSq,
int toSq,
S_MOVELIST * moveList )
static

Definition at line 125 of file move_generator.cpp.

References AddQuietMove(), ASSERT, bB, bN, bQ, bR, EMPTY, isBoardStateValid(), MOVE, RANK_2, rankIndex120, and SqOnBoard().

Referenced by generateAllMoves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddCaptureMove()

void AddCaptureMove ( const S_BOARD * board,
int move,
S_MOVELIST * moveList )
static

Definition at line 53 of file move_generator.cpp.

References ASSERT, CAPTURED, S_MOVELIST::count, FROMSQ, isBoardStateValid(), S_MOVE::move, S_MOVELIST::moves, MvvLvaScores, S_BOARD::pieces, pieceValueid(), SqOnBoard(), and TOSQ.

Referenced by AddBlackPawnCapMove(), AddWhitePawnCapMove(), generateAllCaptures(), and generateAllMoves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddEnPassantMove()

void AddEnPassantMove ( const S_BOARD * board,
int move,
S_MOVELIST * moveList )
static

Definition at line 65 of file move_generator.cpp.

References ASSERT, BLACK, S_MOVELIST::count, FROMSQ, isBoardStateValid(), S_MOVE::move, S_MOVELIST::moves, RANK_3, RANK_6, rankIndex120, S_BOARD::side, SqOnBoard(), TOSQ, and WHITE.

Referenced by generateAllCaptures(), and generateAllMoves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddQuietMove()

void AddQuietMove ( const S_BOARD * board,
int move,
S_MOVELIST * moveList )
static

Definition at line 34 of file move_generator.cpp.

References ASSERT, S_MOVELIST::count, FROMSQ, isBoardStateValid(), MAXDEPTH, S_MOVE::move, S_MOVELIST::moves, S_BOARD::pieces, S_BOARD::ply, S_BOARD::searchHistory, S_BOARD::searchKillers, SqOnBoard(), and TOSQ.

Referenced by AddBlackPawnMove(), AddWhitePawnMove(), and generateAllMoves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddWhitePawnCapMove()

void AddWhitePawnCapMove ( const S_BOARD * board,
int fromSq,
int toSq,
int captured,
S_MOVELIST * moveList )
static

Definition at line 78 of file move_generator.cpp.

References AddCaptureMove(), ASSERT, EMPTY, isBoardStateValid(), MOVE, pieceValueidEmpty(), RANK_7, rankIndex120, SqOnBoard(), wB, wN, wQ, and wR.

Referenced by generateAllCaptures(), and generateAllMoves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddWhitePawnMove()

void AddWhitePawnMove ( const S_BOARD * board,
int fromSq,
int toSq,
S_MOVELIST * moveList )
static

Definition at line 94 of file move_generator.cpp.

References AddQuietMove(), ASSERT, EMPTY, isBoardStateValid(), MOVE, RANK_7, rankIndex120, SqOnBoard(), wB, wN, wQ, and wR.

Referenced by generateAllMoves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateAllCaptures()

void generateAllCaptures ( const S_BOARD * pos,
S_MOVELIST * list )

Generate all pseudo-legal capture moves (including en passant).

Parameters
posPointer to the current board position.
listPointer to the move list to be filled.

Definition at line 299 of file move_generator.cpp.

References AddBlackPawnCapMove(), AddCaptureMove(), AddEnPassantMove(), AddWhitePawnCapMove(), ASSERT, BLACK, bP, S_MOVELIST::count, EMPTY, S_BOARD::enPas, isBoardStateValid(), LoopNonSlideIndex, LoopNonSlidePce, LoopSlideIndex, LoopSlidePce, MFLAGEP, MOVE, MoveListOk(), NO_SQ, numberDirections, S_BOARD::pceNum, pieceColor, pieceDirection, S_BOARD::pieces, pieceValueid(), S_BOARD::pList, S_BOARD::side, SQOFFBOARD, SqOnBoard(), WHITE, and wP.

Here is the call graph for this function:

◆ generateAllMoves()

void generateAllMoves ( const S_BOARD * pos,
S_MOVELIST * list )

Generate all pseudo-legal moves for the side to move.

Parameters
posPointer to the current board position.
listPointer to the move list to be filled.

Definition at line 165 of file move_generator.cpp.

References AddBlackPawnCapMove(), AddBlackPawnMove(), AddCaptureMove(), AddEnPassantMove(), AddQuietMove(), AddWhitePawnCapMove(), AddWhitePawnMove(), ASSERT, B1, B8, BKCA, BLACK, bP, BQCA, C1, C8, S_BOARD::castlePerm, S_MOVELIST::count, D1, D8, E1, E8, EMPTY, S_BOARD::enPas, F1, F8, G1, G8, isBoardStateValid(), isSquareAttacked(), LoopNonSlideIndex, LoopNonSlidePce, LoopSlideIndex, LoopSlidePce, MFLAGCA, MFLAGEP, MFLAGPS, MOVE, MoveListOk(), NO_SQ, numberDirections, S_BOARD::pceNum, pieceColor, pieceDirection, S_BOARD::pieces, pieceValueid(), S_BOARD::pList, RANK_2, RANK_7, rankIndex120, S_BOARD::side, SQOFFBOARD, SqOnBoard(), WHITE, WKCA, wP, and WQCA.

Referenced by LegalMovesFrom(), moveExists(), parseMoveString(), Perft(), and PerftTest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitMvvLva()

void InitMvvLva ( void )

Initialize the MVV-LVA capture scoring table.

Note
Call once at engine startup before generating moves.

Definition at line 141 of file move_generator.cpp.

References bK, MvvLvaScores, VictimScore, and wP.

Referenced by initializeEngine().

Here is the caller graph for this function:

◆ moveExists()

int moveExists ( S_BOARD * pos,
int move )

Check if a given move exists in the current position.

Parameters
posPointer to the current board position.
moveEncoded move integer.
Returns
Non-zero if the move exists; zero otherwise.

Definition at line 149 of file move_generator.cpp.

References S_MOVELIST::count, FALSE, generateAllMoves(), makeMove(), S_MOVE::move, S_MOVELIST::moves, takeMove(), and TRUE.

Here is the call graph for this function:

Variable Documentation

◆ LoopNonSlideIndex

const int LoopNonSlideIndex[2] = { 0, 3 }

Indices into LoopNonSlidePce for white and black.

Definition at line 10 of file move_generator.cpp.

Referenced by generateAllCaptures(), and generateAllMoves().

◆ LoopNonSlidePce

const int LoopNonSlidePce[6] = { wN, wK, 0, bN, bK, 0 }

Non-sliding piece loop order for move generation.

Definition at line 8 of file move_generator.cpp.

Referenced by generateAllCaptures(), and generateAllMoves().

◆ LoopSlideIndex

const int LoopSlideIndex[2] = { 0, 4 }

Indices into LoopSlidePce for white and black.

Definition at line 9 of file move_generator.cpp.

Referenced by generateAllCaptures(), and generateAllMoves().

◆ LoopSlidePce

const int LoopSlidePce[8] = { wB, wR, wQ, 0, bB, bR, bQ, 0 }

Sliding piece loop order for move generation.

Definition at line 7 of file move_generator.cpp.

Referenced by generateAllCaptures(), and generateAllMoves().

◆ MvvLvaScores

int MvvLvaScores[13][13]
static

Definition at line 31 of file move_generator.cpp.

Referenced by AddCaptureMove(), and InitMvvLva().

◆ numberDirections

const int numberDirections[13] = { 0, 0, 8, 4, 4, 8, 8, 0, 8, 4, 4, 8, 8 }

Number of valid directions for each piece type.

Definition at line 28 of file move_generator.cpp.

Referenced by generateAllCaptures(), and generateAllMoves().

◆ pieceDirection

const int pieceDirection[13][8]
Initial value:
= {
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ -8, -19, -21, -12, 8, 19, 21, 12 },
{ -9, -11, 11, 9, 0, 0, 0, 0 },
{ -1, -10, 1, 10, 0, 0, 0, 0 },
{ -1, -10, 1, 10, -9, -11, 11, 9 },
{ -1, -10, 1, 10, -9, -11, 11, 9 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ -8, -19, -21, -12, 8, 19, 21, 12 },
{ -9, -11, 11, 9, 0, 0, 0, 0 },
{ -1, -10, 1, 10, 0, 0, 0, 0 },
{ -1, -10, 1, 10, -9, -11, 11, 9 },
{ -1, -10, 1, 10, -9, -11, 11, 9 }
}

Direction offsets for each piece type.

Definition at line 12 of file move_generator.cpp.

Referenced by generateAllCaptures(), and generateAllMoves().

◆ VictimScore

const int VictimScore[13] = { 0,100,200,300,400,500,600,100,200,300,400,500,600 }

Victim scoring table for MVV-LVA.

Definition at line 30 of file move_generator.cpp.

Referenced by InitMvvLva().