Line data Source code
1 : //////////////////////////////////////////////////////////////////////
2 : //
3 : // FILE: movelist.h
4 : // MoveList class
5 : //
6 : // Part of: Scid (Shane's Chess Information Database)
7 : // Version: 3.4
8 : //
9 : // Notice: Copyright (c) 1999-2002 Shane Hudson. All rights reserved.
10 : // Copyright (c) 2016 Fulvio Benini. All rights reserved.
11 : //
12 : // Author: Shane Hudson (sgh@users.sourceforge.net)
13 : //
14 : //////////////////////////////////////////////////////////////////////
15 :
16 :
17 : #ifndef SCID_MOVELIST_H
18 : #define SCID_MOVELIST_H
19 :
20 : #include "common.h"
21 : #include <cstring>
22 : #include <iterator>
23 :
24 : //////////////////////////////////////////////////////////////////////
25 : // MoveList: Constants
26 :
27 : const uint MAX_LEGAL_MOVES = 256; // max. length of the moves list
28 :
29 :
30 : ///////////////////////////////////////////////////////////////////////////
31 : // MoveList: Data Structures
32 :
33 : // *** SimpleMove: less expensive to store than a full move as defined
34 : // in game.h, but still fully undoable.
35 : //
36 : struct simpleMoveT
37 : {
38 : squareT from;
39 : squareT to;
40 : pieceT promote;
41 : pieceT movingPiece;
42 : byte pieceNum;
43 : byte capturedNum;
44 : pieceT capturedPiece;
45 : squareT capturedSquare; // ONLY different to "to" field if this capture
46 : // is an en passant capture.
47 : byte castleFlags; // pre-move information
48 : squareT epSquare; // pre-move information
49 : ushort oldHalfMoveClock;
50 : int32_t score; // used for alpha/beta ordering.
51 :
52 15221005 : bool isNullMove() const {
53 15221005 : return from == to && from != NULL_SQUARE &&
54 15221005 : piece_Type(movingPiece) == KING;
55 : }
56 :
57 1023775 : int isCastle() const {
58 1023775 : ASSERT(piece_Type(movingPiece) == KING);
59 1023775 : if (square_Fyle(from) == E_FYLE) {
60 174493 : squareT toFyle = square_Fyle(to);
61 174493 : if (toFyle == G_FYLE)
62 920 : return 1;
63 173573 : if (toFyle == C_FYLE)
64 449 : return 2;
65 : }
66 1022406 : return 0;
67 : }
68 :
69 : bool operator<(const simpleMoveT& b) const {
70 : // Highest score first
71 : return score > b.score;
72 : }
73 :
74 4380983 : void clear() {
75 4380983 : std::memset(this, 0, sizeof *this);
76 4380983 : }
77 : };
78 :
79 : struct cmpMove {
80 : const simpleMoveT& m;
81 0 : explicit cmpMove(const simpleMoveT& sm) : m(sm) {}
82 : };
83 0 : inline bool operator==(const simpleMoveT& a, const cmpMove& b) {
84 0 : return a.from == b.m.from && a.to == b.m.to && a.promote == b.m.promote;
85 : }
86 :
87 : // typedef std::vector<simpleMoveT> MoveList;
88 214134 : class MoveList {
89 : uint ListSize = 0;
90 : simpleMoveT Moves[MAX_LEGAL_MOVES];
91 :
92 : public:
93 : typedef simpleMoveT* iterator;
94 0 : iterator begin() { return Moves; };
95 0 : iterator end() { return Moves + ListSize; }
96 1574815 : uint Size() { return ListSize; }
97 1047379 : void Clear() { ListSize = 0; }
98 17970512 : void emplace_back(squareT from, squareT to, pieceT promote,
99 : pieceT movingPiece, pieceT capturedPiece) {
100 17970512 : ASSERT(ListSize < MAX_LEGAL_MOVES);
101 17970512 : simpleMoveT& sm = Moves[ListSize++];
102 17970512 : sm.from = from;
103 17970512 : sm.to = to;
104 17970512 : sm.promote = promote;
105 17970512 : sm.movingPiece = movingPiece;
106 17970512 : sm.capturedPiece = capturedPiece;
107 17970512 : }
108 : void resize(size_t count) {
109 : ASSERT(count <= MAX_LEGAL_MOVES);
110 : ListSize = static_cast<uint>(count);
111 : }
112 : void push_back(const simpleMoveT& sm) {
113 : ASSERT(ListSize < MAX_LEGAL_MOVES);
114 : Moves[ListSize++] = sm;
115 : }
116 976323 : simpleMoveT* Get(size_t index) {
117 976323 : ASSERT(index < ListSize);
118 976323 : return &(Moves[index]);
119 : }
120 : };
121 :
122 : #endif // SCID_MOVELIST_H
|