7 #include <boost/algorithm/string/replace.hpp>
8 #include <boost/foreach.hpp>
19 if (m == Move::DeclareWin())
48 show(
const SimpleState& state)
50 std::ostringstream ret;
51 if (state == SimpleState(
HIRATE)) {
56 for (
int y=1; y<=9; ++y) {
58 for (
int x=9; x>=1; --x) {
72 if (y < 9) ret <<
"/";
74 ret <<
" " <<
"bw"[state.turn() ==
WHITE] <<
" ";
76 for (
int z=0; z<2; ++z) {
79 const int count = state.countPiecesOnStand(player, ptype);
95 strToMove(
const std::string& str,
const SimpleState& s)
98 return Move::DeclareWin();
100 return Move::PASS(s.turn());
102 return Move::INVALID();
106 catch (std::exception& e) {
107 throw ParseError(
"usi::strToMove failed for " + str +
" by "+ e.what());
110 throw ParseError(
"usi::strToMove failed for " + str);
119 throw ParseError(
"Invalid piece character: " + c);
127 throw ParseError(word);
131 for (
size_t i=0; i<word.size(); ++i) {
132 const char c = word[i];
137 }
else if (c ==
'+') {
138 if ( (i+1) >= word.size() )
139 throw ParseError(word);
140 const char next = word[i+1];
142 throw ParseError(word);
145 throw ParseError(word);
150 }
else if (c ==
'/') {
152 throw ParseError(word);
155 }
else if (isdigit(c)) {
156 const int n = c -
'0';
158 throw ParseError(word);
161 throw ParseError(
"usi: unknown input " + c);
163 if (x < 0 || x > 9 || y < 0 || y > 9)
164 throw ParseError(word);
172 parse(line, board, moves);
173 state.copyFrom(NumEffectState(board));
174 BOOST_FOREACH(Move move, moves) {
175 state.makeMove(move);
188 std::istringstream is(line);
191 if (word ==
"position")
193 if (word ==
"startpos")
197 throw ParseError(
"sfen not found "+word);
201 if (word !=
"b" && word !=
"w")
202 throw ParseError(
" turn error "+word);
207 BOOST_FOREACH(
char c, word) {
210 for (
int j=0; j<
std::max(1, prefix); ++j)
216 throw ParseError(word);
217 prefix = (c -
'0') + prefix*10;
219 throw ParseError(word);
223 state.initPawnMask();
225 if (! (is >> move_number))
232 throw ParseError(
"moves not found "+word);
233 NumEffectState state_copy(state);
237 if (! m.isNormal() || ! state_copy.isValidMove(m))
238 throw ParseError(
"invalid move "+word);
239 state_copy.makeMove(m);
246 boost::algorithm::replace_all(str,
"/",
"_");
247 boost::algorithm::replace_all(str,
"+",
"@");
248 boost::algorithm::replace_all(str,
" ",
".");
254 boost::algorithm::replace_all(str,
"_",
"/");
255 boost::algorithm::replace_all(str,
"@",
"+");
256 boost::algorithm::replace_all(str,
".",
" ");
263 std::ifstream is(filename.c_str());
265 if (! std::getline(is, line))
267 const std::string msg =
"UsiFile::UsiFile file cannot read ";
268 std::cerr << msg << filename <<
"\n";
273 parse(line, initial, moves);
274 assert(initial.isConsistent());
275 record.setInitialState(initial);
279 BOOST_FOREACH(
Move move, moves)
297 return NumEffectState(record.getInitialState());