Skip to content

Commit

Permalink
Initial development for variant descriptions (#65)
Browse files Browse the repository at this point in the history
Add command to print description of game rules, e.g.:
`rules chess`
  • Loading branch information
ianfab committed Jan 12, 2020
1 parent 9aff700 commit a01ea03
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ namespace {
Options["VariantPath"] = token;
}

// rules() prints the rules of the given variant.

void rules(istringstream& is) {

string token;
if (is >> token && variants.find(token) != variants.end())
sync_cout << variants.find(token) << sync_endl;
else
sync_cout << "No such variant: " << token << sync_endl;
}

} // namespace


Expand Down Expand Up @@ -276,6 +287,7 @@ void UCI::loop(int argc, char* argv[]) {
else if (token == "d") sync_cout << pos << sync_endl;
else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
else if (token == "load") { load(is); argc = 1; } // continue reading stdin
else if (token == "rules") rules(is);
else
sync_cout << "Unknown command: " << cmd << sync_endl;

Expand Down
36 changes: 36 additions & 0 deletions src/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "parser.h"
#include "piece.h"
#include "position.h"
#include "thread.h"
#include "variant.h"

using std::string;
Expand Down Expand Up @@ -962,3 +964,37 @@ std::vector<std::string> VariantMap::get_keys() {
keys.push_back(element.first);
return keys;
}

std::string value_to_result(Value value) {
return value == VALUE_MATE ? "win"
: value == -VALUE_MATE ? "loss"
: "draw";
}

std::ostream& operator<<(std::ostream& os, const VariantMap::iterator& variantIterator) {
std::string variantName = variantIterator->first;
variantName[0] = toupper(variantName[0]);
const Variant* v = variantIterator->second;
StateInfo st;
Position pos;
pos.set(v, v->startFen, v->chess960, &st, Threads.main());

os << variantName << " is a chess variant played on a board of " << v->maxFile + 1 << " x " << v->maxRank + 1 << " squares." << std::endl;

// diagram
os << pos << std::endl << std::endl;

os << "# Game end" << std::endl << std::endl;
// checkmate
if (v->checking && v->pieceTypes.find(KING) != v->pieceTypes.end())
os << "Checkmating the opponent's king leads to a "
<< value_to_result(-v->checkmateValue)
<< " for the the player who delivered the checkmate." << std::endl << std::endl;

// stalemate
os << "Stalemate is adjudicated as a " << value_to_result(v->stalemateValue) << " for player who can not move." << std::endl;

// TODO: implement more options

return os;
}
2 changes: 2 additions & 0 deletions src/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ class VariantMap : public std::map<std::string, const Variant*> {

extern VariantMap variants;

extern std::ostream& operator<<(std::ostream& os, const VariantMap::iterator& variantIterator);

#endif // #ifndef VARIANT_H_INCLUDED
13 changes: 13 additions & 0 deletions src/xboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ namespace {
moveList.pop_back();
}

// rules() prints the rules of the given variant.

void rules(std::istringstream& is) {

std::string token;
if (is >> token && variants.find(token) != variants.end())
sync_cout << variants.find(token) << sync_endl;
else
sync_cout << "Error (unkown command): " << token << sync_endl;
}

} // namespace

namespace XBoard {
Expand Down Expand Up @@ -277,6 +288,8 @@ void StateMachine::process_command(Position& pos, std::string token, std::istrin
sync_cout << pos << sync_endl;
else if (token == "eval")
sync_cout << Eval::trace(pos) << sync_endl;
else if (token == "rules")
rules(is);
// Move strings and unknown commands
else
{
Expand Down

0 comments on commit a01ea03

Please sign in to comment.