miniBoardChar50.cc
Go to the documentation of this file.
1 /* miniBoardChar50.cc
2  */
5 #include "osl/bits/ptypeTable.h"
6 #include "osl/bits/pieceTable.h"
7 #include <tuple>
8 #include <algorithm>
9 #include <stdexcept>
10 
13 {
14  data.fill(0);
15 }
16 
19 {
20  data.fill(0);
21  SimpleState board = (org.turn() == BLACK) ? org : org.rotate180();
22  CArray<std::tuple<int/* =Ptype*/,bool,int /* =Player*/,Square>, Piece::SIZE> pieces;
23  for (int i=0; i<Piece::SIZE; ++i)
24  {
25  const Piece p = board.pieceOf(i);
26  const int ptype_index = Ptype_Table.getIndexMin(unpromote(p.ptype()));
27  pieces[i] = std::make_tuple(ptype_index, p.isPromoted(), p.owner(), p.square());
28  }
29  std::sort(pieces.begin(), pieces.end());
30  for (int i=0; i<Piece::SIZE; ++i)
31  {
32  data[i] = OPiece::position2Bits(std::get<3>(pieces[i]));
33  data[Piece::SIZE + i/8] |= playerToIndex(static_cast<Player>(std::get<2>(pieces[i]))) << (i%8);
34  data[Piece::SIZE + i/8 + 5] |= std::get<1>(pieces[i]) << (i%8);
35  }
36 }
37 
39 MiniBoardChar50::MiniBoardChar50(const std::string& src)
40 {
41  if (src.size() != data.size())
42  throw std::runtime_error("bad argument in MiniBoardChar50::MiniBoardChar50(const std::string&)");
43  std::copy(src.begin(), src.end(), data.begin());
44 }
45 
48 {
49  SimpleState state;
50  state.init();
51 
52  for (int i = 0; i<Piece::SIZE; i++)
53  {
54  const Square position = OPiece::bits2Square(data[i]);
55  const Player owner = indexToPlayer((data[40+i/8] >> (i%8)) & 1);
56  const bool promoted = (data[40+i/8+5] >> (i%8)) & 1;
57  Ptype ptype = Piece_Table.getPtypeOf(i);
58  if (promoted)
59  ptype = promote(ptype);
60  state.setPiece(owner, position, ptype);
61  }
62  state.setTurn(BLACK);
63  state.initPawnMask();
64  if (turn != BLACK)
65  state = state.rotate180();
66  assert(state.turn() == turn);
67  return state;
68 }
69 
70 const std::string osl::book::
72 {
73  return std::string(data.begin(), data.end());
74 }
75 
77 {
78  return std::lexicographical_compare(l.data.begin(), l.data.end(),
79  r.data.begin(), r.data.end());
80 }
82 {
83  return std::equal(l.data.begin(), l.data.end(), r.data.begin());
84 }
85 
86 // ;;; Local Variables:
87 // ;;; mode:c++
88 // ;;; c-basic-offset:2
89 // ;;; End:
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に,promote前の型を返す. promoteしていない型の時はそのまま返す ...
Definition: basic_type.h:157
iterator begin()
Definition: container.h:64
Ptype ptype() const
Definition: basic_type.h:821
void setPiece(Player player, Square sq, Ptype ptype)
Definition: simpleState.cc:114
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
Definition: compactBoard.cc:73
bool operator<(const MiniBoardChar50 &, const MiniBoardChar50 &)
Ptype promote(Ptype ptype)
promote可能なptypeに対して,promote後の型を返す promote不可のptypeを与えてはいけない. ...
Definition: basic_type.h:173
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:83
void init()
盤面が空の状態に初期化
Definition: simpleState.cc:44
const Piece pieceOf(int num) const
Definition: simpleState.h:76
const PtypeTable Ptype_Table
Definition: tables.cc:97
const SimpleState toSimpleState(Player turn=BLACK) const
bool isPromoted() const
promoteした駒かどうかをチェックする
Definition: basic_type.h:898
constexpr int playerToIndex(Player player)
Definition: basic_type.h:16
const PieceTable Piece_Table
Definition: tables.cc:94
CArray< uint8_t, 50 > data
Player turn() const
Definition: simpleState.h:220
50 byte の盤面.
const Square square() const
Definition: basic_type.h:832
constexpr Player indexToPlayer(int n)
Definition: basic_type.h:19
iterator end()
Definition: container.h:65
const SimpleState rotate180() const
Definition: simpleState.cc:505
Ptype getPtypeOf(int num) const
Definition: pieceTable.h:18
const std::string toString() const
Player
Definition: basic_type.h:8
int getIndexMin(Ptype ptype) const
Definition: ptypeTable.h:88
void setTurn(Player player)
Definition: simpleState.h:217
Player owner() const
Definition: basic_type.h:963