My Project
compactBoard.cc
Go to the documentation of this file.
2#include <iostream>
3#include <algorithm>
4#include <sstream>
5
8{
9 return pos.isPieceStand() ? 0 : ((pos.x() << 4) | pos.y()); // 8 bits
10}
11
13OPiece::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
21namespace osl
22{
23 namespace book
24 {
25
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
73operator==(const CompactBoard& lhs, const CompactBoard& rhs)
74{
75 return (lhs.turn() == rhs.turn()) && (lhs.pieces() == rhs.pieces());
76}
77
78std::ostream& osl::book::
79operator<<(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
90std::istream& osl::book::
91operator>>(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:
bool test(int num) const
Definition pieceMask.h:45
void setTurn(Player player)
void init()
盤面が空の状態に初期化
Player turn() const
const PieceMask & usedMask() const
const Piece pieceOf(int num) const
Definition simpleState.h:76
void setPiece(Player player, Square sq, Ptype ptype)
bool isPieceStand() const
Definition basic_type.h:576
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
static const Square STAND()
Definition basic_type.h:548
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
SimpleStateよりcompactな局面の表現
const std::vector< OPiece > & pieces() const
std::vector< OPiece > piece_vector
SimpleState state() const
Square square() const
Player owner() const
static Square bits2Square(const int bit_position)
Converts an integer (bits) to Square.
static int position2Bits(const Square &pos)
Converts a position to an integer (bits)
Ptype ptype() const
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
std::istream & operator>>(std::istream &os, CompactBoard &c)
void writeInt(std::ostream &os, int n)
int readInt(std::istream &is)
Definition openingBook.cc:7
Player
Definition basic_type.h:8
@ WHITE
Definition basic_type.h:10
bool operator()(const OPiece &l, const OPiece &r)