hash: Key() 改为只返回 st->key

并优化 analyze 打印。
将 Fen/Key 打印转移到 Position 的 <<
This commit is contained in:
Calcitem 2020-09-05 12:07:44 +08:00
parent 079d1bd7bb
commit 0a92670b60
6 changed files with 24 additions and 35 deletions

View File

@ -140,15 +140,11 @@ void AiThread::analyze(Color c)
string strUs = (c == BLACK ? "黑方" : "白方"); string strUs = (c == BLACK ? "黑方" : "白方");
string strThem = (c == BLACK ? "白方" : "黑方"); string strThem = (c == BLACK ? "白方" : "黑方");
loggerDebug("Depth: %d\n\n", ai.newDepth);
Position *pos = ai.position(); Position *pos = ai.position();
#ifdef TEST_MODE cout << *pos << "\n" << endl;
cout << *pos << endl;
#endif
#ifdef TEST_MODE
cout << "Key: " << std::hex << std::uppercase << pos->key() << endl;
#endif
switch (pos->get_phase()) switch (pos->get_phase())
{ {

View File

@ -116,10 +116,8 @@ std::ostream &operator<<(std::ostream &os, const Position &pos)
#undef P #undef P
#if 0
os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase
<< std::setfill('0') << std::setw(16) << pos.key(); << std::setfill('0') << std::setw(16) << pos.key();
#endif
return os; return os;
} }
@ -328,35 +326,26 @@ Position &Position::set(const string &code, Color c, StateInfo *si)
const string Position::fen() const const string Position::fen() const
{ {
// TODO
#if 0
int emptyCnt;
std::ostringstream ss; std::ostringstream ss;
for (Rank r = RANK_8; r >= RANK_1; --r) { for (File f = FILE_A; f <= FILE_C; f = (File)(f + 1)) {
for (File f = FILE_A; f <= FILE_C; ++f) { for (Rank r = RANK_1; r <= RANK_8; r = (Rank)(r + 1)) {
for (emptyCnt = 0; f <= FILE_C && empty(make_square(f, r)); ++f) ss << PieceToChar(piece_on(make_square(f, r)));
++emptyCnt;
if (emptyCnt)
ss << emptyCnt;
if (f <= FILE_C)
ss << PieceToChar[piece_on(make_square(f, r))];
} }
if (r > RANK_1) if (f == FILE_C) {
ss << '/'; ss << " ";
} else {
ss << "/";
}
} }
ss << (sideToMove == WHITE ? " w " : " b "); ss << (sideToMove == WHITE ? " w " : " b ");
ss << (" - ") // ss << (" - ")
<< st->rule50 << " " << 1 + (gamePly - (sideToMove == BLACK)) / 2; // << st->rule50 << " " << 1 + (gamePly - (sideToMove == BLACK)) / 2;
return ss.str(); return ss.str();
#endif
return "";
} }

View File

@ -95,7 +95,7 @@ public:
bool do_null_move(); bool do_null_move();
// Accessing hash keys // Accessing hash keys
Key key(); Key key() const;
void construct_key(); void construct_key();
Key revert_key(Square s); Key revert_key(Square s);
Key update_key(Square s); Key update_key(Square s);
@ -287,10 +287,9 @@ template<PieceType Pt> inline int Position::count(Color c) const
return 0; return 0;
} }
inline Key Position::key() inline Key Position::key() const
{ {
// TODO: Move to suitable function return st->key;
return update_key_misc();
} }
inline void Position::construct_key() inline void Position::construct_key()

View File

@ -380,7 +380,7 @@ Depth AIAlgorithm::changeDepth()
assert(d <= 32); assert(d <= 32);
loggerDebug("Depth: %d\n", d); //loggerDebug("Depth: %d\n", d);
return d; return d;
} }
@ -414,6 +414,7 @@ int AIAlgorithm::search()
Value value = VALUE_ZERO; Value value = VALUE_ZERO;
Depth d = changeDepth(); Depth d = changeDepth();
newDepth = d;
time_t time0 = time(nullptr); time_t time0 = time(nullptr);
srand(static_cast<unsigned int>(time0)); srand(static_cast<unsigned int>(time0));
@ -431,6 +432,7 @@ int AIAlgorithm::search()
static int nRepetition = 0; static int nRepetition = 0;
if (pos->get_phase() == PHASE_MOVING) { if (pos->get_phase() == PHASE_MOVING) {
pos->update_key_misc();
Key key = pos->key(); Key key = pos->key();
if (std::find(moveHistory.begin(), moveHistory.end(), key) != moveHistory.end()) { if (std::find(moveHistory.begin(), moveHistory.end(), key) != moveHistory.end()) {
@ -564,6 +566,7 @@ const char* AIAlgorithm::nextMove()
Endgame endgame; Endgame endgame;
endgame.type = state->position->playerSideToMove == PLAYER_BLACK ? endgame.type = state->position->playerSideToMove == PLAYER_BLACK ?
ENDGAME_PLAYER_WHITE_WIN : ENDGAME_PLAYER_BLACK_WIN; ENDGAME_PLAYER_WHITE_WIN : ENDGAME_PLAYER_BLACK_WIN;
position->update_key_misc();
key_t endgameHash = position->key(); // TODO: Do not generate hash repeately key_t endgameHash = position->key(); // TODO: Do not generate hash repeately
recordEndgameHash(endgameHash, endgame); recordEndgameHash(endgameHash, endgame);
} }
@ -707,6 +710,7 @@ Value search(Position *pos, Stack<Position> &ss, Depth depth, Depth originDepth,
#endif // TT_MOVE_ENABLE #endif // TT_MOVE_ENABLE
#if defined (TRANSPOSITION_TABLE_ENABLE) || defined(ENDGAME_LEARNING) #if defined (TRANSPOSITION_TABLE_ENABLE) || defined(ENDGAME_LEARNING)
pos->update_key_misc();
Key posKey = pos->key(); Key posKey = pos->key();
#endif #endif

View File

@ -178,7 +178,8 @@ public:
Value bestvalue { VALUE_ZERO }; Value bestvalue { VALUE_ZERO };
Value lastvalue { VALUE_ZERO }; Value lastvalue { VALUE_ZERO };
Depth originDepth{ 0 }; Depth originDepth { 0 };
Depth newDepth{ 0 };
inline Position *position() inline Position *position()
{ {

View File

@ -326,7 +326,7 @@ enum File : int
enum Rank : int enum Rank : int
{ {
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB = 8 RANK_1 = 1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB = 8
}; };