Scid  4.6.5
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  FullMove(uint32_t m = 0) : m_(m) {};
50  bool operator!=(const FullMove& f) const { return m_ != f.m_; }
51  bool isNull() const { return (m_ & 0xFFFF) == 0; }
52  bool isPromo() const { return (m_ & (3 << 14)) == (1 << 14); }
53  bool isEnpassant() const { return (m_ & (3 << 14)) == (2 << 14); }
54  bool isCastle() const { return (m_ & (3 << 14)) == (3 << 14); }
55  squareT getTo() const { return m_ & 0x3F; }
56  squareT getFrom() const { return (m_ >> 6) & 0x3F; }
57  pieceT getPiece() const { return (m_ >> 24) & 0x07; }
58  colorT getColor() const { return (m_ >> 27 & 1) ? BLACK : WHITE; }
59  pieceT getPromo() const { return ((m_ >> 12) & 0x03) +2; }
60  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  std::string getSAN(colorT* toMove = 0) const {
67  std::string res;
68  if (toMove) *toMove = getColor();
69  squareT to = getTo();
70  squareT from = getFrom();
71  if (to == 0 && from == 0) return "--";
72  if (isCastle()) return (to > from) ? "O-O" : "O-O-O";
73  bool fromFyle = (m_ >> 28) & 1;
74  bool fromRank = (m_ >> 29) & 1;
75  bool check = (m_ >> 30) & 1;
76  bool capture = (getCaptured() != 0);
77 
78  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  if (capture) res += 'a' + (from % 8);
86  }
87  if (fromFyle) res += 'a' + (from % 8);
88  if (fromRank) res += '1' + (from / 8);
89  if (capture) res += "x";
90  res += 'a' + (to % 8);
91  res += '1' + (to / 8);
92  if (isPromo()) {
93  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  if (check) res += "+";
101  return res;
102  }
103 
104  void reset(colorT c, pieceT p, squareT from, squareT to, pieceT promo = 0) {
105  m_ = to | (from << 6) | (p << 24) | (c << 27);
106  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  m_ = rookSq | (kingSq << 6) | (3 << 14) | (KING << 24) | (c << 27);
111  }
112  void setCapture(pieceT piece, bool enPassant) {
113  m_ |= ((piece & 0x07) << 21);
114  if (enPassant) m_ |= (2 << 14);
115  }
116  void setAmbiguous(const FullMove& move2) {
117  if ((m_ & 0x700003F) == (move2.m_ & 0x700003F)) {
118  int from = getFrom();
119  int from2 = move2.getFrom();
120  if ((from % 8) != (from2 % 8)) m_ |= (1 << 28);
121  else m_ |= (1 << 29);
122  }
123  }
124  void setCheck() { m_ |= (1 << 30); }
125 };
126 
127 #endif
bool isCastle() const
Definition: fullmove.h:54
piecew sq
Definition: board.tcl:1293
const colorT WHITE
Definition: common.h:203
squareT getTo() const
Definition: fullmove.h:55
promopiece
Definition: calvar.tcl:255
const pieceT KING
Definition: common.h:221
void setAmbiguous(const FullMove &move2)
Definition: fullmove.h:116
const colorT BLACK
Definition: common.h:204
const pieceT BISHOP
Definition: common.h:224
squareT getCaptSq() const
Definition: fullmove.h:61
const pieceT KNIGHT
Definition: common.h:225
bool isPromo() const
Definition: fullmove.h:52
bool operator!=(const FullMove &f) const
Definition: fullmove.h:50
const pieceT QUEEN
Definition: common.h:222
void setCapture(pieceT piece, bool enPassant)
Definition: fullmove.h:112
std::string getSAN(colorT *toMove=0) const
Definition: fullmove.h:66
const pieceT ROOK
Definition: common.h:223
bool isEnpassant() const
Definition: fullmove.h:53
pieceT getCaptured() const
Definition: fullmove.h:60
pieceT getPromo() const
Definition: fullmove.h:59
squareT getFrom() const
Definition: fullmove.h:56
void resetCastle(colorT c, squareT kingSq, squareT rookSq)
Definition: fullmove.h:108
FullMove(uint32_t m=0)
Definition: fullmove.h:49
byte colorT
Definition: common.h:112
colorT getColor() const
Definition: fullmove.h:58
void setCheck()
Definition: fullmove.h:124
void reset(colorT c, pieceT p, squareT from, squareT to, pieceT promo=0)
Definition: fullmove.h:104
bool isNull() const
Definition: fullmove.h:51
pieceT getPiece() const
Definition: fullmove.h:57
byte squareT
Definition: common.h:113
byte pieceT
Definition: common.h:111