compactBoard.cc
Go to the documentation of this file.
2 #include <iostream>
3 #include <algorithm>
4 #include <sstream>
5 
6 int osl::book::
8 {
9  return pos.isPieceStand() ? 0 : ((pos.x() << 4) | pos.y()); // 8 bits
10 }
11 
13 OPiece::bits2Square(const int bit_position)
14 {
15  if ((bit_position & 0xff) == 0)
16  return Square::STAND();
17  else
18  return Square((bit_position >> 4) & 0xf, bit_position & 0xf);
19 }
20 
21 namespace osl
22 {
23  namespace book
24  {
25 
26  struct opiece_sort
27  {
28  bool operator()(const OPiece& l, const OPiece& r) {
29  if (l.square() == Square::STAND() || r.square() == Square::STAND()) {
30  if (l.square() != r.square())
31  return l.square() == Square::STAND();
32  if (l.owner() != r.owner())
33  return l.owner() == WHITE;
34  return l.ptype() < r.ptype();
35  }
36  if (l.square().x() != r.square().x())
37  return l.square().x() < r.square().x();
38  return l.square().y() < r.square().y();
39  }
40  };
41  }
42 }
43 
46 {
47  piece_vector.reserve(40);
48  for (int i = 0; i < 40; i++)
49  {
50  if(state.usedMask().test(i))
51  piece_vector.push_back(OPiece(state.pieceOf(i)));
52  }
53  std::sort(piece_vector.begin(), piece_vector.end(), opiece_sort());
54  player_to_move = state.turn();
55 }
56 
59 {
60 
61  SimpleState state;
62  state.init();
63 
64  for (const OPiece& p: piece_vector) {
65  state.setPiece(p.owner(), p.square(), p.ptype());
66  }
67  state.setTurn(turn());
68  state.initPawnMask();
69  return state;
70 }
71 
72 bool osl::book::
73 operator==(const CompactBoard& lhs, const CompactBoard& rhs)
74 {
75  return (lhs.turn() == rhs.turn()) && (lhs.pieces() == rhs.pieces());
76 }
77 
78 std::ostream& osl::book::
79 operator<<(std::ostream& os, const CompactBoard& c)
80 {
81 
82  for (unsigned int i = 0; i < c.pieces().size(); i++)
83  {
84  writeInt(os, static_cast<int>(c.pieces()[i]));
85  }
86  writeInt(os, static_cast<int>(c.turn()));
87  return os;
88 }
89 
90 std::istream& osl::book::
91 operator>>(std::istream& is, CompactBoard& c)
92 {
93  assert(c.piece_vector.size() == 0);
94 
95  for (unsigned int i = 0; i < 40; i++)
96  {
97  c.piece_vector.push_back(OPiece(readInt(is)));
98  }
99  c.player_to_move = static_cast<Player>(readInt(is));
100  return is;
101 }
102 
103 /* ------------------------------------------------------------------------- */
104 // ;;; Local Variables:
105 // ;;; mode:c++
106 // ;;; c-basic-offset:2
107 // ;;; End:
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
Definition: compactBoard.cc:79
Square square() const
Definition: compactBoard.h:25
void setPiece(Player player, Square sq, Ptype ptype)
Definition: simpleState.cc:114
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
Definition: compactBoard.cc:73
bool test(int num) const
Definition: pieceMask.h:45
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
SimpleState state() const
Definition: compactBoard.cc:58
void init()
盤面が空の状態に初期化
Definition: simpleState.cc:44
const Piece pieceOf(int num) const
Definition: simpleState.h:76
std::istream & operator>>(std::istream &os, CompactBoard &c)
Definition: compactBoard.cc:91
static Square bits2Square(const int bit_position)
Converts an integer (bits) to Square.
Definition: compactBoard.cc:13
Player turn() const
Definition: compactBoard.h:66
std::vector< OPiece > piece_vector
Definition: compactBoard.h:72
Ptype ptype() const
Definition: compactBoard.h:29
Player turn() const
Definition: simpleState.h:220
static int position2Bits(const Square &pos)
Converts a position to an integer (bits)
Definition: compactBoard.cc:7
Player owner() const
Definition: compactBoard.h:33
const std::vector< OPiece > & pieces() const
Definition: compactBoard.h:65
int readInt(std::istream &is)
Definition: openingBook.cc:7
bool operator()(const OPiece &l, const OPiece &r)
Definition: compactBoard.cc:28
const PieceMask & usedMask() const
Definition: simpleState.h:131
SimpleStateよりcompactな局面の表現
Definition: compactBoard.h:59
void writeInt(std::ostream &os, int n)
Definition: openingBook.cc:18
Player
Definition: basic_type.h:8
bool isPieceStand() const
Definition: basic_type.h:576
static const Square STAND()
Definition: basic_type.h:548
void setTurn(Player player)
Definition: simpleState.h:217