Skip to content

Commit

Permalink
Proposed rule to allow specific material situations to be searched for
Browse files Browse the repository at this point in the history
e.g QQ vs Q+RR. My understanding is that "material" and
"material-imbalance" don't do this.
  • Loading branch information
rozim committed Dec 3, 2017
1 parent 00cec13 commit c8eb593
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/scout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,22 @@ void search(Thread* th) {
while (*data != MOVE_NONE)
++data;
break;

case RuleMaterialSignature: {
bool ok = true;
for (const auto& it : cond->material_signature) {
Piece p = it.first;
int count = it.second;
if (popcount(pos.pieces(color_of(p), type_of(p))) != count) {
ok = false;
break;
}
}
if (ok) {
goto NextRule;
}
}
break;
}

// Do the move after rule checking
Expand Down Expand Up @@ -494,6 +510,38 @@ void parse_condition(Scout::Data& data, const json& item, int streakId = 0) {
cond.rules.push_back(RuleMaterial);
}

if (item.count("material-signature"))
{
for (std::string code : item["material-signature"])
{
bool white = true;
Piece last_piece = NO_PIECE;
for (size_t i = 0; i < code.size(); i++) {
char ch = code[i];
Piece p = NO_PIECE;
switch (ch) {
case 'K': p = (white ? W_KING : B_KING); break;
case 'Q': p = (white ? W_QUEEN : B_QUEEN); break;
case 'R': p = (white ? W_ROOK : B_ROOK); break;
case 'B': p = (white ? W_BISHOP : B_BISHOP); break;
case 'N': p = (white ? W_KNIGHT : B_KNIGHT); break;
case 'P': p = (white ? W_PAWN : B_PAWN); break;
case 'v': white = not white; break;
case '0':
assert(last_piece != NO_PIECE);
assert(last_piece != W_KING);
assert(last_piece != B_KING);
cond.material_signature[last_piece] = 0;
break;
}
if (p == NO_PIECE) { continue; }
last_piece = p;
cond.material_signature[p] += 1;
}
}
cond.rules.push_back(RuleMaterialSignature);
}

if (item.count("imbalance"))
{
for (std::string code : item["imbalance"])
Expand Down
10 changes: 9 additions & 1 deletion src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define SEARCH_H_INCLUDED

#include <atomic>
#include <map>
#include <sstream>
#include <vector>

Expand All @@ -44,7 +45,8 @@ enum ResultType {
enum RuleType {
RuleNone, RulePass, RuleResult, RuleResultType, RuleSubFen, RuleMaterial,
RuleImbalance, RuleMove, RuleQuietMove, RuleCapturedPiece, RuleMovedPiece,
RuleWhite, RuleBlack, RuleMatchedCondition, RuleMatchedQuery
RuleWhite, RuleBlack, RuleMatchedCondition, RuleMatchedQuery,
RuleMaterialSignature
};

struct SubFen {
Expand Down Expand Up @@ -76,6 +78,12 @@ struct Condition {
std::vector<ScoutMove> moves;
std::vector<Key> matKeys;
std::vector<Imbalance> imbalances;

// Piece -> # occurrances required.
// The value can be zero meaning the piece must be absent.
// If an entry is not present then the piece count
// is not checked for that piece.
std::map<Piece, int> material_signature;
};

struct MatchingGame {
Expand Down

1 comment on commit c8eb593

@gbtami
Copy link

@gbtami gbtami commented on c8eb593 Dec 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Dave!

Telling the truth I sent another PR to Marco at Feb 7 to fix mcostalba#41 and mcostalba#33 also but got no feedback. Later I asked him in a private email about scoutfish and chess_db and he replied he mainly lost some interest in these projects and he is OK with my forks using the same name. I just hoping he will have more time in the future to continue working on these great projects.
Anyhow, I like to see this enhancement in PyChess GUI, so may I ask you to send this PR to https://github.com/gbtami/scoutfish also, please?

Please sign in to comment.