5 #include <boost/algorithm/string/split.hpp>
6 #include <boost/algorithm/string/trim.hpp>
24 throw CsaIOError(
"not a csa PlayerCharacter "+std::string(1,c));
33 return Square::STAND();
40 for(
int i=0;i<16;i++){
42 return static_cast<Ptype>(i);
44 throw CsaIOError(
"unknown std::string in csa::strToPtype "+s);
51 return Move::DeclareWin();
53 return Move::INVALID();
55 return Move::PASS(state.
turn());
61 if(fromPos==Square::STAND()){
63 throw CsaIOError(
"drop with promote ?! in csa::strToMove "+s);
64 return Move(toPos,ptype,pl);
70 bool isPromote=(p0.
ptype()!=ptype);
72 throw CsaIOError(
"bad promotion in csa::strToMove "+s);
73 return Move(fromPos,toPos,ptype,
74 capturePtype,isPromote,pl);
80 show(
Player player, std::string& buf,
size_t offset)
82 assert(buf.size() >= offset+1);
83 buf[offset] = (player==
BLACK) ?
'+' :
'-';
90 assert(buf.capacity() >= 7);
92 if (move == Move::DeclareWin())
93 return buf =
"%KACHI";
95 return buf =
"%TORYO";
106 show(
Square pos, std::string& buf,
size_t offset)
108 assert(buf.size() >= offset+2);
115 const int x = pos.
x();
116 const int y = pos.
y();
117 buf[offset+0] = x +
'0';
118 buf[offset+1] = y +
'0';
123 show(
Ptype ptype, std::string& buf,
size_t offset)
125 assert(buf.size() >= offset+2);
127 buf[0+offset] = name[0];
128 buf[1+offset] = name[1];
136 std::string buf(
"+7776FU");
137 return show(move, buf);
143 std::string ret =
show(move);
156 std::string buf(
"+");
157 return show(player, buf);
163 std::string buf(
"00");
164 return show(position, buf);
170 std::string buf(
"OU");
171 return show(ptype, buf);
191 std::ostringstream out;
192 for (; first != last; ++first) {
203 std::ifstream is(filename);
205 const std::string msg =
"CsaFileMinimal::CsaFileMinimal file open failed ";
206 std::cerr << msg << filename <<
"\n";
226 while (std::getline(is, line))
230 && (line[line.size()-1] == 13))
231 line.erase(line.size()-1);
233 std::vector<std::string> elements;
234 boost::algorithm::split(elements, line, boost::algorithm::is_any_of(
","));
235 for (
auto& e: elements) {
236 boost::algorithm::trim(e);
237 boost::algorithm::trim_left(e);
238 parseLine(work, record, e, board_parsed);
241 if (*std::min_element(board_parsed.
begin(), board_parsed.
end()) ==
false)
242 throw CsaIOError(
"incomplete position description in csaParseLine");
243 assert(record.initial_state.isConsistent());
250 while (! s.empty() && isspace(s[s.size()-1]))
251 s.resize(s.size()-1);
258 board_parsed.
fill(
true);
264 for(
int i=2;i<=(int)s.length()-4;i+=4){
266 if(s.substr(i+2,2) ==
"AL"){
277 if(isdigit(s.at(1))){
278 const int y=s.at(1)-
'0';
279 board_parsed[y-1] =
true;
280 for(
unsigned int x=9,i=2;i<s.length();i+=3,x--){
281 if (s.at(i) !=
'+' && s.at(i) !=
'-' && s.find(
" *",i)!=i) {
285 std::cerr <<
"possible typo for empty square " << s <<
"\n";
287 if (s.at(i) !=
'+' && s.at(i) !=
'-')
continue;
308 std::cerr <<
"Illegal move " << m << std::endl;
311 record.
moves.push_back(m);
327 std::istringstream is(s);