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