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   if (str.size() < 4)
00088     throw ParseError("Invalid move string: " + str);
00089   if (str == "pass")
00090   {
00091     return Move::PASS(s.turn());
00092   }
00093 
00094   const Square to = strToPos(str.substr(2,2));
00095   if (str[1] == '*')
00096   {
00097     const Ptype ptype = charToPtype(str[0]);
00098     return Move(to, ptype, s.turn());
00099   }
00100 
00101   const Square from = strToPos(str.substr(0,2));
00102   const Ptype ptype = s.pieceOnBoard(from).ptype();
00103   const Ptype captured = s.pieceOnBoard(to).ptype();
00104   bool promotion = false;
00105   if (str.size() > 4)
00106   {
00107     assert(str[4] == '+');
00108     promotion = true;
00109   }
00110   return Move(from, to, (promotion ? promote(ptype) : ptype), 
00111               captured, promotion, s.turn());
00112 }
00113 
00114 const osl::Square osl::record::psn::
00115 strToPos(const std::string& str)
00116 {
00117   assert(str.size() == 2);
00118   const int x = str[0] - '0';
00119   const int y = str[1] - 'a' + 1;
00120   if (x <= 0 || x > 9 || y <= 0 || y > 9)
00121     throw ParseError("Invalid square character: " + str);
00122   return Square(x, y);
00123 }
00124 
00125 osl::Ptype osl::record::psn::
00126 charToPtype(char c)
00127 {
00128   switch (c) 
00129   {
00130   case 'P': return PAWN;
00131   case 'L': return LANCE;
00132   case 'N': return KNIGHT;
00133   case 'S': return SILVER;
00134   case 'G': return GOLD;
00135   case 'B': return BISHOP;
00136   case 'R': return ROOK;
00137   case 'K': return KING;
00138   default:
00139     return PTYPE_EMPTY;
00140   }
00141 }
00142 
00143 /* ------------------------------------------------------------------------- */
00144 // ;;; Local Variables:
00145 // ;;; mode:c++
00146 // ;;; c-basic-offset:2
00147 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines