Go to the documentation of this file.00001
00002
00003 #include "osl/c/facade.h"
00004 #include "osl/checkmate/dualDfpn.h"
00005 #include "osl/game_playing/alphaBetaPlayer.h"
00006 #include "osl/game_playing/gameState.h"
00007 #include "osl/game_playing/usiState.h"
00008 #include "osl/game_playing/usiResponse.h"
00009 #include "osl/search/simpleHashTable.h"
00010 #include "osl/search/simpleHashRecord.h"
00011 #include "osl/record/csaString.h"
00012 #include "osl/record/csa.h"
00013 #include "osl/record/kanjiPrint.h"
00014 #include "osl/record/usi.h"
00015 #include <boost/make_shared.hpp>
00016 #include <string>
00017 #include <cstdio>
00018 #include <cstring>
00019
00020 extern "C"
00021 void osl_init()
00022 {
00023 osl::OslConfig::setUp();
00024 }
00025
00026 extern "C"
00027 int checkmate_attack(const char *state_str, int& limit, char *move)
00028 {
00029 osl::DualDfpn checkmate;
00030 osl::Move checkmate_move;
00031 osl::NumEffectState state(osl::CsaString(state_str).getInitialState());
00032 osl::HashKey key(state);
00033 osl::PathEncoding pe(state.turn());
00034 const bool win = checkmate.isWinningState(limit, state, key, pe,
00035 checkmate_move);
00036 limit = checkmate.totalNodeCount();
00037 if (win) {
00038 const std::string checkmate_move_str =
00039 osl::record::csa::show(checkmate_move);
00040 sprintf(move, "%s", checkmate_move_str.c_str());
00041 }
00042 return win;
00043 }
00044
00045 extern "C"
00046 int checkmate_escape(const char *state_str, int limit)
00047 {
00048 osl::DualDfpn checkmate;
00049 osl::Move checkmate_move;
00050 osl::NumEffectState state(osl::CsaString(state_str).getInitialState());
00051 osl::HashKey key(state);
00052 osl::PathEncoding pe(state.turn());
00053 const bool escape = checkmate.isLosingState(limit, state, key, pe);
00054 return escape;
00055 }
00056
00057 extern "C"
00058 int search(const char *state_str, int seconds, int verbose, char *move)
00059 {
00060 osl::game_playing::AlphaBeta2OpenMidEndingEvalPlayer player;
00061 player.setNextIterationCoefficient(1.7);
00062 player.setVerbose(verbose);
00063 if (osl::OslConfig::isMemoryLimitEffective())
00064 {
00065 player.setTableLimit(std::numeric_limits<size_t>::max(), 200);
00066 player.setNodeLimit(std::numeric_limits<size_t>::max());
00067 }
00068 else
00069 {
00070 player.setTableLimit(3000000, 200);
00071 }
00072 player.setDepthLimit(2000, 400, 200);
00073
00074 osl::game_playing::GameState state(osl::CsaString(state_str).getInitialState());
00075 osl::Move best_move = player.searchWithSecondsForThisMove(state, osl::search::TimeAssigned(osl::MilliSeconds::Interval(seconds*1000))).move;
00076
00077 const std::string best_move_str = osl::record::csa::show(best_move);
00078 sprintf(move, "%s", best_move_str.c_str());
00079
00080 const osl::SimpleHashTable *table = player.table();
00081 const osl::HashKey key(state.state());
00082
00083 const osl::SimpleHashRecord *record = table->find(key);
00084 int value = record ? record->lowerBound() : 0;
00085 return value;
00086 }
00087
00088 extern "C"
00089 int usiMovesToKanji(const char *command, char *out, int out_size)
00090 {
00091 assert(out_size>0);
00092 osl::game_playing::UsiState usi_state;
00093 osl::game_playing::UsiResponse res(usi_state, true, false);
00094 std::string ret;
00095
00096 res.hasImmediateResponse(std::string(command), ret);
00097
00098 const int size = std::min(out_size, static_cast<int>(ret.size()));
00099 memcpy(out, ret.c_str(), size);
00100 return size;
00101 }
00102
00103 extern "C"
00104 int usiMovesToPositionString(const char *moves_str, char *out, int out_size)
00105 {
00106 assert(out_size>0);
00107
00108 osl::NumEffectState state;
00109 osl::vector<osl::Move> moves;
00110 std::istringstream is(moves_str);
00111 std::string s;
00112 while (is >> s) {
00113 const osl::Move move = osl::record::usi::strToMove(s, state);
00114 moves.push_back(move);
00115 state.makeMove(move);
00116 }
00117 assert(!moves.empty());
00118
00119 osl::Move last_move;
00120 if (! moves.empty()) {
00121 last_move = moves.back();
00122 }
00123
00124 std::ostringstream os;
00125 osl::record::KanjiPrint printer(os, boost::make_shared<osl::record::KIFCharacters>());
00126 printer.print(state, &last_move);
00127
00128 const std::string ret = os.str();
00129 const int size = std::min(out_size, static_cast<int>(ret.size()));
00130 memcpy(out, ret.c_str(), size);
00131 return size;
00132 }
00133
00134
00135
00136
00137
00138