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());
57 Player pl=csa::charToPlayer(s.at(0));
58 Square fromPos=csa::strToPos(s.substr(1,2));
59 Square toPos=csa::strToPos(s.substr(3,2));
60 Ptype ptype=csa::strToPtype(s.substr(5,2));
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);
80show(
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";
106show(
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';
123show(
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);
void fill(const T_simple &value=T_simple())
bool isInvalid() const
state に apply 可能でない場合にtrue
Ptype capturePtype() const
bool isNormal() const
INVALID でも PASS でもない.
const Square from() const
Ptype getPtypeOf(int num) const
const char * getCsaName(Ptype ptype) const
bool isValidMove(Move move, bool show_error=true) const
合法手かどうかを検査する. isValidMoveByRule, isAlmostValidMove をおこなう. 玉の素抜きや王手を防いでいるか, 千日手,打歩詰かどうかは検査しない.
void setTurn(Player player)
void setPieceAll(Player player)
void setPiece(Player player, Square sq, Ptype ptype)
const Piece pieceAt(Square sq) const
bool isPieceStand() const
int y() const
将棋としてのY座標を返す.
int x() const
将棋としてのX座標を返す.
static bool parseLine(SimpleState &, RecordMinimal &, std::string element, CArray< bool, 9 > &)
virtual ~CsaFileMinimal()
RecordMinimal load() const
CsaString(const std::string &)
const Square strToPos(const std::string &s)
Ptype strToPtype(const std::string &s)
const std::string show(Move)
Player charToPlayer(char c)
const Move strToMove(const std::string &s, const SimpleState &st)
const std::string fancyShow(Move)
const PtypeTable Ptype_Table
const PieceTable Piece_Table
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に,promote前の型を返す. promoteしていない型の時はそのまま返す
bool isPromoted(Ptype ptype)
ptypeがpromote後の型かどうかのチェック
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
NumEffectState initial_state
std::vector< Move > moves