LCOV - code coverage report
Current view: top level - src - fullmove.h (source / functions) Hit Total Coverage
Test: test_coverage.info Lines: 4 43 9.3 %
Date: 2017-06-21 14:32:49 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             : * Copyright (C) 2014  Fulvio Benini
       3             : 
       4             : * This file is part of Scid (Shane's Chess Information Database).
       5             : *
       6             : * Scid is free software: you can redistribute it and/or modify
       7             : * it under the terms of the GNU General Public License as published by
       8             : * the Free Software Foundation.
       9             : *
      10             : * Scid is distributed in the hope that it will be useful,
      11             : * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : * GNU General Public License for more details.
      14             : *
      15             : * You should have received a copy of the GNU General Public License
      16             : * along with Scid.  If not, see <http://www.gnu.org/licenses/>.
      17             : */
      18             : 
      19             : #ifndef FULLMOVE_H
      20             : #define FULLMOVE_H
      21             : 
      22             : #include "common.h"
      23             : #include <string>
      24             : 
      25             : class FullMove {
      26             :         // ** Lower 16 bits are compatible with Stockfish's Move **
      27             :         // bits  0- 5: destination square (from 0 to 63)
      28             :         // bits  6-11: origin square (from 0 to 63)
      29             :         // bits 12-13: promotion piece type -2 (from QUEEN-2 to KNIGHT-2)
      30             :         // bits 14-15: special move flag: promotion (1), en passant (2), castling (3)
      31             : 
      32             :         // ** Info for undoing the move **
      33             :         // bits 16-17: castling flags - TODO
      34             :         // bits 18-20: enpassant file - TODO
      35             :         // bits 21-23: captured pieceT
      36             : 
      37             :         // ** Info for direct SAN conversion **
      38             :         // bits 24-26: moving pieceT
      39             :         // bit     27: black to move
      40             :         // bit     28: ambiguous move, insert from fyle
      41             :         // bit     29: ambiguous move, insert from rank
      42             :         // bit     30: check
      43             : 
      44             :         // ** TODO: Use this flag to embed tags, variations, etc.. in a move stream
      45             :         // bit     31: special flag
      46             :         uint32_t m_;
      47             : 
      48             : public:
      49        1559 :         FullMove(uint32_t m = 0) : m_(m) {};
      50           0 :         bool operator!=(const FullMove& f) const { return m_ != f.m_; }
      51     2978898 :         bool    isNull()      const { return (m_ & 0xFFFF) == 0; }
      52           0 :         bool    isPromo()     const { return (m_ & (3 << 14)) == (1 << 14); }
      53             :         bool    isEnpassant() const { return (m_ & (3 << 14)) == (2 << 14); }
      54           0 :         bool    isCastle()    const { return (m_ & (3 << 14)) == (3 << 14); }
      55      280999 :         squareT getTo()       const { return m_ & 0x3F; }
      56     2967962 :         squareT getFrom()     const { return (m_ >> 6) & 0x3F; }
      57           0 :         pieceT  getPiece()    const { return (m_ >> 24) & 0x07; }
      58           0 :         colorT  getColor()    const { return (m_ >> 27 & 1) ? BLACK : WHITE; }
      59           0 :         pieceT  getPromo()    const { return ((m_ >> 12) & 0x03) +2; }
      60           0 :         pieceT  getCaptured() const { return (m_ >> 21) & 0x07; }
      61             :         squareT getCaptSq()   const {
      62             :                 squareT res = getTo();
      63             :                 if (isEnpassant()) res += (getColor() == WHITE) ? -8 : +8;
      64             :                 return res;
      65             :         }
      66           0 :         std::string getSAN(colorT* toMove = 0) const {
      67           0 :                 std::string res;
      68           0 :                 if (toMove) *toMove = getColor();
      69           0 :                 squareT to = getTo();
      70           0 :                 squareT from = getFrom();
      71           0 :                 if (to == 0 && from == 0) return "--";
      72           0 :                 if (isCastle()) return (to > from) ? "O-O" : "O-O-O";
      73           0 :                 bool fromFyle = (m_ >> 28) & 1;
      74           0 :                 bool fromRank = (m_ >> 29) & 1;
      75           0 :                 bool check    = (m_ >> 30) & 1;
      76           0 :                 bool capture  = (getCaptured() != 0);
      77             : 
      78           0 :                 switch (getPiece()) {
      79             :                 case BISHOP: res += "B"; break;
      80             :                 case KNIGHT: res += "N"; break;
      81             :                 case ROOK:   res += "R"; break;
      82             :                 case QUEEN:  res += "Q"; break;
      83             :                 case KING:   res += "K"; break;
      84             :                 default: //PAWN
      85           0 :                         if (capture) res += 'a' + (from % 8);
      86             :                 }
      87           0 :                 if (fromFyle) res += 'a' + (from % 8);
      88           0 :                 if (fromRank) res += '1' + (from / 8);
      89           0 :                 if (capture)  res += "x";
      90           0 :                 res += 'a' + (to % 8);
      91           0 :                 res += '1' + (to / 8);
      92           0 :                 if (isPromo()) {
      93           0 :                         switch (getPromo()) {
      94             :                         case BISHOP: res += "=B"; break;
      95             :                         case KNIGHT: res += "=N"; break;
      96             :                         case ROOK:   res += "=R"; break;
      97             :                         case QUEEN:  res += "=Q"; break;
      98             :                         }
      99             :                 }
     100           0 :                 if (check) res += "+";
     101             :                 return res;
     102             :         }
     103             : 
     104             :         void reset(colorT c, pieceT p, squareT from, squareT to, pieceT promo = 0) {
     105           0 :                 m_ = to | (from << 6) | (p << 24) | (c << 27);
     106           0 :                 if (promo > 2) m_ |= ((promo -2) << 12) | (1 << 14);
     107             :         }
     108             :         void resetCastle(colorT c, squareT kingSq, squareT rookSq) {
     109             :                 //Encoding as king to rook allow undoing of chess360 moves
     110           0 :                 m_ = rookSq | (kingSq << 6) | (3 << 14) | (KING << 24) | (c << 27);
     111             :         }
     112             :         void setCapture(pieceT piece, bool enPassant) {
     113           0 :                 m_ |= ((piece & 0x07) << 21);
     114           0 :                 if (enPassant) m_ |= (2 << 14);
     115             :         }
     116             :         void setAmbiguous(const FullMove& move2) {
     117           0 :                 if ((m_ & 0x700003F) == (move2.m_ & 0x700003F)) {
     118           0 :                         int from = getFrom();
     119           0 :                         int from2 = move2.getFrom();
     120           0 :                         if ((from % 8) != (from2 % 8)) m_ |= (1 << 28);
     121           0 :                         else m_ |= (1 << 29);
     122             :                 }
     123             :         }
     124           0 :         void setCheck() { m_ |= (1 << 30); }
     125             : };
     126             : 
     127             : #endif

Generated by: LCOV version 1.12