Belofte version 2.1.8
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/** create PGN move based on basicmove_t
13 * for this, we create all moves in position and compare it
14 */
16 : m_move{""}
17 , m_evalstr{""}
18{
19 bMoveList ml(b);
20 movenum_t n_moves = ml.getNumberOfMoves();
21 for (movenum_t moveid = 1; moveid <= n_moves; moveid++) {
22 if (ml.getMoveT(moveid) == bm) {
23 ctor(b, bMove(ml[moveid]));
24 break;
25 }
26 }
27}
28
30 : m_move{m.m_move}
31 , m_evalstr{m.m_evalstr}
32{
33}
34
36 : m_move{""}
37 , m_evalstr{""}
38{
39 ctor(b, m);
40}
41
42void bPgnMove::ctor(bBoard& b, bMove const& m)
43{
44 std::string piece = bPiece::getPieceStrUpper(b.getPiece(m.from()));
45 movelist_t alternatives;
46
47 if (piece == S_P_KING) {
48 if (m.isCastleMove()) {
49 m_move = "O-O";
50 if (m.isLongCastleMove()) {
51 m_move += "-O";
52 }
53 } else {
54 m_move = piece;
55 }
56 } else if (piece != S_P_PAWN) {
57 m_move = piece;
58
59 // TOCHECK: why can't we capture alternatives, b ?
60 auto getSiblings = [m](bBoard& bs) -> movelist_t {
61 movelist_t siblings;
62 bMoveList& ml = bs.getMoveListRef();
63 movenum_t n_moves = ml.generateMoves(bs);
64 for (movenum_t moveid = 1; moveid <= n_moves; moveid++) {
65 bMove bm(ml[moveid]);
66 if (bm.to() == m.to()) {
67 if (!(bm.from() == m.from())
68 && (bs.getPiece(bm.from()) == bs.getPiece(m.from()))) {
69 // same destination sq, same major piece, not same move
70 siblings.emplace_back(bm);
71 }
72 }
73 }
74 return siblings;
75 };
76 alternatives = getSiblings(b);
77 }
78
79 if (m.isCapture()) {
80 if (piece == S_P_PAWN) {
81 m_move = (bCase(m.from()).operator std::string()).substr(0,1);
82 }
83 m_move += "x";
84 }
85
86 if (!m.isCastleMove()) {
87 m_move += bCase(m.to()).operator std::string();
88 }
89
90 if (m.isPromotion()) {
91 m_move += getPromotionDecorationStr(m.getPromotion());
92 }
93
94 if (alternatives.size() > 0) {
95 // add column or row or both if multiple moves are possible
96 // with same destination
97 if (std::none_of(alternatives.begin(), alternatives.end(),
98 [m](bMove const& bm)
99 { return bm.fromcolumn() == m.fromcolumn(); })) {
100 // add column descriptor
101 m_move.insert(1, (bCase(m.from()).operator std::string()).substr(0, 1));
102 } else if (std::none_of(alternatives.begin(), alternatives.end(),
103 [m](bMove const& bm)
104 { return bm.fromrank() == m.fromrank(); })) {
105 // add row descriptor
106 m_move.insert(1, (bCase(m.from()).operator std::string()).substr(1, 1));
107 } else {
108 // add from case, both row and column are not unique
109 m_move.insert(1, (bCase(m.from()).operator std::string()));
110 }
111 }
112
113 if (m.isCheck()) {
115 && m.isEndOfGame()) {
116 m_move += "#";
117 } else {
118 m_move += "+";
119 }
120 }
121
122 if (!m_move.size()) throw std::logic_error("empty bPgnMove");
123 m_evalstr = m.getMoveEvalStr();
124}
125
127{
128 m_move.clear();
129 m_evalstr.clear();
130}
131
132//-----------------------------------------------------------------------
133
134std::string const bPgnMove::getPromotionDecorationStr(ppiece_t const piece)
135 const
136{
137 if (piece == tPPiece::N_P_QUEEN) {
138 return "=Q";
139 } else if (piece == tPPiece::N_P_KNIGHT) {
140 return "=N";
141 } else if (piece == tPPiece::N_P_ROOK) {
142 return "=R";
143 } else if (piece == tPPiece::N_P_BISHOP) {
144 return "=B";
145 }
146 throw std::logic_error("superfluous call to bPgnMove::getPromotionDecorationStr");
147}
148
149//-----------------------------------------------------------------------
150
151bool operator==(bPgnMove const& lhs, std::string const& rhs)
152{
153 if (lhs.m_move == rhs) return true;
154 return false;
155}
156
157std::ostream& operator<<(std::ostream& os, bPgnMove const& m)
158{
159 os << m.m_move;
160 return os;
161}
162
163//-----------------------------------------------------------------------
164
166 : m_pmoves{}
167{
168}
169
171 : m_pmoves{}
172{
173 bMoveList& ml = b.getMoveListRef();
174 movenum_t n_moves = ml.generateMoves(b);
175 for (movenum_t moveid = 1; moveid <= n_moves; moveid++) {
176 m_pmoves.emplace_back(b, ml[moveid]);
177 }
178}
179
181{
182 m_pmoves.clear();
183}
184
185std::ostream& operator<<(std::ostream& os, bPgnMoveList const& mpl)
186{
187 os << "#" << belofte::to_string(static_cast<int64_t>(mpl.m_pmoves.size())) << " ";
188 for (bPgnMove const& m: mpl.m_pmoves) {
189 os << m << m.getMoveEvalStr() << " ";
190 }
191 return os;
192}
193
194// eof
This is the main include file, needs to be included before any other include.
uint16_t basicmove_t
Definition belofte.h:107
uint_fast8_t movenum_t
Definition belofte.h:109
piece_t getPiece(case_t const bc) const
1 for white, 2 for black
Definition board.cpp:157
MEMBER_CONSTEXPR case_t to() const
Definition move.h:29
MEMBER_CONSTEXPR case_t from() const
Definition move.h:25
ppiece_t getPromotion() const
Definition move.h:39
board
Definition board.h:147
bMoveList & getMoveListRef()
return reference to movelist
Definition board.cpp:954
position on board, defined as 255 if invalid used primarily to compose a move or a source or destinat...
Definition case.h:17
Definition move.h:69
bool isCapture() const
Definition move.h:87
bool isCastleMove() const
Definition move.h:91
bool isPromotion() const
Definition move.h:83
bool isCheck() const
Definition move.h:95
bScore getScore() const
Definition move.h:107
std::string getMoveEvalStr() const
Definition move.cpp:116
bool isEndOfGame() const
Flag if mated.
Definition move.cpp:140
bool isLongCastleMove() const
Definition move.h:94
movenum_t getNumberOfMoves() const
Definition movelist.cpp:332
movenum_t generateMoves(bBoard const &b)
generate moves if not yet generated
Definition movelist.cpp:259
basicmove_t getMoveT(movenum_t const moveid) const
Definition movelist.cpp:327
PgnMove is for user-interface only.
Definition pgnmove.h:12
bPgnMove(bPgnMove const &m)
Definition pgnmove.cpp:29
static const std::string getPieceStrUpper(piece_t const piece)
static class member function
Definition piece.cpp:136
constexpr bScore SCORE_POSITIVE
Definition eval.h:16
constexpr bScore SCORE_THEORETIC_DRAW
Definition eval.h:17
std::vector< bMove > movelist_t
Definition movelist.h:11
bool operator==(bPgnMove const &lhs, std::string const &rhs)
Definition pgnmove.cpp:151
std::ostream & operator<<(std::ostream &os, bPgnMove const &m)
Definition pgnmove.cpp:157
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