Scid  4.6.5
position.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////
2 //
3 // FILE: position.h
4 // Position class
5 //
6 // Part of: Scid (Shane's Chess Information Database)
7 // Version: 3.5
8 //
9 // Notice: Copyright (c) 1999-2003 Shane Hudson. All rights reserved.
10 //
11 // Author: Shane Hudson (sgh@users.sourceforge.net)
12 //
13 //////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef SCID_POSITION_H
17 #define SCID_POSITION_H
18 
19 #include "common.h"
20 #include "movelist.h"
21 #include "tokens.h"
22 #include <stdio.h>
23 
24 class DString;
25 class SquareSet;
26 class SquareList;
27 
28 
29 //////////////////////////////////////////////////////////////////////
30 // Position: Constants
31 
32 const byte WQ_CASTLE = 1, WK_CASTLE = 2,
33  BQ_CASTLE = 4, BK_CASTLE = 8;
34 
35 // SANFlag: since checking if a move is check (to add the "+" to its
36 // SAN string) takes time, and checking for mate takes even
37 // longer, we specify whether we want this done with a flag.
38 typedef byte sanFlagT;
42 
43 
44 // Flags that Position::PrintFEN() recognises:
45 //
46 const uint
48  FEN_BOARD = 1,
51 
52 
53 // Flags that Position::GenerateMoves() recognises:
54 //
55 typedef uint genMovesT;
56 const genMovesT
60 
61 
62 // SANList: list of legal move strings in SAN.
63 //
64 struct sanListT
65 {
66  bool current;
69 };
70 
71 
72 ///////////////////////////////////////////////////////////////////////////
73 // Position: Class definition
74 
75 class Position
76 {
77 
78 private:
79  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80  // Position: Data structures
81 
82  pieceT Board[66]; // the actual board + a color square
83  // and a NULL square.
84  uint Count[2]; // count of pieces & pawns each
85  byte Material[16]; // count of each type of piece
86  byte ListPos[64]; // ListPos stores the position in
87  // List[][] for the piece on
88  // square x.
89  squareT List[2][16]; // list of piece squares for each side
90  byte NumOnRank[16][8];
91  byte NumOnFyle[16][8];
92  byte NumOnLeftDiag[16][16]; // Num Queens/Bishops
93  byte NumOnRightDiag[16][16];
94  byte NumOnSquareColor[16][2];
95 
96  directionT Pinned[16]; // For each List[ToMove][x], stores
97  // whether piece is pinned to its
98  // own king and dir from king.
99 
100  squareT EPTarget; // square pawns can EP capture to
101  colorT ToMove;
102  ushort HalfMoveClock; // Count of halfmoves since last capture
103  // or pawn move.
104  ushort PlyCounter;
105  byte Castling; // castling flags
106  bool StrictCastling; // If false, allow castling after moving
107  // the King or Rook.
108 
109  uint Hash; // Hash value.
110  uint PawnHash; // Pawn structure hash value.
111 
112  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113  // Position: Private Functions
114 
115  inline void AddHash (pieceT p, squareT sq);
116  inline void UnHash (pieceT p, squareT sq);
117 
118  inline void AddToBoard (pieceT p, squareT sq);
119  inline void RemoveFromBoard (pieceT p, squareT sq);
120 
121  void CalcPinsDir (directionT dir, pieceT attacker);
122 
123  void GenSliderMoves (MoveList * mlist, colorT c, squareT sq,
124  directionT dir, SquareSet * sqset,
125  bool capturesOnly);
126  void GenKnightMoves (MoveList * mlist, colorT c, squareT sq,
127  SquareSet * sqset, bool capturesOnly);
128 
129  void AddLegalMove (MoveList * mlist, squareT from, squareT to, pieceT promo);
130  void GenCastling (MoveList * mlist);
131  void GenKingMoves (MoveList * mlist, genMovesT genType, bool castling);
132  void AddPromotions (MoveList * mlist, squareT from, squareT dest);
133  bool IsValidEnPassant (squareT from, squareT to);
134  void GenPawnMoves (MoveList * mlist, squareT from, directionT dir,
135  SquareSet * sqset, genMovesT genType);
136  errorT AssertPos (); // Checks for errors in board etc.
137 
138  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139  // Position: Public Functions
140 public:
141  Position();
142  static const Position& getStdStart();
143 
144  void Clear(); // No pieces on board
145  void StdStart() { *this = getStdStart(); }
146  bool IsStdStart();
147  errorT AddPiece (pieceT p, squareT sq);
148 
149  // Set and Get attributes -- one-liners
150  byte PieceCount (pieceT p) { return Material[p]; }
151  byte * GetMaterial () { return Material; }
152  void SetEPTarget (squareT s) { EPTarget = s; }
153  squareT GetEPTarget () { return EPTarget; }
154  void SetToMove (colorT c) { ToMove = c; }
155  colorT GetToMove () { return ToMove; }
156  void SetPlyCounter (ushort x) { PlyCounter = x; }
157  ushort GetPlyCounter () { return PlyCounter; }
158  ushort GetFullMoveCount () { return PlyCounter / 2 + 1; }
159 
160  // Methods to get the Board or piece lists -- used in game.cpp to
161  // decode moves:
162  squareT * GetList (colorT c) { return List[c]; }
163  uint GetCount (colorT c) { return Count[c]; }
164  uint TotalMaterial () { return Count[WHITE] + Count[BLACK]; }
166  return Count[c] - Material[piece_Make(c,PAWN)];
167  }
168  bool InPawnEnding () {
169  return (NumNonPawns(WHITE) == 1 && NumNonPawns(BLACK) == 1);
170  }
171  uint MaterialValue (colorT c);
172  inline uint FyleCount (pieceT p, fyleT f) {
173  return NumOnFyle[p][f];
174  }
175  inline uint RankCount (pieceT p, rankT r) {
176  return NumOnRank[p][r];
177  }
178  inline uint LeftDiagCount (pieceT p, leftDiagT diag) {
179  return NumOnLeftDiag[p][diag];
180  }
182  return NumOnRightDiag[p][diag];
183  }
184  inline uint SquareColorCount (pieceT p, colorT sqColor) {
185  return NumOnSquareColor[p][sqColor];
186  }
187  uint GetSquares (pieceT p, SquareList * sqlist);
188 
189  const pieceT* GetBoard() const {
190  const_cast<Position*>(this)->Board[COLOR_SQUARE] = COLOR_CHAR[ToMove];
191  return Board;
192  }
193 
194  // Other one-line methods
195  squareT GetKingSquare (colorT c) { return List[c][0]; }
196  squareT GetKingSquare () { return List[ToMove][0]; }
197  squareT GetEnemyKingSquare () { return List[1-ToMove][0]; }
198 
199  // Castling flags
200  inline void SetCastling (colorT c, castleDirT dir, bool flag);
201  bool GetCastling (colorT c, castleDirT dir);
202  inline bool CastlingPossible () { return (Castling ? true : false); }
203  byte GetCastlingFlags () { return Castling; }
204  void SetCastlingFlags (byte b) { Castling = b; }
205 
206  void SetStrictCastling (bool b) { StrictCastling = b; }
207  bool GetStrictCastling (void) { return StrictCastling; }
208 
209  // Hashing
210  inline uint HashValue (void) { return Hash; }
211  inline uint PawnHashValue (void) { return PawnHash; }
212  uint GetHPSig ();
213 
214  // Move generation and execution
215  void CalcPins();
216  void GenPieceMoves (MoveList * mlist, squareT sq,
217  SquareSet * sqset, bool capturesOnly);
218 
219  // Generate all legal moves:
220  void GenerateMoves (MoveList* mlist, pieceT mask, genMovesT genType, bool maybeInCheck);
221  void GenerateMoves (MoveList * mlist) { GenerateMoves (mlist, EMPTY, GEN_ALL_MOVES, true); }
222  void GenerateMoves (MoveList * mlist, genMovesT genType) { GenerateMoves (mlist, EMPTY, genType, true); }
223  void GenerateCaptures (MoveList * mlist) { GenerateMoves (mlist, EMPTY, GEN_CAPTURES, true); }
224  bool IsLegalMove (simpleMoveT * sm);
225 
226  void GenCheckEvasions (MoveList * mlist, pieceT mask, genMovesT genType, SquareList * checkSquares);
227  void MatchLegalMove (MoveList * mlist, pieceT mask, squareT target);
228  errorT MatchPawnMove (MoveList * mlist, fyleT fromFyle, squareT to, pieceT promote);
229  errorT MatchKingMove (MoveList * mlist, squareT target);
230 
231  uint CalcAttacks (colorT toMove, squareT kingSq, SquareList * squares);
232  int TreeCalcAttacks (colorT toMove, squareT target);
234  return CalcAttacks (1-ToMove, GetKingSquare(), NULL);
235  }
237  return CalcAttacks (1-ToMove, kingSq, NULL);
238  }
239  uint CalcNumChecks (squareT kingSq, SquareList * checkSquares) {
240  return CalcAttacks (1-ToMove, kingSq, checkSquares);
241  }
242 
243  uint Mobility (pieceT p, colorT color, squareT from);
244  bool IsKingInCheck () { return (CalcNumChecks() > 0); }
245  bool IsKingInCheckDir (directionT dir);
246  bool IsKingInCheck (simpleMoveT * sm);
247  bool IsKingInMate ();
248  bool IsLegal ();
249 
250  bool IsPromoMove (squareT from, squareT to);
251 
252  void DoSimpleMove (simpleMoveT * sm); // move execution ...
253  void UndoSimpleMove (simpleMoveT * sm); // ... and taking back
254 
255  errorT RelocatePiece (squareT fromSq, squareT toSq);
256 
257  void MakeSANString (simpleMoveT * sm, char * s, sanFlagT flag);
258  void MakeUCIString (simpleMoveT * sm, char * s);
259  void CalcSANStrings (sanListT *sanList, sanFlagT flag);
260 
261  errorT ReadCoordMove (simpleMoveT * m, const char * s, bool reverse);
262  errorT ReadMove (simpleMoveT * m, const char * s, tokenT t);
263  errorT ParseMove (simpleMoveT * sm, const char * s);
264  errorT ReadLine (const char * s);
265 
266  // Board I/O
267  void MakeLongStr (char * str);
268  void DumpBoard (FILE * fp);
269  void DumpLists (FILE * fp);
270  errorT ReadFromLongStr (const char * str);
271  errorT ReadFromCompactStr (const byte * str);
272  errorT ReadFromFEN (const char * s);
273  void PrintCompactStr (char * cboard);
274  void PrintCompactStrFlipped (char * cboard);
276  return (Board[0] << 4) | Board[1];
277  }
278  void PrintFEN (char * str, uint flags);
279  void DumpLatexBoard (DString * dstr, bool flip);
280  void DumpLatexBoard (DString * dstr) {
281  DumpLatexBoard (dstr, false);
282  }
283  void DumpHtmlBoard (DString * dstr, uint style, const char * dir,
284  bool flip);
285  void DumpHtmlBoard (DString * dstr, uint style, const char * dir) {
286  DumpHtmlBoard (dstr, style, dir, false);
287  }
288 
289  // Copy, compare positions
290  int Compare (Position * p);
291  void CopyFrom (Position * src) { *this = *src; }
292 
293  // Set up a random position:
294  errorT Random (const char * material);
295 };
296 
297 
298 
299 //////////////////////////////////////////////////////////////////////
300 // Position: Public Inline Functions
301 
302 
303 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
304 // Position::SetCastling():
305 // Set a castling flag.
306 //
307 inline void
309 {
310  byte b = (c==WHITE ? 1 : 4);
311  if (dir == KSIDE) b += b;
312  // Now b = 1 or 2 (white flags), or 4 or 8 (black flags)
313  if (flag) { Castling |= b; } else { Castling &= (255-b); }
314  return;
315 }
316 
317 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
318 // Position::GetCastling():
319 // Get a castling flag.
320 //
321 inline bool
323 {
324  byte b = (c==WHITE ? 1 : 4);
325  if (dir == KSIDE) b += b;
326  // Now b == 1 or 2 (white flags), or 4 or 8 (black flags)
327  if (Castling & b) { return true; } else { return false; }
328 }
329 
330 #endif // SCID_POSITION_H
331 
332 //////////////////////////////////////////////////////////////////////
333 // EOF: position.h
334 //////////////////////////////////////////////////////////////////////
335 
void GenerateCaptures(MoveList *mlist)
Definition: position.h:223
unsigned char byte
Definition: common.h:97
void SetCastlingFlags(byte b)
Definition: position.h:204
void SetToMove(colorT c)
Definition: position.h:154
const colorT WHITE
Definition: common.h:203
uint GetCount(colorT c)
Definition: position.h:163
byte sanFlagT
Definition: position.h:38
sqsqname
Definition: board.tcl:292
const byte BQ_CASTLE
Definition: position.h:33
const castleDirT KSIDE
Definition: common.h:215
flipw?newstate?
Definition: board.tcl:1510
bool IsKingInCheck()
Definition: position.h:244
byte rankT
Definition: common.h:115
byte GetCastlingFlags()
Definition: position.h:203
promopiece
Definition: calvar.tcl:255
const byte WQ_CASTLE
Definition: position.h:32
void GenerateMoves(MoveList *mlist)
Definition: position.h:221
uint RightDiagCount(pieceT p, rightDiagT diag)
Definition: position.h:181
squareT GetKingSquare(colorT c)
Definition: position.h:195
uint PawnHashValue(void)
Definition: position.h:211
const colorT BLACK
Definition: common.h:204
byte fyleT
Definition: common.h:116
byte * GetMaterial()
Definition: position.h:151
const uint MAX_LEGAL_MOVES
Definition: movelist.h:26
bool CastlingPossible()
Definition: position.h:202
uint CalcNumChecks(squareT kingSq, SquareList *checkSquares)
Definition: position.h:239
const byte WK_CASTLE
Definition: position.h:32
void CopyFrom(Position *src)
Definition: position.h:291
uint TotalMaterial()
Definition: position.h:164
uint NumNonPawns(colorT c)
Definition: position.h:165
byte PieceCount(pieceT p)
Definition: position.h:150
colorpct?col?
Definition: ptracker.tcl:77
uint FyleCount(pieceT p, fyleT f)
Definition: position.h:172
void SetStrictCastling(bool b)
Definition: position.h:206
void SetCastling(colorT c, castleDirT dir, bool flag)
Definition: position.h:308
uint CalcNumChecks()
Definition: position.h:233
const pieceT EMPTY
Definition: common.h:234
uint genMovesT
Definition: position.h:55
bool GetStrictCastling(void)
Definition: position.h:207
uint32_t uint
Definition: common.h:99
uint tokenT
Definition: tokens.h:23
void ReadLine(DString *s, MFile *fp)
Definition: pbook.cpp:529
const uint FEN_BOARD
Definition: position.h:48
void DumpHtmlBoard(DString *dstr, uint style, const char *dir)
Definition: position.h:285
uint LeftDiagCount(pieceT p, leftDiagT diag)
Definition: position.h:178
void StdStart()
Definition: position.h:145
squareT * GetList(colorT c)
Definition: position.h:162
byte rightDiagT
Definition: common.h:118
const uint FEN_COMPACT
Definition: position.h:47
const pieceT PAWN
Definition: common.h:226
byte CompactStrFirstByte()
Definition: position.h:275
uint HashValue(void)
Definition: position.h:210
Clear
Definition: game.tcl:8
unsigned short errorT
Definition: error.h:20
void SetEPTarget(squareT s)
Definition: position.h:152
const sanFlagT SAN_NO_CHECKTEST
Definition: position.h:39
const sanFlagT SAN_CHECKTEST
Definition: position.h:40
void SetPlyCounter(ushort x)
Definition: position.h:156
const char COLOR_CHAR[3]
Definition: common.h:207
const genMovesT GEN_CAPTURES
Definition: position.h:57
uint RankCount(pieceT p, rankT r)
Definition: position.h:175
const genMovesT GEN_ALL_MOVES
Definition: position.h:59
uint16_t ushort
Definition: common.h:98
uint SquareColorCount(pieceT p, colorT sqColor)
Definition: position.h:184
const pieceT * GetBoard() const
Definition: position.h:189
materialw
Definition: board.tcl:1551
ushort num
Definition: position.h:67
byte colorT
Definition: common.h:112
char sanStringT[10]
Definition: common.h:137
ushort GetPlyCounter()
Definition: position.h:157
colorT GetToMove()
Definition: position.h:155
bool current
Definition: position.h:66
bool InPawnEnding()
Definition: position.h:168
const genMovesT GEN_NON_CAPS
Definition: position.h:58
byte directionT
Definition: common.h:114
byte leftDiagT
Definition: common.h:117
void GenerateMoves(MoveList *mlist, genMovesT genType)
Definition: position.h:222
squareT GetEnemyKingSquare()
Definition: position.h:197
const squareT COLOR_SQUARE
Definition: common.h:351
uint CalcNumChecks(squareT kingSq)
Definition: position.h:236
ushort GetFullMoveCount()
Definition: position.h:158
squareT GetEPTarget()
Definition: position.h:153
void DumpLatexBoard(DString *dstr)
Definition: position.h:280
bool GetCastling(colorT c, castleDirT dir)
Definition: position.h:322
pieceT piece_Make(colorT c, pieceT p)
Definition: common.h:290
sanStringT list[MAX_LEGAL_MOVES]
Definition: position.h:68
const uint FEN_CASTLING_EP
Definition: position.h:49
squareT GetKingSquare()
Definition: position.h:196
const byte BK_CASTLE
Definition: position.h:33
byte squareT
Definition: common.h:113
const sanFlagT SAN_MATETEST
Definition: position.h:41
byte pieceT
Definition: common.h:111
byte castleDirT
Definition: common.h:129
const uint FEN_ALL_FIELDS
Definition: position.h:50