00001
00002
00003 #include "osl/record/psn.h"
00004 #include "osl/state/simpleState.h"
00005
00006 const std::string osl::record::psn::
00007 show(Position 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 default:
00030 assert("unsupported ptype" == 0);
00031 return '!';
00032 }
00033 }
00034
00035 const std::string osl::record::psn::
00036 show(Move m)
00037 {
00038 const Position from = m.from();
00039 const Position to = m.to();
00040 if (from.isPieceStand())
00041 {
00042 std::string result = "X*";
00043 result[0] = show(m.ptype());
00044 result += show(to);
00045 return result;
00046 }
00047 std::string result = show(from);
00048 result += show(to);
00049 if (m.promoteMask())
00050 result += '+';
00051 return result;
00052 }
00053
00054
00055 const osl::Move osl::record::psn::
00056 strToMove(const std::string& str, const SimpleState& s)
00057 {
00058 assert(str.size() >= 4);
00059 const Position to = strToPos(str.substr(2,2));
00060 if (str[1] == '*')
00061 {
00062 const Ptype ptype = charToPtype(str[0]);
00063 return Move(to, ptype, s.getTurn());
00064 }
00065
00066 const Position from = strToPos(str.substr(0,2));
00067 const Ptype ptype = s.getPieceOnBoard(from).ptype();
00068 const Ptype captured = s.getPieceOnBoard(to).ptype();
00069 bool promotion = false;
00070 if (str.size() > 4)
00071 {
00072 assert(str[4] == '+');
00073 promotion = true;
00074 }
00075 return Move(from, to, (promotion ? promote(ptype) : ptype),
00076 captured, promotion, s.getTurn());
00077 }
00078
00079 const osl::Position osl::record::psn::
00080 strToPos(const std::string& str)
00081 {
00082 assert(str.size() == 2);
00083 const int x = str[0] - '0';
00084 const int y = str[1] - 'a' + 1;
00085 return Position(x, y);
00086 }
00087
00088 osl::Ptype osl::record::psn::
00089 charToPtype(char c)
00090 {
00091 switch (c)
00092 {
00093 case 'P': return PAWN;
00094 case 'L': return LANCE;
00095 case 'N': return KNIGHT;
00096 case 'S': return SILVER;
00097 case 'G': return GOLD;
00098 case 'B': return BISHOP;
00099 case 'R': return ROOK;
00100 case 'K': return KING;
00101 default:
00102 assert("unknown psn char"==0);
00103 return PTYPE_EMPTY;
00104 }
00105 }
00106
00107
00108
00109
00110
00111