Scid  4.7.0
fullmove.h
Go to the documentation of this file.
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  constexpr FullMove(uint32_t m = 0) : m_(m){};
50 
51  FullMove(colorT c, squareT kingSq, squareT rookSq)
52  // Castle: encoding as king to rook allows the undoing of chess360 moves
53  : FullMove(c, kingSq, rookSq, KING) {
54  m_ |= (3 << 14);
55  }
56 
57  FullMove(colorT c, squareT from, squareT to, pieceT pt) {
58  m_ = to | (from << 6) | (pt << 24) | (c << 27);
59  }
60 
61  operator bool() const { return m_ != 0; }
62  bool operator!=(const FullMove& f) const { return m_ != f.m_; }
63  bool isPromo() const { return (m_ & (3 << 14)) == (1 << 14); }
64  bool isEnpassant() const { return (m_ & (3 << 14)) == (2 << 14); }
65  bool isCastle() const { return (m_ & (3 << 14)) == (3 << 14); }
66  squareT getTo() const { return m_ & 0x3F; }
67  squareT getFrom() const { return (m_ >> 6) & 0x3F; }
68  pieceT getPiece() const { return (m_ >> 24) & 0x07; }
69  colorT getColor() const { return (m_ >> 27 & 1) ? BLACK : WHITE; }
70  pieceT getPromo() const { return ((m_ >> 12) & 0x03) +2; }
71  pieceT getCaptured() const { return (m_ >> 21) & 0x07; }
72  std::string getSAN(colorT* toMove = 0) const {
73  std::string res;
74  if (toMove) *toMove = getColor();
75  squareT to = getTo();
76  squareT from = getFrom();
77  if (to == 0 && from == 0) return "--";
78  if (isCastle()) {
79  res = (to > from) ? "O-O" : "O-O-O";
80  bool check = (m_ >> 30) & 1;
81  if (check)
82  res += "+";
83  return res;
84  }
85  bool fromFyle = (m_ >> 28) & 1;
86  bool fromRank = (m_ >> 29) & 1;
87  bool check = (m_ >> 30) & 1;
88  bool capture = (getCaptured() != 0);
89 
90  switch (getPiece()) {
91  case BISHOP: res += "B"; break;
92  case KNIGHT: res += "N"; break;
93  case ROOK: res += "R"; break;
94  case QUEEN: res += "Q"; break;
95  case KING: res += "K"; break;
96  default: //PAWN
97  if (capture) res += 'a' + (from % 8);
98  }
99  if (fromFyle) res += 'a' + (from % 8);
100  if (fromRank) res += '1' + (from / 8);
101  if (capture) res += "x";
102  res += 'a' + (to % 8);
103  res += '1' + (to / 8);
104  if (isPromo()) {
105  switch (getPromo()) {
106  case BISHOP: res += "=B"; break;
107  case KNIGHT: res += "=N"; break;
108  case ROOK: res += "=R"; break;
109  case QUEEN: res += "=Q"; break;
110  }
111  }
112  if (check) res += "+";
113  return res;
114  }
115 
117  ASSERT(promo == QUEEN || promo == ROOK || promo == BISHOP ||
118  promo == KNIGHT);
119  m_ |= ((promo - 2) << 12) | (1 << 14);
120  }
121  void setCapture(pieceT piece, bool enPassant) {
122  m_ |= ((piece & 0x07) << 21);
123  if (enPassant) m_ |= (2 << 14);
124  }
125  void setAmbiguity(bool fyle, bool rank) {
126  m_ &= ~(3 << 28);
127  if (fyle)
128  m_ |= (1 << 28);
129  if (rank)
130  m_ |= (1 << 29);
131  }
132  void setCheck() { m_ |= (1 << 30); }
133 };
134 
135 #endif
squareT getTo() const
Definition: fullmove.h:66
piecew sq
Definition: board.tcl:1248
const colorT WHITE
Definition: common.h:207
bool isCastle() const
Definition: fullmove.h:65
promopiece
Definition: calvar.tcl:258
#define ASSERT(f)
Definition: common.h:59
constexpr FullMove(uint32_t m=0)
Definition: fullmove.h:49
const pieceT KING
Definition: common.h:226
bool operator!=(const FullMove &f) const
Definition: fullmove.h:62
const colorT BLACK
Definition: common.h:208
pieceT getCaptured() const
Definition: fullmove.h:71
FullMove(colorT c, squareT from, squareT to, pieceT pt)
Definition: fullmove.h:57
colorT getColor() const
Definition: fullmove.h:69
const pieceT BISHOP
Definition: common.h:229
const pieceT KNIGHT
Definition: common.h:230
pieceT getPiece() const
Definition: fullmove.h:68
const pieceT QUEEN
Definition: common.h:227
void setCapture(pieceT piece, bool enPassant)
Definition: fullmove.h:121
const pieceT ROOK
Definition: common.h:228
bool isEnpassant() const
Definition: fullmove.h:64
void setAmbiguity(bool fyle, bool rank)
Definition: fullmove.h:125
bool isPromo() const
Definition: fullmove.h:63
void setPromo(pieceT promo)
Definition: fullmove.h:116
FullMove(colorT c, squareT kingSq, squareT rookSq)
Definition: fullmove.h:51
pieceT getPromo() const
Definition: fullmove.h:70
byte colorT
Definition: common.h:104
void setCheck()
Definition: fullmove.h:132
squareT getFrom() const
Definition: fullmove.h:67
byte squareT
Definition: common.h:105
byte pieceT
Definition: common.h:103
std::string getSAN(colorT *toMove=0) const
Definition: fullmove.h:72