Scid  4.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
movelist.h
Go to the documentation of this file.
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 //
37 {
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
50  int32_t score; // used for alpha/beta ordering.
51 
52  bool isNullMove() const {
53  return from == to && from != NULL_SQUARE &&
54  piece_Type(movingPiece) == KING;
55  }
56 
57  int isCastle() const {
58  ASSERT(piece_Type(movingPiece) == KING);
59  if (square_Fyle(from) == E_FYLE) {
60  squareT toFyle = square_Fyle(to);
61  if (toFyle == G_FYLE)
62  return 1;
63  if (toFyle == C_FYLE)
64  return 2;
65  }
66  return 0;
67  }
68 
69  bool operator<(const simpleMoveT& b) const {
70  // Highest score first
71  return score > b.score;
72  }
73 
74  void clear() {
75  std::memset(this, 0, sizeof *this);
76  }
77 };
78 
79 struct cmpMove {
80  const simpleMoveT& m;
81  explicit cmpMove(const simpleMoveT& sm) : m(sm) {}
82 };
83 inline bool operator==(const simpleMoveT& a, const cmpMove& b) {
84  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 class MoveList {
89  uint ListSize = 0;
91 
92 public:
94  iterator begin() { return Moves; };
95  iterator end() { return Moves + ListSize; }
96  uint Size() { return ListSize; }
97  void Clear() { ListSize = 0; }
100  ASSERT(ListSize < MAX_LEGAL_MOVES);
101  simpleMoveT& sm = Moves[ListSize++];
102  sm.from = from;
103  sm.to = to;
104  sm.promote = promote;
107  }
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  simpleMoveT* Get(size_t index) {
117  ASSERT(index < ListSize);
118  return &(Moves[index]);
119  }
120 };
121 
122 #endif // SCID_MOVELIST_H
unsigned char byte
Definition: common.h:89
squareT capturedSquare
Definition: movelist.h:45
pieceT piece_Type(pieceT p)
Definition: common.h:292
iterator end()
Definition: movelist.h:95
cmpMove(const simpleMoveT &sm)
Definition: movelist.h:81
simpleMoveT * Get(size_t index)
Definition: movelist.h:116
const fyleT G_FYLE
Definition: common.h:366
const squareT NULL_SQUARE
Definition: common.h:357
#define ASSERT(f)
Definition: common.h:59
const pieceT KING
Definition: common.h:226
byte capturedNum
Definition: movelist.h:43
byte castleFlags
Definition: movelist.h:47
uint Size()
Definition: movelist.h:96
const uint MAX_LEGAL_MOVES
Definition: movelist.h:27
void resize(size_t count)
Definition: movelist.h:108
const fyleT C_FYLE
Definition: common.h:365
simpleMoveT * iterator
Definition: movelist.h:93
iterator begin()
Definition: movelist.h:94
ushort oldHalfMoveClock
Definition: movelist.h:49
const fyleT E_FYLE
Definition: common.h:365
uint32_t uint
Definition: common.h:91
void Clear()
Definition: movelist.h:97
bool isNullMove() const
Definition: movelist.h:52
bool operator<(const simpleMoveT &b) const
Definition: movelist.h:69
bool operator==(const simpleMoveT &a, const cmpMove &b)
Definition: movelist.h:83
squareT to
Definition: movelist.h:39
byte pieceNum
Definition: movelist.h:42
uint16_t ushort
Definition: common.h:90
squareT from
Definition: movelist.h:38
pieceT capturedPiece
Definition: movelist.h:44
int32_t score
Definition: movelist.h:50
squareT epSquare
Definition: movelist.h:48
pieceT movingPiece
Definition: movelist.h:41
void push_back(const simpleMoveT &sm)
Definition: movelist.h:112
const simpleMoveT & m
Definition: movelist.h:80
pieceT promote
Definition: movelist.h:40
byte squareT
Definition: common.h:105
void clear()
Definition: movelist.h:74
void emplace_back(squareT from, squareT to, pieceT promote, pieceT movingPiece, pieceT capturedPiece)
Definition: movelist.h:98
byte pieceT
Definition: common.h:103
int isCastle() const
Definition: movelist.h:57
fyleT square_Fyle(squareT sq)
Definition: common.h:384