209 lines
8.5 KiB
C++
209 lines
8.5 KiB
C++
#ifndef INCLUDE_GUARD_TYPES_H
|
|
#define INCLUDE_GUARD_TYPES_H
|
|
|
|
#include <cstdint> // uint64_t
|
|
#include <limits> // std::numeric_limits
|
|
|
|
/** square, file and rank index (index > 63 indicates illegal or off board) */
|
|
using Index = unsigned;
|
|
/** Bit board, exactly 64 bits (one bit per square) */
|
|
using u64 = uint64_t; // easy on the eyes (well, my eyes)
|
|
/**
|
|
* Board position score from white point of view in centipawns.
|
|
* (1 pawn ~ 100 centipawns)
|
|
*/
|
|
using Score = int;
|
|
|
|
template <typename T>
|
|
struct limits {
|
|
static constexpr T upper();
|
|
static constexpr T lower();
|
|
};
|
|
|
|
template <>
|
|
struct limits<Score> {
|
|
static constexpr Score upper() { return static_cast<Score>(+32768); };
|
|
static constexpr Score lower() { return static_cast<Score>(-32768); };
|
|
static constexpr bool isMate(const Score score) {
|
|
constexpr Score mateBound = upper() - 512;
|
|
return (score < -mateBound) || (mateBound < score);
|
|
}
|
|
};
|
|
|
|
enum piece {
|
|
none = 0,
|
|
white = 0,
|
|
black = 1,
|
|
pawn = 2,
|
|
knight = 3,
|
|
bishop = 4,
|
|
rook = 5,
|
|
queen = 6,
|
|
king = 7,
|
|
kingEG = 8 // Lookup index for king end game PSQT
|
|
};
|
|
|
|
enum square : Index {
|
|
a8, b8, c8, d8, e8, f8, g8, h8,
|
|
a7, b7, c7, d7, e7, f7, g7, h7,
|
|
a6, b6, c6, d6, e6, f6, g6, h6,
|
|
a5, b5, c5, d5, e5, f5, g5, h5,
|
|
a4, b4, c4, d4, e4, f4, g4, h4,
|
|
a3, b3, c3, d3, e3, f3, g3, h3,
|
|
a2, b2, c2, d2, e2, f2, g2, h2,
|
|
a1, b1, c1, d1, e1, f1, g1, h1
|
|
};
|
|
|
|
enum location {
|
|
Square,
|
|
Up, Down, Left, Right,
|
|
QueenSide = Left, KingSide = Right,
|
|
File, Rank,
|
|
Diag, AntiDiag,
|
|
RightUp, RightDown,
|
|
LeftUp, LeftDown,
|
|
Plus, Cross, Star,
|
|
WhiteSquares, BlackSquares
|
|
};
|
|
|
|
// Material weighting per piece
|
|
constexpr Score pieceValues[8] = {
|
|
0, 0, // white, black (irrelevant)
|
|
100, // pawn
|
|
295, // knight
|
|
315, // bishop
|
|
450, // rook
|
|
870, // queen
|
|
0 // king (irrelevant, always 2 opposite kings)
|
|
};
|
|
|
|
// Move lookup tables for knights and kings
|
|
constexpr u64 knightMoveLookup[64] = {
|
|
0x0000000000020400, 0x0000000000050800, 0x00000000000a1100, 0x0000000000142200,
|
|
0x0000000000284400, 0x0000000000508800, 0x0000000000a01000, 0x0000000000402000,
|
|
0x0000000002040004, 0x0000000005080008, 0x000000000a110011, 0x0000000014220022,
|
|
0x0000000028440044, 0x0000000050880088, 0x00000000a0100010, 0x0000000040200020,
|
|
0x0000000204000402, 0x0000000508000805, 0x0000000a1100110a, 0x0000001422002214,
|
|
0x0000002844004428, 0x0000005088008850, 0x000000a0100010a0, 0x0000004020002040,
|
|
0x0000020400040200, 0x0000050800080500, 0x00000a1100110a00, 0x0000142200221400,
|
|
0x0000284400442800, 0x0000508800885000, 0x0000a0100010a000, 0x0000402000204000,
|
|
0x0002040004020000, 0x0005080008050000, 0x000a1100110a0000, 0x0014220022140000,
|
|
0x0028440044280000, 0x0050880088500000, 0x00a0100010a00000, 0x0040200020400000,
|
|
0x0204000402000000, 0x0508000805000000, 0x0a1100110a000000, 0x1422002214000000,
|
|
0x2844004428000000, 0x5088008850000000, 0xa0100010a0000000, 0x4020002040000000,
|
|
0x0400040200000000, 0x0800080500000000, 0x1100110a00000000, 0x2200221400000000,
|
|
0x4400442800000000, 0x8800885000000000, 0x100010a000000000, 0x2000204000000000,
|
|
0x0004020000000000, 0x0008050000000000, 0x00110a0000000000, 0x0022140000000000,
|
|
0x0044280000000000, 0x0088500000000000, 0x0010a00000000000, 0x0020400000000000
|
|
};
|
|
constexpr u64 kingMoveLookup[64] = {
|
|
0x0000000000000302, 0x0000000000000705, 0x0000000000000E0A, 0x0000000000001C14,
|
|
0x0000000000003828, 0x0000000000007050, 0x000000000000E0A0, 0x000000000000C040,
|
|
0x0000000000030203, 0x0000000000070507, 0x00000000000E0A0E, 0x00000000001C141C,
|
|
0x0000000000382838, 0x0000000000705070, 0x0000000000E0A0E0, 0x0000000000C040C0,
|
|
0x0000000003020300, 0x0000000007050700, 0x000000000E0A0E00, 0x000000001C141C00,
|
|
0x0000000038283800, 0x0000000070507000, 0x00000000E0A0E000, 0x00000000C040C000,
|
|
0x0000000302030000, 0x0000000705070000, 0x0000000E0A0E0000, 0x0000001C141C0000,
|
|
0x0000003828380000, 0x0000007050700000, 0x000000E0A0E00000, 0x000000C040C00000,
|
|
0x0000030203000000, 0x0000070507000000, 0x00000E0A0E000000, 0x00001C141C000000,
|
|
0x0000382838000000, 0x0000705070000000, 0x0000E0A0E0000000, 0x0000C040C0000000,
|
|
0x0003020300000000, 0x0007050700000000, 0x000E0A0E00000000, 0x001C141C00000000,
|
|
0x0038283800000000, 0x0070507000000000, 0x00E0A0E000000000, 0x00C040C000000000,
|
|
0x0302030000000000, 0x0705070000000000, 0x0E0A0E0000000000, 0x1C141C0000000000,
|
|
0x3828380000000000, 0x7050700000000000, 0xE0A0E00000000000, 0xC040C00000000000,
|
|
0x0203000000000000, 0x0507000000000000, 0x0A0E000000000000, 0x141C000000000000,
|
|
0x2838000000000000, 0x5070000000000000, 0xA0E0000000000000, 0x40C0000000000000
|
|
};
|
|
|
|
// Declare I/O streams (allows to globaly replace the I/O streams)
|
|
#ifdef RCPP_RCOUT
|
|
#include <Rcpp.h>
|
|
// Set I/O streams to Rcpp I/O streams
|
|
static Rcpp::Rostream<true> cout;
|
|
static Rcpp::Rostream<false> cerr;
|
|
#elif NULLSTREAM
|
|
#include "nullstream.h"
|
|
// Set I/O streams to "null"
|
|
static nullstream cout;
|
|
static nullstream cerr;
|
|
#else
|
|
#include <iostream>
|
|
// Default STL I/O streams
|
|
using std::cout;
|
|
using std::cerr;
|
|
#endif
|
|
|
|
// Piece SQuare Tables (partially automated tuned tables via supervised
|
|
// optimization using stockfish [https://stockfishchess.org/] evaluated positions
|
|
// from the lichess database [https://database.lichess.org/])
|
|
// endgame table: https://www.chessprogramming.org/Simplified_Evaluation_Function
|
|
// Which is addapted by adding 50. then scaled by 2 / 3 and rounded.
|
|
constexpr Score PSQT[9][64] = {
|
|
{ }, { }, // white, black (empty)
|
|
{ // pawn (white)
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
109, 82, 89, 25, 25, 89, 82, 109,
|
|
21, 18, -3, 18, 18, -3, 18, 21,
|
|
-12, -1, -19, 6, 6, -19, -1, -12,
|
|
-25, -15, -22, 9, 9, -22, -15, -25,
|
|
-25, -11, -27, -23, -23, -27, -11, -25,
|
|
-25, -13, -23, -29, -29, -23, -13, -25,
|
|
0, 0, 0, 0, 0, 0, 0, 0 },
|
|
{ // knight (white)
|
|
-90, -80, -18, 26, 26, -18, -80, -90,
|
|
-40, -13, 21, -22, -22, 21, -13, -40,
|
|
6, 2, 32, 38, 38, 32, 2, 6,
|
|
-9, -11, 22, 20, 20, 22, -11, -9,
|
|
-13, -11, 14, 2, 2, 14, -11, -13,
|
|
-25, -10, 2, 3, 3, 2, -10, -25,
|
|
-21, -54, -12, -8, -8, -12, -54, -21,
|
|
-76, -21, -38, -34, -34, -38, -21, -76 },
|
|
{ // bishop (white)
|
|
-7, 19, 3, -21, -21, 3, 19, -7,
|
|
-15, -5, 6, 40, 40, 6, -5, -15,
|
|
12, 14, 18, 32, 32, 18, 14, 12,
|
|
-5, -2, 17, 26, 26, 17, -2, -5,
|
|
-19, -2, 2, 8, 8, 2, -2, -19,
|
|
2, 4, 2, 8, 8, 2, 4, 2,
|
|
-4, 8, 3, 1, 1, 3, 8, -4,
|
|
-31, -13, -7, -20, -20, -7, -13, -31 },
|
|
{ // rook (white)
|
|
-5, -2, 23, 40, 40, 23, -2, -5,
|
|
18, 17, 42, 25, 25, 42, 17, 18,
|
|
22, 14, 33, 40, 40, 33, 14, 22,
|
|
21, 16, 20, 28, 28, 20, 16, 21,
|
|
-4, -13, -5, 3, 3, -5, -13, -4,
|
|
-20, -2, -3, -2, -2, -3, -2, -20,
|
|
-11, -13, 0, -6, -6, 0, -13, -11,
|
|
-17, -4, 0, 7, 7, 0, -4, -17 },
|
|
{ // queen (white)
|
|
-55, -29, 59, 19, 19, 59, -29, -55,
|
|
12, -18, 34, 85, 85, 34, -18, 12,
|
|
33, 17, 31, 34, 34, 31, 17, 33,
|
|
51, 16, 21, 18, 18, 21, 16, 51,
|
|
-3, 24, 18, 26, 26, 18, 24, -3,
|
|
11, 14, 24, 2, 2, 24, 14, 11,
|
|
28, 5, 17, 15, 15, 17, 5, 28,
|
|
1, -10, -14, 18, 18, -14, -10, 1 },
|
|
{ // king middle game (white)
|
|
-5, -5, -5, -5, -5, -5, -5, -5,
|
|
-5, -5, -5, -5, -5, -5, -5, -5,
|
|
-5, -5, -5, -5, -5, -5, -5, -5,
|
|
-5, -5, -5, -5, -5, -5, -5, -5,
|
|
-5, -5, -5, -5, -5, -5, -5, -5,
|
|
-5, -5, -5, -5, -5, -5, -5, -5,
|
|
-4, -4, -4, -4, -4, -4, -4, -4,
|
|
24, 13, 3, -28, 2, -14, 15, 1 },
|
|
{ // king end game (white) // TODO: self/supervised tuning
|
|
0, 7, 13, 20, 20, 13, 7, 0,
|
|
13, 20, 27, 33, 33, 27, 20, 13,
|
|
13, 27, 47, 53, 53, 47, 27, 13,
|
|
13, 27, 53, 60, 60, 53, 27, 13,
|
|
13, 27, 53, 60, 60, 53, 27, 13,
|
|
13, 27, 47, 53, 53, 47, 27, 13,
|
|
13, 13, 33, 33, 33, 33, 13, 13,
|
|
0, 13, 13, 13, 13, 13, 13, 0 }
|
|
};
|
|
|
|
#endif /* INCLUDE_GUARD_TYPES_H */
|