皮卡魚源碼導讀
運行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 files else 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的是精簡版。

浙公網安備 33010602011771號