LCOV - code coverage report
Current view: top level - src - position.h (source / functions) Hit Total Coverage
Test: test_coverage.info Lines: 26 33 78.8 %
Date: 2017-06-21 14:32:49 Functions: 1 1 100.0 %

          Line data    Source code
       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;
      39             : const sanFlagT    SAN_NO_CHECKTEST   = 0,
      40             :                   SAN_CHECKTEST      = 1,
      41             :                   SAN_MATETEST       = 2;
      42             : 
      43             : 
      44             : // Flags that Position::PrintFEN() recognises:
      45             : //
      46             : const uint
      47             :     FEN_COMPACT = 0,
      48             :     FEN_BOARD = 1,
      49             :     FEN_CASTLING_EP = 2,
      50             :     FEN_ALL_FIELDS = 3;
      51             : 
      52             : 
      53             : // Flags that Position::GenerateMoves() recognises:
      54             : //
      55             : typedef uint genMovesT;
      56             : const genMovesT
      57             :     GEN_CAPTURES = 1,
      58             :     GEN_NON_CAPS = 2,
      59             :     GEN_ALL_MOVES = (GEN_CAPTURES | GEN_NON_CAPS);
      60             : 
      61             : 
      62             : // SANList: list of legal move strings in SAN.
      63             : //
      64             : struct sanListT
      65             : {
      66             :     bool        current;
      67             :     ushort      num;
      68             :     sanStringT  list [MAX_LEGAL_MOVES];
      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       19079 :     void        StdStart() { *this = getStdStart(); }
     146             :     bool        IsStdStart();
     147             :     errorT      AddPiece (pieceT p, squareT sq);
     148             : 
     149             :     // Set and Get attributes -- one-liners
     150           0 :     byte        PieceCount (pieceT p)    { return Material[p]; }
     151      949126 :     byte *      GetMaterial ()           { return Material; }
     152             :     void        SetEPTarget (squareT s)  { EPTarget = s; }
     153             :     squareT     GetEPTarget ()           { return EPTarget; }
     154           0 :     void        SetToMove (colorT c)     { ToMove = c; }
     155             :     colorT      GetToMove ()             { return ToMove; }
     156             :     void        SetPlyCounter (ushort x) { PlyCounter = x; }
     157             :     ushort      GetPlyCounter ()         { return PlyCounter; }
     158     1153608 :     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      780177 :     squareT *   GetList (colorT c)    { return List[c]; }
     163           0 :     uint        GetCount (colorT c)   { return Count[c]; }
     164             :     uint        TotalMaterial ()      { return Count[WHITE] + Count[BLACK]; }
     165             :     uint        NumNonPawns (colorT c) { 
     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    14264544 :         return NumOnFyle[p][f];
     174             :     }
     175             :     inline uint RankCount (pieceT p, rankT r) {
     176    14264544 :         return NumOnRank[p][r];
     177             :     }
     178             :     inline uint LeftDiagCount (pieceT p, leftDiagT diag) {
     179    14378784 :         return NumOnLeftDiag[p][diag];
     180             :     }
     181             :     inline uint RightDiagCount (pieceT p, rightDiagT diag) {
     182    14378784 :         return NumOnRightDiag[p][diag];
     183             :     }
     184             :     inline uint SquareColorCount (pieceT p, colorT sqColor) {
     185     5593409 :         return NumOnSquareColor[p][sqColor];
     186             :     }
     187             :     uint        GetSquares (pieceT p, SquareList * sqlist);
     188             : 
     189             :     const pieceT* GetBoard() const {
     190      780177 :         const_cast<Position*>(this)->Board[COLOR_SQUARE] = COLOR_CHAR[ToMove];
     191      780177 :         return Board;
     192             :     }
     193             : 
     194             :     // Other one-line methods
     195     4699866 :     squareT     GetKingSquare (colorT c)  { return List[c][0]; }
     196     3354600 :     squareT     GetKingSquare ()          { return List[ToMove][0]; }
     197      992842 :     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           0 :     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       77857 :     void  GenerateMoves (MoveList * mlist) { GenerateMoves (mlist, EMPTY, GEN_ALL_MOVES, true); }
     222             :     void  GenerateMoves (MoveList * mlist, genMovesT genType) { GenerateMoves (mlist, EMPTY, genType, true); }
     223           0 :     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);
     233             :     uint        CalcNumChecks () {
     234      742366 :                     return CalcAttacks (1-ToMove, GetKingSquare(), NULL);
     235             :                 }
     236             :     uint        CalcNumChecks (squareT kingSq) {
     237     7989791 :                     return CalcAttacks (1-ToMove, kingSq, NULL);
     238             :                 }
     239             :     uint        CalcNumChecks (squareT kingSq, SquareList * checkSquares) {
     240      857196 :                     return CalcAttacks (1-ToMove, kingSq, checkSquares);
     241             :                 }
     242             : 
     243             :     uint        Mobility (pieceT p, colorT color, squareT from);
     244      742366 :     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);
     275             :     byte        CompactStrFirstByte () {
     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           0 :         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           0 :         DumpHtmlBoard (dstr, style, dir, false);
     287             :     }
     288             : 
     289             :     // Copy, compare positions
     290             :     int         Compare (Position * p);
     291        4000 :     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
     308             : Position::SetCastling (colorT c, castleDirT dir, bool flag)
     309             : {
     310      278186 :     byte b = (c==WHITE ? 1 : 4);
     311      143029 :     if (dir == KSIDE) b += b;
     312             :     // Now b = 1 or 2 (white flags), or 4 or 8 (black flags)
     313      278183 :     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
     322             : Position::GetCastling (colorT c, castleDirT dir)
     323             : {
     324      275750 :     byte b = (c==WHITE ? 1 : 4);
     325      137953 :     if (dir == KSIDE) b += b;
     326             :     // Now b == 1 or 2 (white flags), or 4 or 8 (black flags)
     327      275750 :     if (Castling & b) { return true; } else { return false; }
     328             : }
     329             : 
     330             : #endif  // SCID_POSITION_H
     331             : 
     332             : //////////////////////////////////////////////////////////////////////
     333             : //  EOF: position.h
     334             : //////////////////////////////////////////////////////////////////////
     335             : 

Generated by: LCOV version 1.12