File: csa.cc

package info (click to toggle)
libosl 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 273,976 kB
  • sloc: cpp: 129,625; ansic: 7,145; ruby: 1,290; makefile: 558; perl: 413; sh: 35
file content (222 lines) | stat: -rw-r--r-- 5,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "osl/record/csa.h"
#include "osl/record/csaIOError.h"
#include "osl/state/simpleState.h"
#include "osl/pieceTable.h"
#include <iostream>
#include <stdexcept>
#include <cassert>
#include <string>
#include <sstream>

/* ------------------------------------------------------------------------- */

osl::Player osl::record::csa::
charToPlayer(char c)
{
  if(c=='+') 
    return BLACK;
  if(c=='-') 
    return WHITE;
  throw CsaIOError("not a csa PlayerCharacter "+std::string(1,c));
}

const osl::Square osl::record::csa::
strToPos(const std::string& s)
{
  int x=s.at(0)-'0';
  int y=s.at(1)-'0';
  if(x==0 && y==0) 
    return Square::STAND();
  return Square(x,y);
}

osl::Ptype osl::record::csa::
strToPtype(const std::string& s)
{
  for(int i=0;i<16;i++){
    if(s == Ptype_Table.getCsaName(static_cast<Ptype>(i))) 
      return static_cast<Ptype>(i);
  }
  throw CsaIOError("unknown std::string in csa::strToPtype "+s);
}

const osl::Move osl::record::csa::
strToMove(const std::string& s,const SimpleState& state)
{
  if (s == "%KACHI")
    return Move::DeclareWin();
  if (s == "%TORYO")
    return Move::INVALID();
  if (s == "%PASS")		// FIXME: not in CSA protocol
    return Move::PASS(state.turn());

  Player pl=csa::charToPlayer(s.at(0));
  Square fromPos=csa::strToPos(s.substr(1,2));
  Square toPos=csa::strToPos(s.substr(3,2));
  Ptype ptype=csa::strToPtype(s.substr(5,2));
  if(fromPos==Square::STAND()){
    if (isPromoted(ptype))
      throw CsaIOError("drop with promote ?! in csa::strToMove "+s);
    return Move(toPos,ptype,pl);
  }
  else{
    Piece p0=state.pieceAt(fromPos);
    Piece p1=state.pieceAt(toPos);
    Ptype capturePtype=p1.ptype();
    bool isPromote=(p0.ptype()!=ptype);
    if (! ((p0.ptype()==ptype)||(p0.ptype()==unpromote(ptype))))
      throw CsaIOError("bad promotion in csa::strToMove "+s);
    return Move(fromPos,toPos,ptype,
		capturePtype,isPromote,pl);
  }
}

/* ------------------------------------------------------------------------- */
const std::string osl::record::csa::
show(Player player, std::string& buf, size_t offset)
{
  assert(buf.size() >= offset+1);
  buf[offset] = (player==BLACK) ? '+' : '-';
  return buf;
}

const std::string osl::record::csa::
show(Move move, std::string& buf)
{
  assert(buf.capacity() >= 7);
  buf.resize(7);
  if (move == Move::DeclareWin())
    return buf = "%KACHI";
  if (move.isInvalid())
    return buf = "%TORYO";
  if (move.isPass())
    return buf = "%PASS";		// FIXME: not in CSA protocol
  show(move.player(), buf);
  show(move.from(), buf, 1);
  show(move.to(), buf, 3);
  show(move.ptype(), buf, 5);
  return buf;
}

const std::string osl::record::csa::
show(Square pos, std::string& buf, size_t offset)
{
  assert(buf.size() >= offset+2);
  if (pos.isPieceStand()) 
  {
    buf[0+offset] = '0';
    buf[1+offset] = '0';
    return buf;
  }
  const int x = pos.x();
  const int y = pos.y();
  buf[offset+0] = x + '0';
  buf[offset+1] = y + '0';
  return buf;
}

const std::string osl::record::csa::
show(Ptype ptype, std::string& buf, size_t offset)
{
  assert(buf.size() >= offset+2);
  const char *name = Ptype_Table.getCsaName(ptype);
  buf[0+offset] = name[0];
  buf[1+offset] = name[1];
  return buf;
}

const std::string osl::record::csa::
show(Move move)
{
  // NOTE: copy コピーを返すので dangling pointer ではない
  std::string buf("+7776FU");
  return show(move, buf);
}

const std::string osl::record::csa::
fancyShow(Move move)
{
  std::string ret = show(move);
  if (move.isNormal()) {
    if (move.capturePtype() != PTYPE_EMPTY)
      ret += "x" + show(move.capturePtype());
    if (move.isPromotion())
      ret += '+';
  }
  return ret;
}

const std::string osl::record::csa::
show(Player player)
{
  std::string buf("+");
  return show(player, buf);
}

const std::string osl::record::csa::
show(Square position)
{
  std::string buf("00");
  return show(position, buf);
}

const std::string osl::record::csa::
show(Ptype ptype)
{
  std::string buf("OU");
  return show(ptype, buf);
}

const std::string osl::record::csa::
show(Piece piece)
{
  if (piece.isEdge())
    return "   ";
  if (piece.isEmpty())
    return " * ";

  assert(piece.isPiece() && isPiece(piece.ptype()));
  assert(unpromote(piece.ptype()) == Piece_Table.getPtypeOf(piece.number()));
  return show(piece.owner()) 
    + show(piece.ptype());
}

const std::string osl::record::csa::
show(const Move *first, const Move *last)
{
  std::ostringstream out;
  for (; first != last; ++first) {
    if (first->isInvalid())
      break;
    out << show(*first);
  }
  return out.str();
}

/* ------------------------------------------------------------------------- */

std::ostream& osl::csaShow(std::ostream& os,const Square pos)
{
  return os << record::csa::show(pos);
}

std::ostream& osl::csaShow(std::ostream& os, const Piece piece)
{
  return os << record::csa::show(piece);
}

std::ostream& osl::csaShow(std::ostream& os,const osl::Ptype ptype)
{
  return os << record::csa::show(ptype);
}

std::ostream& osl::csaShow(std::ostream& os,const Move move)
{
  return os << record::csa::show(move);
}

/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: