All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
psn.cc
Go to the documentation of this file.
1 /* psn.cc
2  */
3 #include "osl/record/psn.h"
5 
6 const std::string osl::record::psn::
7 show(Square pos)
8 {
9  const int x = pos.x();
10  const int y = pos.y();
11  std::string result = "XX";
12  result[0] = x + '0';
13  result[1] = y + 'a' - 1;
14  return result;
15 }
16 
18 show(Ptype ptype)
19 {
20  switch (ptype)
21  {
22  case PAWN: return 'P';
23  case LANCE: return 'L';
24  case KNIGHT: return 'N';
25  case SILVER: return 'S';
26  case GOLD: return 'G';
27  case BISHOP: return 'B';
28  case ROOK: return 'R';
29  case KING: return 'K';
30  default:
31  assert("unsupported ptype" == 0);
32  return '!';
33  }
34 }
35 
36 const std::string osl::record::psn::
38 {
39  const Square from = m.from();
40  const Square to = m.to();
41  if (from.isPieceStand())
42  {
43  std::string result = "X*";
44  result[0] = show(m.ptype());
45  result += show(to);
46  return result;
47  }
48  std::string result = show(from);
49  result += show(to);
50  if (m.promoteMask())
51  result += '+';
52  return result;
53 }
54 
55 const std::string osl::record::psn::
57 {
58  if (m.isInvalid())
59  return "resign";
60  if (m.isPass())
61  return "pass";
62  const Square from = m.from();
63  const Square to = m.to();
64  if (from.isPieceStand())
65  {
66  std::string result = "X*";
67  result[0] = show(m.ptype());
68  result += show(to);
69  return result;
70  }
71  std::string result = show(from);
72  if (m.capturePtype() != PTYPE_EMPTY)
73  result += 'x';
74  result += show(to);
75  if (m.isPromotion())
76  result += '+';
77  else if (canPromote(m.ptype())
78  && (from.canPromote(m.player()) || to.canPromote(m.player())))
79  result += '=';
80  return result;
81 }
82 
83 
85 strToMove(const std::string& str, const SimpleState& s)
86 {
87  if (str.size() < 4)
88  throw ParseError("Invalid move string: " + str);
89 
90  const Square to = strToPos(str.substr(2,2));
91  if (str[1] == '*')
92  {
93  const Ptype ptype = charToPtype(str[0]);
94  return Move(to, ptype, s.turn());
95  }
96 
97  const Square from = strToPos(str.substr(0,2));
98  const Ptype ptype = s.pieceOnBoard(from).ptype();
99  const Ptype captured = s.pieceOnBoard(to).ptype();
100  if (! isPiece(ptype))
101  throw ParseError("No piece on square: " + str);
102  bool promotion = false;
103  if (str.size() > 4)
104  {
105  assert(str[4] == '+');
106  promotion = true;
107  }
108  return Move(from, to, (promotion ? promote(ptype) : ptype),
109  captured, promotion, s.turn());
110 }
111 
113 strToPos(const std::string& str)
114 {
115  assert(str.size() == 2);
116  const int x = str[0] - '0';
117  const int y = str[1] - 'a' + 1;
118  if (x <= 0 || x > 9 || y <= 0 || y > 9)
119  throw ParseError("Invalid square character: " + str);
120  return Square(x, y);
121 }
122 
124 charToPtype(char c)
125 {
126  switch (c)
127  {
128  case 'P': return PAWN;
129  case 'L': return LANCE;
130  case 'N': return KNIGHT;
131  case 'S': return SILVER;
132  case 'G': return GOLD;
133  case 'B': return BISHOP;
134  case 'R': return ROOK;
135  case 'K': return KING;
136  default:
137  return PTYPE_EMPTY;
138  }
139 }
140 
141 /* ------------------------------------------------------------------------- */
142 // ;;; Local Variables:
143 // ;;; mode:c++
144 // ;;; c-basic-offset:2
145 // ;;; End: