运行pikafish后输入d命令:
rook:车; knight:马; bishop:象; advisor:士; king:将; cannon:炮; pawn:卒。红棋用大写字母。红方叫白方。
棋盘可以看作9x10的交叉点,也可看作方格(square).
对应代码是position.cpp里的std::ostream& operator<<(std::ostream& os, const Position& pos),不是bitboard.cpp里的pretty,后者没人调用。
先看types.h。enum Square : int8_t { SQ_A0 ... SQ_I9, SQUARE_NB = 90 } NB是number的意思:…的个数。
enum Direction : int8_t { EAST = 1, NORTH = 9, SOUTH = -NORTH } 上北下南,左西右东。
// Initializes the position object with the given FEN string. Position& Position::set(const string& fenStr, StateInfo* si) {unsigned char token;size_t idx;Square sq = SQ_A9;std::istringstream ss(fenStr);while ((ss >> token) && !isspace(token)) {if (isdigit(token)) sq += (token - '0') * EAST; // Advance the given number of fileselse if (token == '/') sq += 2 * SOUTH;else if ((idx = PieceToChar.find(token)) != string::npos) put_piece(Piece(idx), sq);} }
过度花哨了。* 1怎么说?stringstream包string包char*?
Piece是棋子。File是列,rank是行,和row首字母都是r. rank有军衔的意思,可联想兵到底线升变。
constexpr std::string_view PieceToChar(" RACPNBK racpnbk");
string_view是C++17引入的轻量级字符串视图类,定义于<string_view>中。它提供对字符串数据的只读访问,仅存储指针和长度信息。
① 这个函数没必要优化。
② 最快的是像ctype.h那样放在T [256]的表里,T是char好还是int好?还是自己操作位?
Position::put_piece(Piece pc, Square s) {
board[s] = pc;
byTypeBB[ALL_PIECES] |= byTypeBB[type_of(pc)] |= s;
byColorBB[color_of(pc)] |= s;
pieceCount[pc]++;
}
enum Piece std::int8_t {...}
Piece board[SQUARE_NB];
typedef __uint128_t Bitboard;
// 实际为using Bitboard = ...,处理Linux和Windows不同的情况。Linux下啥头文件都不用包含。
enum PieceType std::int8_t { ROOK, ..., PIECE_TYPE_NB = 8 }
Bitboard byTypeBB[PIECE_TYPE_NB];
enum Color : int8_t { WHITE, BLACK, COLOR_NB = 2 };
Bitboard byColorBB[COLOR_NB];
每方有7种、16个棋子。补充type_of和color_of:
constexpr PieceType type_of(Piece pc) { return PieceType(pc & 7); }constexpr Color color_of(Piece pc) {assert(pc != NO_PIECE);return Color(pc >> 3); }
与GNU chess相比:
① 虽然我都看不懂,但感觉皮卡鱼赏心悦目,可读性强。在misc.cpp里作者提到了"Our fancy logging facility"; 也许极个别别的地方也有点fancy.
② 搜索部分比较简明。NNUE的online部分1000来行。
③ 速度极快的xxHash还有汇编写的Huffman解码可以先不看。Meta有基础研究科学家,腾讯呢?
④ 皮卡鱼(基于Stockfish)棋力强。
⑤ 带数据。43M的pikafish.nnue是压缩后,7z一点都再压缩不动。Top CPU Contributors里有个Contributors for training data generation with >10,000,000 positions generated, as of 2023-02-16. 不是说拿1千万个局面训练的,是1千万是上榜条件(157人上榜),榜首kaka是452524769438/1e8 > 4525。Contributors to Fishtest with >10,000 CPU hours, as of 2023-07-31. 榜首浮生若梦5029923 (574.2年?)。也许43M的是精简版。
晚上睡不着?可拿平板装QuickEdit看源码,也可听无聊的英语,TTSMAKER可以转AAC,比如:
The year 1066 stands as a pivotal moment in British history, marking the Norman Conquest and the Battle of Hastings. Under the leadership of William the Conqueror, the Normans defeated the Anglo-Saxon King Harold II, reshaping England's political, cultural, and linguistic landscape. This event not only established Norman rule but also introduced feudalism to England, leaving a lasting legacy in its legal systems, architecture, and language. The Battle of Hastings thus remains a defining chapter in the nation's medieval history.
((⏱️=2000))