csa.cc
Go to the documentation of this file.
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 #include <sstream>
00010 
00011 /* ------------------------------------------------------------------------- */
00012 
00013 osl::Player osl::record::csa::
00014 charToPlayer(char c)
00015 {
00016   if(c=='+') 
00017     return BLACK;
00018   if(c=='-') 
00019     return WHITE;
00020   std::cerr << "oops " << (int)c << "\n";
00021   throw CsaIOError("not a csa PlayerCharacter "+std::string(1,c));
00022 }
00023 
00024 const osl::Square osl::record::csa::
00025 strToPos(const std::string& s)
00026 {
00027   int x=s.at(0)-'0';
00028   int y=s.at(1)-'0';
00029   if(x==0 && y==0) 
00030     return Square::STAND();
00031   return Square(x,y);
00032 }
00033 
00034 osl::Ptype osl::record::csa::
00035 strToPtype(const std::string& s)
00036 {
00037   for(int i=0;i<16;i++){
00038     if(s == Ptype_Table.getCsaName(static_cast<Ptype>(i))) 
00039       return static_cast<Ptype>(i);
00040   }
00041   throw CsaIOError("unknown std::string in csa::strToPtype "+s);
00042 }
00043 
00044 const osl::Move osl::record::csa::
00045 strToMove(const std::string& s,const SimpleState& state)
00046 {
00047   if (s == "%KACHI")
00048     return Move::DeclareWin();
00049   if (s == "%TORYO")
00050     return Move::INVALID();
00051   if (s == "%PASS")             // FIXME: not in CSA protocol
00052     return Move::PASS(state.turn());
00053 
00054   Player pl=csa::charToPlayer(s.at(0));
00055   Square fromPos=csa::strToPos(s.substr(1,2));
00056   Square toPos=csa::strToPos(s.substr(3,2));
00057   Ptype ptype=csa::strToPtype(s.substr(5,2));
00058   if(fromPos==Square::STAND()){
00059     if (isPromoted(ptype))
00060       throw CsaIOError("drop with promote ?! in csa::strToMove "+s);
00061     return Move(toPos,ptype,pl);
00062   }
00063   else{
00064     Piece p0=state.pieceAt(fromPos);
00065     Piece p1=state.pieceAt(toPos);
00066     Ptype capturePtype=p1.ptype();
00067     bool isPromote=(p0.ptype()!=ptype);
00068     if (! ((p0.ptype()==ptype)||(p0.ptype()==unpromote(ptype))))
00069       throw CsaIOError("bad promotion in csa::strToMove "+s);
00070     return Move(fromPos,toPos,ptype,
00071                 capturePtype,isPromote,pl);
00072   }
00073 }
00074 
00075 /* ------------------------------------------------------------------------- */
00076 const std::string osl::record::csa::
00077 show(Player player, std::string& buf, size_t offset)
00078 {
00079   assert(buf.size() >= offset+1);
00080   buf[offset] = (player==BLACK) ? '+' : '-';
00081   return buf;
00082 }
00083 
00084 const std::string osl::record::csa::
00085 show(Move move, std::string& buf)
00086 {
00087   assert(buf.capacity() >= 7);
00088   buf.resize(7);
00089   if (move == Move::DeclareWin())
00090     return buf = "%KACHI";
00091   if (move.isInvalid())
00092     return buf = "%TORYO";
00093   if (move.isPass())
00094     return buf = "%PASS";               // FIXME: not in CSA protocol
00095   show(move.player(), buf);
00096   show(move.from(), buf, 1);
00097   show(move.to(), buf, 3);
00098   show(move.ptype(), buf, 5);
00099   return buf;
00100 }
00101 
00102 const std::string osl::record::csa::
00103 show(Square pos, std::string& buf, size_t offset)
00104 {
00105   assert(buf.size() >= offset+2);
00106   if (pos.isPieceStand()) 
00107   {
00108     buf[0+offset] = '0';
00109     buf[1+offset] = '0';
00110     return buf;
00111   }
00112   const int x = pos.x();
00113   const int y = pos.y();
00114   buf[offset+0] = x + '0';
00115   buf[offset+1] = y + '0';
00116   return buf;
00117 }
00118 
00119 const std::string osl::record::csa::
00120 show(Ptype ptype, std::string& buf, size_t offset)
00121 {
00122   assert(buf.size() >= offset+2);
00123   const char *name = Ptype_Table.getCsaName(ptype);
00124   buf[0+offset] = name[0];
00125   buf[1+offset] = name[1];
00126   return buf;
00127 }
00128 
00129 const std::string osl::record::csa::
00130 show(Move move)
00131 {
00132   // NOTE: copy コピーを返すので dangling pointer ではない
00133   std::string buf("+7776FU");
00134   return show(move, buf);
00135 }
00136 
00137 const std::string osl::record::csa::
00138 fancyShow(Move move)
00139 {
00140   std::string ret = show(move);
00141   if (move.isNormal()) {
00142     if (move.capturePtype() != PTYPE_EMPTY)
00143       ret += "x" + show(move.capturePtype());
00144     if (move.isPromotion())
00145       ret += '+';
00146   }
00147   return ret;
00148 }
00149 
00150 const std::string osl::record::csa::
00151 show(Player player)
00152 {
00153   std::string buf("+");
00154   return show(player, buf);
00155 }
00156 
00157 const std::string osl::record::csa::
00158 show(Square position)
00159 {
00160   std::string buf("00");
00161   return show(position, buf);
00162 }
00163 
00164 const std::string osl::record::csa::
00165 show(Ptype ptype)
00166 {
00167   std::string buf("OU");
00168   return show(ptype, buf);
00169 }
00170 
00171 const std::string osl::record::csa::
00172 show(Piece piece)
00173 {
00174   if (piece.isEdge())
00175     return "   ";
00176   if (piece.isEmpty())
00177     return " * ";
00178 
00179   assert(piece.isPiece() && isPiece(piece.ptype()));
00180   assert(unpromote(piece.ptype()) == Piece_Table.getPtypeOf(piece.number()));
00181   return show(piece.owner()) 
00182     + show(piece.ptype());
00183 }
00184 
00185 const std::string osl::record::csa::
00186 show(const Move *first, const Move *last)
00187 {
00188   std::ostringstream out;
00189   for (; first != last; ++first) {
00190     if (first->isInvalid())
00191       break;
00192     out << show(*first);
00193   }
00194   return out.str();
00195 }
00196 
00197 /* ------------------------------------------------------------------------- */
00198 
00199 std::ostream& osl::csaShow(std::ostream& os,const Square pos)
00200 {
00201   return os << record::csa::show(pos);
00202 }
00203 
00204 std::ostream& osl::csaShow(std::ostream& os, const Piece piece)
00205 {
00206   return os << record::csa::show(piece);
00207 }
00208 
00209 std::ostream& osl::csaShow(std::ostream& os,const osl::Ptype ptype)
00210 {
00211   return os << record::csa::show(ptype);
00212 }
00213 
00214 std::ostream& osl::csaShow(std::ostream& os,const Move move)
00215 {
00216   return os << record::csa::show(move);
00217 }
00218 
00219 /* ------------------------------------------------------------------------- */
00220 // ;;; Local Variables:
00221 // ;;; mode:c++
00222 // ;;; c-basic-offset:2
00223 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines