libscid 0.1.0
Chess applications made easy.
Loading...
Searching...
No Matches
primitives.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2000-2004 Shane Hudson..
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
23#pragma once
24
25#include <cstdint>
26
27namespace scid::core {
28
29// General scalar types
30typedef unsigned char byte; // 8 bit unsigned
31typedef std::uint16_t ushort; // 16 bit unsigned
32typedef std::uint32_t uint; // 32 bit unsigned
33typedef std::int32_t sint; // 32 bit signed
34
35typedef byte pieceT; // e.g ROOK or WK
36typedef byte colorT; // WHITE or BLACK
37typedef byte squareT; // e.g. A3
38typedef byte rankT; // Chess board rank
39typedef byte fyleT; // Chess board file
40typedef byte directionT; // e.g. UP_LEFT
41typedef byte leftDiagT; // Up-left diagonals
42typedef byte rightDiagT; // Up-right diagonals
43typedef byte castleDirT; // LEFT or RIGHT
44
45const castleDirT QSIDE = 0, KSIDE = 1;
46
47//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48// PIECES COLORS
49const unsigned NUM_COLOR_TYPES = 2;
50const colorT WHITE = 0, BLACK = 1, NOCOLOR = 2;
51const char COLOR_CHAR[3] = {'W', 'B', '_'};
52
53inline colorT color_Flip(colorT c) { return 1 - c; }
54
55inline char color_Char(colorT c) { return COLOR_CHAR[c]; }
56
57// PIECE TYPES (without color; same value as a white piece)
58const pieceT INVALID_PIECE = 0, KING = 1, QUEEN = 2, ROOK = 3, BISHOP = 4,
59 KNIGHT = 5, PAWN = 6;
60
61// PIECES:
62// Note that color(x) == ((x & 0x8) >> 3) and type(x) == (x & 0x7)
63// EMPTY is deliberately nonzero, and END_OF_BOARD is zero, so that
64// a board can be used as a regular 0-terminated string, provided
65// that board[NULL_SQUARE] == END_OF_BOARD, as it always should be.
66const pieceT EMPTY = 7;
67const pieceT END_OF_BOARD = 0;
68const pieceT WK = 1, WQ = 2, WR = 3, WB = 4, WN = 5, WP = 6;
69const pieceT BK = 9, BQ = 10, BR = 11, BB = 12, BN = 13, BP = 14;
70
71inline colorT piece_Color(pieceT p) {
72 return (p == EMPTY) ? NOCOLOR : ((p & 8) >> 3);
73}
74// Slightly faster piece_Color when we are sure the piece is not empty:
75inline colorT piece_Color_NotEmpty(pieceT p) { return (p & 8) >> 3; }
76
77inline pieceT piece_Type(pieceT p) { return (p & 7); }
78
79inline pieceT piece_Make(colorT c, pieceT p) { return ((c << 3) | (p & 7)); }
80
81// PIECE_CHAR[]: array of piece characters, capitals for White pieces.
82const char PIECE_CHAR[] = "xKQRBNP.xkqrbnpxMm";
83
84inline char piece_Char(pieceT p) { return PIECE_CHAR[piece_Type(p)]; }
85
87 pieceT pieceFromByte_[256] = {};
88
89public:
90 constexpr PieceFromByte() {
91 for (auto& e : pieceFromByte_) {
92 e = EMPTY;
93 }
94 pieceFromByte_[(int)'K'] = WK;
95 pieceFromByte_[(int)'k'] = BK;
96 pieceFromByte_[(int)'Q'] = WQ;
97 pieceFromByte_[(int)'q'] = BQ;
98 pieceFromByte_[(int)'R'] = WR;
99 pieceFromByte_[(int)'r'] = BR;
100 pieceFromByte_[(int)'B'] = WB;
101 pieceFromByte_[(int)'b'] = BB;
102 pieceFromByte_[(int)'N'] = WN;
103 pieceFromByte_[(int)'n'] = BN;
104 pieceFromByte_[(int)'P'] = WP;
105 pieceFromByte_[(int)'p'] = BP;
106 };
107
108 pieceT operator[](unsigned char idx) const { return pieceFromByte_[idx]; }
109};
110constexpr inline auto pieceFromByte = PieceFromByte();
111
112//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113// SQUARES
114const squareT A1 = 0, B1 = 1, C1 = 2, D1 = 3, E1 = 4, F1 = 5, G1 = 6, H1 = 7,
115 A2 = 8, B2 = 9, C2 = 10, D2 = 11, E2 = 12, F2 = 13, G2 = 14,
116 H2 = 15, A3 = 16, B3 = 17, C3 = 18, D3 = 19, E3 = 20, F3 = 21,
117 G3 = 22, H3 = 23, A4 = 24, B4 = 25, C4 = 26, D4 = 27, E4 = 28,
118 F4 = 29, G4 = 30, H4 = 31, A5 = 32, B5 = 33, C5 = 34, D5 = 35,
119 E5 = 36, F5 = 37, G5 = 38, H5 = 39, A6 = 40, B6 = 41, C6 = 42,
120 D6 = 43, E6 = 44, F6 = 45, G6 = 46, H6 = 47, A7 = 48, B7 = 49,
121 C7 = 50, D7 = 51, E7 = 52, F7 = 53, G7 = 54, H7 = 55, A8 = 56,
122 B8 = 57, C8 = 58, D8 = 59, E8 = 60, F8 = 61, G8 = 62, H8 = 63,
123 COLOR_SQUARE = 64, NULL_SQUARE = 65,
124 NS = 65; // NS is abbreviation for NULL_SQUARE.
125
126const rankT RANK_1 = 0, RANK_2 = 1, RANK_3 = 2, RANK_4 = 3, RANK_5 = 4,
127 RANK_6 = 5, RANK_7 = 6, RANK_8 = 7, NO_RANK = 64;
128
129const fyleT
130 // we use "fyle" instead of "file" to avoid confusion with disk files.
131 A_FYLE = 0,
132 B_FYLE = 1, C_FYLE = 2, D_FYLE = 3, E_FYLE = 4, F_FYLE = 5, G_FYLE = 6,
133 H_FYLE = 7, NO_FYLE = 64;
134
135inline rankT rank_FromChar(char c) {
136 if (c < '1' || c > '8') {
137 return NO_RANK;
138 } else
139 return (c - '1');
140}
141
142inline fyleT fyle_FromChar(char c) {
143 if (c < 'a' || c > 'h') {
144 return NO_FYLE;
145 } else
146 return (c - 'a');
147}
148
149constexpr squareT square_Make(fyleT f, rankT r) { return ((r << 3) | f); }
150
151constexpr fyleT square_Fyle(squareT sq) { return (sq & 0x7); }
152
153constexpr rankT square_Rank(squareT sq) { return ((sq >> 3) & 0x7); }
154
155constexpr squareT square_Relative(colorT c, squareT sq) {
156 return static_cast<squareT>(sq ^ (c * 56));
157}
158
159constexpr rankT rank_Relative(colorT c, rankT r) {
160 return static_cast<rankT>(r ^ (c * 7));
161}
162
163} // namespace scid::core
Definition primitives.h:86