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