Belofte version 2.2.0
A promising chess program using the UCI or Winboard interface
pgnmove.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: pgnmove.cpp
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#include "belofte.h"
9
10//-----------------------------------------------------------------------
11
12#if defined(__GNUC__)
13#pragma GCC diagnostic push
14#pragma GCC diagnostic ignored "-Weffc++"
15#endif
16
17/**
18 * create PGN move based on bMove
19 * note: std::string members do not need to be initialised
20 */
21bPgnMove::bPgnMove(bBoard const& b, bmove_t const bmt)
22{
23 bMoveList ml;
24 ml.generateMoves(b);
25 movenum_t n_moves = ml.getNumberOfMoves();
26 for (ml.curmoveid = 1; ml.curmoveid <= n_moves; ++ml.curmoveid) {
27 bMove const& m = ml[ml.curmoveid];
28 if (m == bmt) {
29 ctor(b, m);
30 break;
31 }
32 }
33}
34
35#if defined(__GNUC__)
36#pragma GCC diagnostic pop
37#endif
38
39void bPgnMove::ctor(bBoard const& b, bMove const& m)
40{
41 std::string piece = bPiece::getPieceStrUpper(b.getPiece(m.from()));
42 movelist_t alternatives;
43
44 if (piece == S_P_KING) {
45 if (m.isCastleMove()) {
46 m_move = "O-O";
47 if (m.isLongCastleMove()) {
48 m_move += "-O";
49 }
50 } else {
51 m_move = piece;
52 }
53 } else if (piece != S_P_PAWN) {
54 m_move = piece;
55
56 /// @todo check why can't we capture alternatives, b ?
57 auto getSiblings = [m](bBoard const& bs) -> movelist_t {
58 movelist_t siblings;
59 bMoveList ml;
60 movenum_t n_moves = ml.generateMoves(bs);
61 for (ml.curmoveid = 1; ml.curmoveid <= n_moves; ++ml.curmoveid) {
62 bMove const& bm(ml[ml.curmoveid]);
63 if (bm.to() == m.to()) {
64 if (!(bm.from() == m.from())
65 && (bs.getPiece(bm.from()) == bs.getPiece(m.from()))) {
66 // same destination sq, same major piece, not same move
67 siblings.push_back(bm);
68 }
69 }
70 }
71 return siblings;
72 };
73 alternatives = getSiblings(b);
74 }
75
76 if (m.isCapture()) {
77 if (piece == S_P_PAWN) {
78 m_move = (bCase(m.from()).operator std::string()).substr(0,1);
79 }
80 m_move += "x";
81 }
82
83 if (!m.isCastleMove()) {
84 m_move += bCase(m.to()).operator std::string();
85 }
86
87 if (m.isPromotion()) {
88 m_move += getPromotionDecorationStr(m.getPromotion());
89 }
90
91 if (alternatives.size() > 0) {
92 // add column or row or both if multiple moves are possible
93 // with same destination
94 if (std::none_of(alternatives.begin(), alternatives.end(),
95 [m](bMove const& bm)
96 { return bm.fromcolumn() == m.fromcolumn(); })) {
97 // add column descriptor
98 m_move.insert(1, (bCase(m.from()).operator std::string()).substr(0, 1));
99 } else if (std::none_of(alternatives.begin(), alternatives.end(),
100 [m](bMove const& bm)
101 { return bm.fromrank() == m.fromrank(); })) {
102 // add row descriptor
103 m_move.insert(1, (bCase(m.from()).operator std::string()).substr(1, 1));
104 } else {
105 // add from case, both row and column are not unique
106 m_move.insert(1, (bCase(m.from()).operator std::string()));
107 }
108 }
109
110 if (m.isCheck()) {
111 if ((!m.isDrawScore()) && m.isMateMove()) {
112 m_move += "#";
113 } else {
114 m_move += "+";
115 }
116 }
117
118 if (!m_move.size()) throw std::logic_error("empty bPgnMove");
119 m_evalstr = m.getMoveEvalStr();
120}
121
122//-----------------------------------------------------------------------
123
124std::string bPgnMove::getPromotionDecorationStr(ppiece_t const piece)
125{
126 if (piece == tPPiece::N_P_QUEEN) {
127 return "=Q";
128 } else if (piece == tPPiece::N_P_KNIGHT) {
129 return "=N";
130 } else if (piece == tPPiece::N_P_ROOK) {
131 return "=R";
132 } else if (piece == tPPiece::N_P_BISHOP) {
133 return "=B";
134 }
135 throw std::logic_error("superfluous call to bPgnMove::getPromotionDecorationStr");
136}
137
138//-----------------------------------------------------------------------
139
140#if defined(__GNUC__)
141#pragma GCC diagnostic push
142#pragma GCC diagnostic ignored "-Weffc++"
143#endif
144
146{
147 bMoveList ml;
148 movenum_t n_moves = ml.generateMoves(b);
149 for (ml.curmoveid = 1; ml.curmoveid <= n_moves; ++ml.curmoveid) {
150 bPgnMove pm(b, ml[ml.curmoveid].getBMoveT());
151 m_pmoves.push_back(pm);
152 }
153}
154
155#if defined(__GNUC__)
156#pragma GCC diagnostic pop
157#endif
158
159std::ostream& operator<<(std::ostream& os, bPgnMoveList const& mpl)
160{
161 os << "#" << belofte::to_string(static_cast<int64_t>(mpl.m_pmoves.size())) << " ";
162 for (bPgnMove const& m: mpl.m_pmoves) {
163 os << m << m.getMoveEvalStr() << " ";
164 }
165 return os;
166}
167
168// eof
This is the main include file, needs to be included before any other include.
uint32_t bmove_t
Definition belofte.h:101
uint_fast8_t movenum_t
Definition belofte.h:103
constexpr piece_t getPiece(case_t const cf) const
Definition basicboard.h:178
constexpr bool isLongCastleMove() const
Definition basicmove.h:107
constexpr bool isCastleMove() const
Definition basicmove.h:101
constexpr bool isCapture() const
Definition basicmove.h:93
constexpr ppiece_t getPromotion() const
Definition basicmove.h:74
constexpr case_t to() const
Definition basicmove.h:58
constexpr bool isPromotion() const
Definition basicmove.h:82
constexpr bool isCheck() const
Definition basicmove.h:109
constexpr case_t from() const
Definition basicmove.h:56
board
Definition board.h:45
Definition move.h:13
constexpr bool isDrawScore() const
Definition move.h:59
std::string getMoveEvalStr() const
Definition move.cpp:17
bool isMateMove() const
Check if end of game flag is set, and not forced draw.
Definition move.cpp:41
movenum_t generateMoves(bBasicBoard const &b)
generate moves if not yet generated
Definition movelist.cpp:417
constexpr bmove_t getBMoveT(movenum_t const moveid) const
Definition movelist.h:58
constexpr movenum_t getNumberOfMoves() const
Definition movelist.h:49
movenum_t curmoveid
Definition movelist.h:104
PgnMove is for user-interface only.
Definition pgnmove.h:14
std::string const & getMoveEvalStr() const
Definition pgnmove.h:35
bPgnMove(bPgnMove const &pm) noexcept
Definition pgnmove.h:16
static std::string getPieceStrUpper(piece_t const piece)
static class member function
Definition piece.cpp:173
std::vector< bMove > movelist_t
Definition movelist.h:11
std::string to_string(int16_t value)
std::to_string not compatible on Mac OS (Apple LLVM version 5.0) provide generic utility function
Definition util.cpp:186
std::ostream & operator<<(std::ostream &os, bPgnMoveList const &mpl)
Definition pgnmove.cpp:159
enum tPPiece ppiece_t
Definition piece.h:52
#define S_P_PAWN
Definition piece.h:14
#define S_P_KING
Definition piece.h:12
@ N_P_KNIGHT
Definition piece.h:48
@ N_P_QUEEN
Definition piece.h:48
@ N_P_ROOK
Definition piece.h:48
@ N_P_BISHOP
Definition piece.h:48