libscid 0.1.0
Chess applications made easy.
Loading...
Searching...
No Matches
movelist.h
Go to the documentation of this file.
1
4#pragma once
5
6#include "scid/core/board.h"
7
8#include <cassert>
9#include <cstddef>
10#include <cstdint>
11
13// MoveList: Constants
14
15namespace scid::core {
16
17const uint MAX_LEGAL_MOVES = 256; // max. length of the moves list
18
19
21// MoveList: Data Structures
22
23// TODO [MoveAction]: This is a position-resolved reversible action record.
24// It semantically belongs with Position, but currently lives here because
25// MoveList stores generated actions and Position includes MoveList.
27{
28 squareT from;
29 squareT to;
30 pieceT promote; // EMPTY if not a promotion, type (no color) otherwise
31 pieceT movingPiece : 7;
32 pieceT castling : 1;
33 byte pieceNum;
34 byte capturedNum;
35 pieceT capturedPiece;
36 squareT capturedSquare; // ONLY different to "to" field if this capture
37 // is an en passant capture.
38 byte castleFlags; // pre-move information
39 squareT epSquare; // pre-move information
40 ushort oldHalfMoveClock;
41
42 bool isNullMove() const {
43 return from == to && from != NULL_SQUARE &&
44 piece_Type(movingPiece) == KING;
45 }
46
51 int isCastle() const {
52 if (castling)
53 return to > from ? 2 : -2;
54
55 return 0;
56 }
57
60 template <typename OutputIt> OutputIt toLongNotation(OutputIt dest) const {
61 if (from == to) {
62 // UCI standard for null move
63 *dest++ = '0';
64 *dest++ = '0';
65 *dest++ = '0';
66 *dest++ = '0';
67 } else {
68 *dest++ = square_FyleChar(from);
69 *dest++ = square_RankChar(from);
70 *dest++ = square_FyleChar(to);
71 *dest++ = square_RankChar(to);
72 if (promote != EMPTY) {
73 constexpr const char promoChars[] = " qrbn ";
74 *dest++ = promoChars[piece_Type(promote)];
75 }
76 }
77 return dest;
78 }
79};
80
81struct ScoredMove : public MoveAction {
82 std::int32_t score; // used for alpha/beta ordering.
83
84 bool operator<(const ScoredMove& b) const {
85 // Highest score first
86 return score > b.score;
87 }
88};
89
90// typedef std::vector<MoveAction> MoveList;
91class MoveList {
92 uint ListSize = 0;
93 ScoredMove Moves[MAX_LEGAL_MOVES];
94
95public:
96 typedef ScoredMove* iterator;
97 iterator begin() { return Moves; };
98 iterator end() { return Moves + ListSize; }
99 uint Size() { return ListSize; }
100 void Clear() { ListSize = 0; }
101 ScoredMove& emplace_back() {
102 assert(ListSize < MAX_LEGAL_MOVES);
103 ScoredMove& sm = Moves[ListSize++];
104 sm = ScoredMove();
105 return sm;
106 }
107 void resize(std::size_t count) {
108 assert(count <= MAX_LEGAL_MOVES);
109 ListSize = static_cast<uint>(count);
110 }
111 void push_back(const ScoredMove& sm) {
112 assert(ListSize < MAX_LEGAL_MOVES);
113 Moves[ListSize++] = sm;
114 }
115 ScoredMove* Get(std::size_t index) {
116 assert(index < ListSize);
117 return &(Moves[index]);
118 }
119};
120
121
122} // namespace scid::core
Chess board constants, piece helpers, square geometry, and directions.
Definition movelist.h:91
Definition movelist.h:27
int isCastle() const
Returns: +2 for king side castle -2 for queen side castle 0 (false) if it is not a castle moves.
Definition movelist.h:51
OutputIt toLongNotation(OutputIt dest) const
Converts the move to UCI coordinate notation.
Definition movelist.h:60
Definition movelist.h:81