facade.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
c/facade.cc
Go to the documentation of this file.
1 /* facade.c
2 */
3 #include "osl/c/facade.h"
4 #include "osl/checkmate/dualDfpn.h"
5 #include "osl/game_playing/alphaBetaPlayer.h"
6 #include "osl/game_playing/gameState.h"
7 #include "osl/game_playing/usiState.h"
8 #include "osl/game_playing/usiResponse.h"
9 #include "osl/search/simpleHashTable.h"
10 #include "osl/search/simpleHashRecord.h"
11 #include "osl/record/csaString.h"
12 #include "osl/record/csa.h"
13 #include "osl/record/kanjiPrint.h"
14 #include "osl/record/usi.h"
15 #include <boost/make_shared.hpp>
16 #include <string>
17 #include <cstdio>
18 #include <cstring>
19
20 extern "C"
21 void osl_init()
22 {
23 osl::OslConfig::setUp();
24 }
25
26 extern "C"
27 int checkmate_attack(const char *state_str, int& limit, char *move)
28 {
29 osl::DualDfpn checkmate;
30 osl::Move checkmate_move;
31 osl::NumEffectState state(osl::CsaString(state_str).getInitialState());
32 osl::HashKey key(state);
33 osl::PathEncoding pe(state.turn());
34 const bool win = checkmate.isWinningState(limit, state, key, pe,
35 checkmate_move);
36 limit = checkmate.totalNodeCount();
37 if (win) {
38 const std::string checkmate_move_str =
39 osl::record::csa::show(checkmate_move);
40 sprintf(move, "%s", checkmate_move_str.c_str());
41 }
42 return win;
43 }
44
45 extern "C"
46 int checkmate_escape(const char *state_str, int limit)
47 {
48 osl::DualDfpn checkmate;
49 osl::Move checkmate_move;
50 osl::NumEffectState state(osl::CsaString(state_str).getInitialState());
51 osl::HashKey key(state);
52 osl::PathEncoding pe(state.turn());
53 const bool escape = checkmate.isLosingState(limit, state, key, pe);
54 return escape;
55 }
56
57 extern "C"
58 int search(const char *state_str, int seconds, int verbose, char *move)
59 {
60 osl::game_playing::AlphaBeta2OpenMidEndingEvalPlayer player;
61 player.setNextIterationCoefficient(1.7);
62 player.setVerbose(verbose);
63 if (osl::OslConfig::isMemoryLimitEffective())
64 {
65 player.setTableLimit(std::numeric_limits<size_t>::max(), 200);
66 player.setNodeLimit(std::numeric_limits<size_t>::max());
67 }
68 else
69 {
70 player.setTableLimit(3000000, 200);
71 }
72 player.setDepthLimit(2000, 400, 200);
73
74 osl::game_playing::GameState state(osl::CsaString(state_str).getInitialState());
75 osl::Move best_move = player.searchWithSecondsForThisMove(state, osl::search::TimeAssigned(osl::MilliSeconds::Interval(seconds*1000))).move;
76
77 const std::string best_move_str = osl::record::csa::show(best_move);
78 sprintf(move, "%s", best_move_str.c_str());
79
80 const osl::SimpleHashTable *table = player.table();
81 const osl::HashKey key(state.state());
82
83 const osl::SimpleHashRecord *record = table->find(key);
84 int value = record ? record->lowerBound() : 0;
85 return value;
86 }
87
88 extern "C"
89 int usiMovesToKanji(const char *command, char *out, int out_size)
90 {
91 assert(out_size>0);
92 osl::game_playing::UsiState usi_state;
93 osl::game_playing::UsiResponse res(usi_state, true, false);
94 std::string ret;
95
96 res.hasImmediateResponse(std::string(command), ret);
97
98 const int size = std::min(out_size, static_cast<int>(ret.size()));
99 memcpy(out, ret.c_str(), size);
100 return size;
101 }
102
103 extern "C"
104 int usiMovesToPositionString(const char *moves_str, char *out, int out_size)
105 {
106 assert(out_size>0);
107
108 osl::NumEffectState state;
109 osl::vector<osl::Move> moves;
110 std::istringstream is(moves_str);
111 std::string s;
112 while (is >> s) {
113 const osl::Move move = osl::record::usi::strToMove(s, state);
114 moves.push_back(move);
115 state.makeMove(move);
116 }
117 assert(!moves.empty());
118
119 osl::Move last_move;
120 if (! moves.empty()) {
121 last_move = moves.back();
122 }
123
124 std::ostringstream os;
125 osl::record::KanjiPrint printer(os, boost::make_shared<osl::record::KIFCharacters>());
126 printer.print(state, &last_move);
127
128 const std::string ret = os.str();
129 const int size = std::min(out_size, static_cast<int>(ret.size()));
130 memcpy(out, ret.c_str(), size);
131 return size;
132 }
133
134 /* ------------------------------------------------------------------------- */
135 // ;;; Local Variables:
136 // ;;; mode:c++
137 // ;;; c-basic-offset:2
138 // ;;; End: