My Project
miniBoardChar50.cc
Go to the documentation of this file.
1/* miniBoardChar50.cc
2 */
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
39MiniBoardChar50::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
70const 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:
iterator end()
Definition container.h:65
iterator begin()
Definition container.h:64
Ptype getPtypeOf(int num) const
Definition pieceTable.h:18
Ptype ptype() const
Definition basic_type.h:821
bool isPromoted() const
promoteした駒かどうかをチェックする
Definition basic_type.h:898
const Square square() const
Definition basic_type.h:832
Player owner() const
Definition basic_type.h:963
int getIndexMin(Ptype ptype) const
Definition ptypeTable.h:88
void setTurn(Player player)
void init()
盤面が空の状態に初期化
Player turn() const
const Piece pieceOf(int num) const
Definition simpleState.h:76
void setPiece(Player player, Square sq, Ptype ptype)
const SimpleState rotate180() const
const std::string toString() const
CArray< uint8_t, 50 > data
const SimpleState toSimpleState(Player turn=BLACK) const
bool operator<(const MiniBoardChar50 &, const MiniBoardChar50 &)
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
const PtypeTable Ptype_Table
Definition tables.cc:97
const PieceTable Piece_Table
Definition tables.cc:94
constexpr Player indexToPlayer(int n)
Definition basic_type.h:19
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に,promote前の型を返す. promoteしていない型の時はそのまま返す
Definition basic_type.h:157
constexpr int playerToIndex(Player player)
Definition basic_type.h:16
Player
Definition basic_type.h:8
@ BLACK
Definition basic_type.h:9
Ptype promote(Ptype ptype)
promote可能なptypeに対して,promote後の型を返す promote不可のptypeを与えてはいけない.
Definition basic_type.h:173