Chess Engine
C++ chess engine with movegen, bitboards, and Arduino-friendly docs
Loading...
Searching...
No Matches
notation.cpp
Go to the documentation of this file.
1#include <cstdio>
2#include "defs.h"
4#include "notation.h"
5#include "setup.h"
6
7char* squareToString(const int square) {
8 static char sqStr[3];
9
10 const int fileIdx = fileIndex120[square];
11 const int rankIdx = rankIndex120[square];
12
13 std::sprintf(sqStr, "%c%c", ('a' + fileIdx), ('1' + rankIdx));
14 return sqStr;
15}
16
17char* moveToString(const int move) {
18 static char mvStr[6];
19
20 const int fromFile = fileIndex120[FROMSQ(move)];
21 const int fromRank = rankIndex120[FROMSQ(move)];
22 const int toFile = fileIndex120[TOSQ(move)];
23 const int toRank = rankIndex120[TOSQ(move)];
24
25 const int promoted = PROMOTED(move);
26
27 if (promoted) {
28 char promoChar = 'q';
29 if (IsKn(promoted)) {
30 promoChar = 'n';
31 } else if (IsRQ(promoted) && !IsBQ(promoted)) {
32 promoChar = 'r';
33 } else if (!IsRQ(promoted) && IsBQ(promoted)) {
34 promoChar = 'b';
35 }
36 std::sprintf(mvStr, "%c%c%c%c%c",
37 ('a' + fromFile), ('1' + fromRank),
38 ('a' + toFile), ('1' + toRank),
39 promoChar);
40 } else {
41 std::sprintf(mvStr, "%c%c%c%c",
42 ('a' + fromFile), ('1' + fromRank),
43 ('a' + toFile), ('1' + toRank));
44 }
45 return mvStr;
46}
47
48int parseMoveString(char* text, S_BOARD* board) {
50
51 // quick format checks
52 if (text[1] > '8' || text[1] < '1') return NOMOVE;
53 if (text[3] > '8' || text[3] < '1') return NOMOVE;
54 if (text[0] > 'h' || text[0] < 'a') return NOMOVE;
55 if (text[2] > 'h' || text[2] < 'a') return NOMOVE;
56
57 const int fromSq = FR2SQ(text[0] - 'a', text[1] - '1');
58 const int toSq = FR2SQ(text[2] - 'a', text[3] - '1');
59
60 ASSERT(SqOnBoard(fromSq) && SqOnBoard(toSq));
61
62 S_MOVELIST moveList[1];
63 generateAllMoves(board, moveList);
64
65 for (int i = 0; i < moveList->count; ++i) {
66 const int moveEnc = moveList->moves[i].move;
67 if (FROMSQ(moveEnc) == fromSq && TOSQ(moveEnc) == toSq) {
68 const int promoPiece = PROMOTED(moveEnc);
69 if (promoPiece != EMPTY) {
70 // require matching promotion letter in text[4]
71 if (IsRQ(promoPiece) && !IsBQ(promoPiece) && text[4] == 'r') return moveEnc;
72 if (!IsRQ(promoPiece) && IsBQ(promoPiece) && text[4] == 'b') return moveEnc;
73 if (IsRQ(promoPiece) && IsBQ(promoPiece) && text[4] == 'q') return moveEnc;
74 if (IsKn(promoPiece) && text[4] == 'n') return moveEnc;
75 continue;
76 }
77 return moveEnc;
78 }
79 }
80 return NOMOVE;
81}
82
83void printMoveList(const S_MOVELIST* moveList) {
84 std::printf("MoveList:\n");
85 for (int i = 0; i < moveList->count; ++i) {
86 const int moveEnc = moveList->moves[i].move;
87 const int score = moveList->moves[i].score;
88 std::printf("Move:%d > %s (score:%d)\n", i + 1, moveToString(moveEnc), score);
89 }
90 std::printf("MoveList Total %d Moves:\n\n", moveList->count);
91}
bool isBoardStateValid(const S_BOARD *pos)
Performs a full internal consistency check of the board state.
Definition board.cpp:28
#define FR2SQ(f, r)
Definition defs.h:181
#define IsKn(p)
Definition defs.h:189
#define TOSQ(m)
Definition defs.h:165
@ EMPTY
Definition defs.h:40
#define IsBQ(p)
Definition defs.h:187
#define NOMOVE
Definition defs.h:176
#define FROMSQ(m)
Definition defs.h:164
#define ASSERT(n)
Definition defs.h:14
#define IsRQ(p)
Definition defs.h:188
#define PROMOTED(m)
Definition defs.h:167
void generateAllMoves(const S_BOARD *board, S_MOVELIST *moveList)
Generate all pseudo-legal moves for the side to move.
int parseMoveString(char *text, S_BOARD *board)
Parse a move from coordinate notation.
Definition notation.cpp:48
char * moveToString(const int move)
Convert an encoded move to coordinate notation.
Definition notation.cpp:17
char * squareToString(const int square)
Convert a board square index to algebraic notation.
Definition notation.cpp:7
void printMoveList(const S_MOVELIST *moveList)
Print all moves in a move list to stdout.
Definition notation.cpp:83
int rankIndex120[BRD_SQ_NUM]
Definition setup.cpp:27
int fileIndex120[BRD_SQ_NUM]
File and rank lookup tables for each square (120-based).
Definition setup.cpp:26
int score
Definition defs.h:63
int move
Definition defs.h:62
int count
Definition defs.h:68
S_MOVE moves[MAXPOSITIONMOVES]
Definition defs.h:67
int SqOnBoard(const int sq)
Checks if a given square index refers to a valid on-board square.
Definition validate.cpp:29