All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
csa.cc
Go to the documentation of this file.
1 #include "osl/record/csa.h"
4 #include "osl/pieceTable.h"
5 #include <iostream>
6 #include <stdexcept>
7 #include <cassert>
8 #include <string>
9 #include <sstream>
10 
11 /* ------------------------------------------------------------------------- */
12 
14 charToPlayer(char c)
15 {
16  if(c=='+')
17  return BLACK;
18  if(c=='-')
19  return WHITE;
20  throw CsaIOError("not a csa PlayerCharacter "+std::string(1,c));
21 }
22 
24 strToPos(const std::string& s)
25 {
26  int x=s.at(0)-'0';
27  int y=s.at(1)-'0';
28  if(x==0 && y==0)
29  return Square::STAND();
30  return Square(x,y);
31 }
32 
34 strToPtype(const std::string& s)
35 {
36  for(int i=0;i<16;i++){
37  if(s == Ptype_Table.getCsaName(static_cast<Ptype>(i)))
38  return static_cast<Ptype>(i);
39  }
40  throw CsaIOError("unknown std::string in csa::strToPtype "+s);
41 }
42 
44 strToMove(const std::string& s,const SimpleState& state)
45 {
46  if (s == "%KACHI")
47  return Move::DeclareWin();
48  if (s == "%TORYO")
49  return Move::INVALID();
50  if (s == "%PASS") // FIXME: not in CSA protocol
51  return Move::PASS(state.turn());
52 
53  Player pl=csa::charToPlayer(s.at(0));
54  Square fromPos=csa::strToPos(s.substr(1,2));
55  Square toPos=csa::strToPos(s.substr(3,2));
56  Ptype ptype=csa::strToPtype(s.substr(5,2));
57  if(fromPos==Square::STAND()){
58  if (isPromoted(ptype))
59  throw CsaIOError("drop with promote ?! in csa::strToMove "+s);
60  return Move(toPos,ptype,pl);
61  }
62  else{
63  Piece p0=state.pieceAt(fromPos);
64  Piece p1=state.pieceAt(toPos);
65  Ptype capturePtype=p1.ptype();
66  bool isPromote=(p0.ptype()!=ptype);
67  if (! ((p0.ptype()==ptype)||(p0.ptype()==unpromote(ptype))))
68  throw CsaIOError("bad promotion in csa::strToMove "+s);
69  return Move(fromPos,toPos,ptype,
70  capturePtype,isPromote,pl);
71  }
72 }
73 
74 /* ------------------------------------------------------------------------- */
75 const std::string osl::record::csa::
76 show(Player player, std::string& buf, size_t offset)
77 {
78  assert(buf.size() >= offset+1);
79  buf[offset] = (player==BLACK) ? '+' : '-';
80  return buf;
81 }
82 
83 const std::string osl::record::csa::
84 show(Move move, std::string& buf)
85 {
86  assert(buf.capacity() >= 7);
87  buf.resize(7);
88  if (move == Move::DeclareWin())
89  return buf = "%KACHI";
90  if (move.isInvalid())
91  return buf = "%TORYO";
92  if (move.isPass())
93  return buf = "%PASS"; // FIXME: not in CSA protocol
94  show(move.player(), buf);
95  show(move.from(), buf, 1);
96  show(move.to(), buf, 3);
97  show(move.ptype(), buf, 5);
98  return buf;
99 }
100 
101 const std::string osl::record::csa::
102 show(Square pos, std::string& buf, size_t offset)
103 {
104  assert(buf.size() >= offset+2);
105  if (pos.isPieceStand())
106  {
107  buf[0+offset] = '0';
108  buf[1+offset] = '0';
109  return buf;
110  }
111  const int x = pos.x();
112  const int y = pos.y();
113  buf[offset+0] = x + '0';
114  buf[offset+1] = y + '0';
115  return buf;
116 }
117 
118 const std::string osl::record::csa::
119 show(Ptype ptype, std::string& buf, size_t offset)
120 {
121  assert(buf.size() >= offset+2);
122  const char *name = Ptype_Table.getCsaName(ptype);
123  buf[0+offset] = name[0];
124  buf[1+offset] = name[1];
125  return buf;
126 }
127 
128 const std::string osl::record::csa::
129 show(Move move)
130 {
131  // NOTE: copy コピーを返すので dangling pointer ではない
132  std::string buf("+7776FU");
133  return show(move, buf);
134 }
135 
136 const std::string osl::record::csa::
138 {
139  std::string ret = show(move);
140  if (move.isNormal()) {
141  if (move.capturePtype() != PTYPE_EMPTY)
142  ret += "x" + show(move.capturePtype());
143  if (move.isPromotion())
144  ret += '+';
145  }
146  return ret;
147 }
148 
149 const std::string osl::record::csa::
150 show(Player player)
151 {
152  std::string buf("+");
153  return show(player, buf);
154 }
155 
156 const std::string osl::record::csa::
157 show(Square position)
158 {
159  std::string buf("00");
160  return show(position, buf);
161 }
162 
163 const std::string osl::record::csa::
164 show(Ptype ptype)
165 {
166  std::string buf("OU");
167  return show(ptype, buf);
168 }
169 
170 const std::string osl::record::csa::
171 show(Piece piece)
172 {
173  if (piece.isEdge())
174  return " ";
175  if (piece.isEmpty())
176  return " * ";
177 
178  assert(piece.isPiece() && isPiece(piece.ptype()));
179  assert(unpromote(piece.ptype()) == Piece_Table.getPtypeOf(piece.number()));
180  return show(piece.owner())
181  + show(piece.ptype());
182 }
183 
184 const std::string osl::record::csa::
185 show(const Move *first, const Move *last)
186 {
187  std::ostringstream out;
188  for (; first != last; ++first) {
189  if (first->isInvalid())
190  break;
191  out << show(*first);
192  }
193  return out.str();
194 }
195 
196 /* ------------------------------------------------------------------------- */
197 
198 std::ostream& osl::csaShow(std::ostream& os,const Square pos)
199 {
200  return os << record::csa::show(pos);
201 }
202 
203 std::ostream& osl::csaShow(std::ostream& os, const Piece piece)
204 {
205  return os << record::csa::show(piece);
206 }
207 
208 std::ostream& osl::csaShow(std::ostream& os,const osl::Ptype ptype)
209 {
210  return os << record::csa::show(ptype);
211 }
212 
213 std::ostream& osl::csaShow(std::ostream& os,const Move move)
214 {
215  return os << record::csa::show(move);
216 }
217 
218 /* ------------------------------------------------------------------------- */
219 // ;;; Local Variables:
220 // ;;; mode:c++
221 // ;;; c-basic-offset:2
222 // ;;; End: