00001 #include "osl/record/csa.h"
00002 #include "osl/record/csaIOError.h"
00003 #include "osl/state/simpleState.h"
00004 #include "osl/pieceTable.h"
00005 #include <iostream>
00006 #include <stdexcept>
00007 #include <cassert>
00008 #include <string>
00009
00010
00011
00012 osl::Player osl::record::csa::
00013 charToPlayer(char c)
00014 {
00015 if(c=='+')
00016 return BLACK;
00017 if(c=='-')
00018 return WHITE;
00019 std::cerr << "oops " << (int)c << "\n";
00020 throw CsaIOError("not a csa PlayerCharacter "+std::string(1,c));
00021 }
00022
00023 const osl::Position osl::record::csa::
00024 strToPos(const std::string& s)
00025 {
00026 int x=s.at(0)-'0';
00027 int y=s.at(1)-'0';
00028 if(x==0 && y==0)
00029 return Position::STAND();
00030 return Position(x,y);
00031 }
00032
00033 osl::Ptype osl::record::csa::
00034 strToPtype(const std::string& s)
00035 {
00036 for(int i=0;i<16;i++){
00037 if(s == Ptype_Table.getCsaName(static_cast<Ptype>(i)))
00038 return static_cast<Ptype>(i);
00039 }
00040 throw CsaIOError("unknown std::string in csa::strToPtype "+s);
00041 }
00042
00043 const osl::Move osl::record::csa::
00044 strToMove(const std::string& s,const SimpleState& state)
00045 {
00046 Player pl=csa::charToPlayer(s.at(0));
00047 Position fromPos=csa::strToPos(s.substr(1,2));
00048 Position toPos=csa::strToPos(s.substr(3,2));
00049 Ptype ptype=csa::strToPtype(s.substr(5,2));
00050 if(fromPos==Position::STAND()){
00051 if (isPromoted(ptype))
00052 throw CsaIOError("drop with promote ?! in csa::strToMove "+s);
00053 return Move(toPos,ptype,pl);
00054 }
00055 else{
00056 Piece p0=state.getPieceAt(fromPos);
00057 Piece p1=state.getPieceAt(toPos);
00058 Ptype capturePtype=p1.ptype();
00059 bool isPromote=(p0.ptype()!=ptype);
00060 if (! ((p0.ptype()==ptype)||(p0.ptype()==unpromote(ptype))))
00061 throw CsaIOError("bad promotion in csa::strToMove "+s);
00062 return Move(fromPos,toPos,ptype,
00063 capturePtype,isPromote,pl);
00064 }
00065 }
00066
00067
00068 const std::string osl::record::csa::
00069 show(Player player, std::string& buf, size_t offset)
00070 {
00071 assert(buf.size() >= offset+1);
00072 buf[offset] = (player==BLACK) ? '+' : '-';
00073 return buf;
00074 }
00075
00076 const std::string osl::record::csa::
00077 show(Move move, std::string& buf)
00078 {
00079 assert(buf.capacity() >= 7);
00080 buf.resize(7);
00081 if (move == Move::DeclareWin())
00082 return buf = "%KACHI";
00083 if (move.isInvalid())
00084 return buf = "%TORYO";
00085 if (move.isPass())
00086 return buf = "%PASS";
00087 show(move.player(), buf);
00088 show(move.from(), buf, 1);
00089 show(move.to(), buf, 3);
00090 show(move.ptype(), buf, 5);
00091 return buf;
00092 }
00093
00094 const std::string osl::record::csa::
00095 show(Position pos, std::string& buf, size_t offset)
00096 {
00097 assert(buf.size() >= offset+2);
00098 if (pos.isPieceStand())
00099 {
00100 buf[0+offset] = '0';
00101 buf[1+offset] = '0';
00102 return buf;
00103 }
00104 const int x = pos.x();
00105 const int y = pos.y();
00106 buf[offset+0] = x + '0';
00107 buf[offset+1] = y + '0';
00108 return buf;
00109 }
00110
00111 const std::string osl::record::csa::
00112 show(Ptype ptype, std::string& buf, size_t offset)
00113 {
00114 assert(buf.size() >= offset+2);
00115 const char *name = Ptype_Table.getCsaName(ptype);
00116 buf[0+offset] = name[0];
00117 buf[1+offset] = name[1];
00118 return buf;
00119 }
00120
00121 const std::string osl::record::csa::
00122 show(Move move)
00123 {
00124
00125 std::string buf("+7776FU");
00126 return show(move, buf);
00127 }
00128
00129 const std::string osl::record::csa::
00130 show(Player player)
00131 {
00132 std::string buf("+");
00133 return show(player, buf);
00134 }
00135
00136 const std::string osl::record::csa::
00137 show(Position position)
00138 {
00139 std::string buf("00");
00140 return show(position, buf);
00141 }
00142
00143 const std::string osl::record::csa::
00144 show(Ptype ptype)
00145 {
00146 std::string buf("OU");
00147 return show(ptype, buf);
00148 }
00149
00150 const std::string osl::record::csa::
00151 show(Piece piece)
00152 {
00153 if (piece.isEdge())
00154 return " ";
00155 if (piece.isEmpty())
00156 return " * ";
00157
00158 assert(piece.isPiece() && isPiece(piece.ptype()));
00159 assert(unpromote(piece.ptype()) == Piece_Table.getPtypeOf(piece.number()));
00160 return show(piece.owner())
00161 + show(piece.ptype());
00162 }
00163
00164
00165
00166 std::ostream& osl::csaShow(std::ostream& os,const Position pos)
00167 {
00168 return os << record::csa::show(pos);
00169 }
00170
00171 std::ostream& osl::csaShow(std::ostream& os, const Piece piece)
00172 {
00173 return os << record::csa::show(piece);
00174 }
00175
00176 std::ostream& osl::csaShow(std::ostream& os,const osl::Ptype ptype)
00177 {
00178 return os << record::csa::show(ptype);
00179 }
00180
00181 std::ostream& osl::csaShow(std::ostream& os,const Move move)
00182 {
00183 return os << record::csa::show(move);
00184 }
00185
00186
00187
00188
00189
00190