psn.cc
Go to the documentation of this file.
00001 /* psn.cc
00002  */
00003 #include "osl/record/psn.h"
00004 #include "osl/state/simpleState.h"
00005 
00006 const std::string osl::record::psn::
00007 show(Square pos)
00008 {
00009   const int x = pos.x();
00010   const int y = pos.y();
00011   std::string result = "XX";
00012   result[0] = x + '0';
00013   result[1] = y + 'a' - 1;
00014   return result;
00015 }
00016 
00017 char osl::record::psn::
00018 show(Ptype ptype)
00019 {
00020   switch (ptype)
00021   {
00022   case PAWN:    return 'P';
00023   case LANCE:   return 'L';
00024   case KNIGHT:  return 'N';
00025   case SILVER:  return 'S';
00026   case GOLD:    return 'G';
00027   case BISHOP:  return 'B';
00028   case ROOK:    return 'R';
00029   case KING:    return 'K';
00030   default:
00031     assert("unsupported ptype" == 0);
00032     return '!';
00033   }
00034 }
00035 
00036 const std::string osl::record::psn::
00037 show(Move m)
00038 {
00039   const Square from = m.from();
00040   const Square to = m.to();
00041   if (from.isPieceStand())
00042   {
00043     std::string result = "X*";
00044     result[0] = show(m.ptype());
00045     result += show(to);
00046     return result;
00047   }
00048   std::string result = show(from);
00049   result += show(to);
00050   if (m.promoteMask())
00051     result += '+';
00052   return result;
00053 }
00054 
00055 const std::string osl::record::psn::
00056 showXP(Move m)
00057 {
00058   if (m.isInvalid())
00059     return "resign";  
00060   if (m.isPass())
00061     return "pass";
00062   const Square from = m.from();
00063   const Square to = m.to();
00064   if (from.isPieceStand())
00065   {
00066     std::string result = "X*";
00067     result[0] = show(m.ptype());
00068     result += show(to);
00069     return result;
00070   }
00071   std::string result = show(from);
00072   if (m.capturePtype() != PTYPE_EMPTY)
00073     result += 'x';
00074   result += show(to);
00075   if (m.isPromotion())
00076     result += '+';
00077   else if (canPromote(m.ptype())
00078            && (from.canPromote(m.player()) || to.canPromote(m.player())))
00079     result += '=';
00080   return result;
00081 }
00082 
00083 
00084 const osl::Move osl::record::psn::
00085 strToMove(const std::string& str, const SimpleState& s)
00086 {
00087   assert(str.size() >= 4);
00088   const Square to = strToPos(str.substr(2,2));
00089   if (str[1] == '*')
00090   {
00091     const Ptype ptype = charToPtype(str[0]);
00092     return Move(to, ptype, s.turn());
00093   }
00094 
00095   const Square from = strToPos(str.substr(0,2));
00096   const Ptype ptype = s.pieceOnBoard(from).ptype();
00097   const Ptype captured = s.pieceOnBoard(to).ptype();
00098   bool promotion = false;
00099   if (str.size() > 4)
00100   {
00101     assert(str[4] == '+');
00102     promotion = true;
00103   }
00104   return Move(from, to, (promotion ? promote(ptype) : ptype), 
00105               captured, promotion, s.turn());
00106 }
00107 
00108 const osl::Square osl::record::psn::
00109 strToPos(const std::string& str)
00110 {
00111   assert(str.size() == 2);
00112   const int x = str[0] - '0';
00113   const int y = str[1] - 'a' + 1;
00114   return Square(x, y);
00115 }
00116 
00117 osl::Ptype osl::record::psn::
00118 charToPtype(char c)
00119 {
00120   switch (c) 
00121   {
00122   case 'P': return PAWN;
00123   case 'L': return LANCE;
00124   case 'N': return KNIGHT;
00125   case 'S': return SILVER;
00126   case 'G': return GOLD;
00127   case 'B': return BISHOP;
00128   case 'R': return ROOK;
00129   case 'K': return KING;
00130   default:
00131     return PTYPE_EMPTY;
00132   }
00133 }
00134 
00135 /* ------------------------------------------------------------------------- */
00136 // ;;; Local Variables:
00137 // ;;; mode:c++
00138 // ;;; c-basic-offset:2
00139 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines