miniBoardChar50.cc
Go to the documentation of this file.
/* miniBoardChar50.cc
*/
#include "osl/book/miniBoardChar50.h"
#include "osl/book/compactBoard.h"
#include "osl/bits/ptypeTable.h"
#include "osl/bits/pieceTable.h"
#include <tuple>
#include <algorithm>
#include <stdexcept>
osl::book::
MiniBoardChar50::MiniBoardChar50()
{
data.fill(0);
}
osl::book::
MiniBoardChar50::MiniBoardChar50(const SimpleState& org)
{
data.fill(0);
SimpleState board = (org.turn() == BLACK) ? org : org.rotate180();
CArray<std::tuple<int/* =Ptype*/,bool,int /* =Player*/,Square>, Piece::SIZE> pieces;
for (int i=0; i<Piece::SIZE; ++i)
{
const Piece p = board.pieceOf(i);
const int ptype_index = Ptype_Table.getIndexMin(unpromote(p.ptype()));
pieces[i] = std::make_tuple(ptype_index, p.isPromoted(), p.owner(), p.square());
}
std::sort(pieces.begin(), pieces.end());
for (int i=0; i<Piece::SIZE; ++i)
{
data[i] = OPiece::position2Bits(std::get<3>(pieces[i]));
data[Piece::SIZE + i/8] |= playerToIndex(static_cast<Player>(std::get<2>(pieces[i]))) << (i%8);
data[Piece::SIZE + i/8 + 5] |= std::get<1>(pieces[i]) << (i%8);
}
}
osl::book::
MiniBoardChar50::MiniBoardChar50(const std::string& src)
{
if (src.size() != data.size())
throw std::runtime_error("bad argument in MiniBoardChar50::MiniBoardChar50(const std::string&)");
std::copy(src.begin(), src.end(), data.begin());
}
const osl::SimpleState osl::book::
MiniBoardChar50::toSimpleState(Player turn) const
{
SimpleState state;
state.init();
for (int i = 0; i<Piece::SIZE; i++)
{
const Square position = OPiece::bits2Square(data[i]);
const Player owner = indexToPlayer((data[40+i/8] >> (i%8)) & 1);
const bool promoted = (data[40+i/8+5] >> (i%8)) & 1;
Ptype ptype = Piece_Table.getPtypeOf(i);
if (promoted)
ptype = promote(ptype);
state.setPiece(owner, position, ptype);
}
state.setTurn(BLACK);
state.initPawnMask();
if (turn != BLACK)
state = state.rotate180();
assert(state.turn() == turn);
return state;
}
const std::string osl::book::
MiniBoardChar50::toString() const
{
return std::string(data.begin(), data.end());
}
bool osl::book::operator<(const MiniBoardChar50& l, const MiniBoardChar50& r)
{
return std::lexicographical_compare(l.data.begin(), l.data.end(),
r.data.begin(), r.data.end());
}
bool osl::book::operator==(const MiniBoardChar50& l, const MiniBoardChar50& r)
{
return std::equal(l.data.begin(), l.data.end(), r.data.begin());
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::CArray
Definition container.h:20
osl::CArray::end
iterator end()
Definition container.h:65
osl::CArray::begin
iterator begin()
Definition container.h:64
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptype
Ptype ptype() const
Definition basic_type.h:821
osl::Piece::isPromoted
bool isPromoted() const
promoteした駒かどうかをチェックする
Definition basic_type.h:898
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::owner
Player owner() const
Definition basic_type.h:963
osl::Piece::SIZE
static const int SIZE
Definition basic_type.h:794
osl::SimpleState
Definition simpleState.h:35
osl::SimpleState::setTurn
void setTurn(Player player)
Definition simpleState.h:217
osl::SimpleState::init
void init()
盤面が空の状態に初期化
Definition simpleState.cc:44
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::SimpleState::initPawnMask
void initPawnMask()
Definition simpleState.cc:20
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::SimpleState::setPiece
void setPiece(Player player, Square sq, Ptype ptype)
Definition simpleState.cc:114
osl::SimpleState::rotate180
const SimpleState rotate180() const
Definition simpleState.cc:505
osl::Square
Definition basic_type.h:532
osl::book::MiniBoardChar50
50 byte の盤面.
Definition miniBoardChar50.h:19
osl::book::MiniBoardChar50::toString
const std::string toString() const
Definition miniBoardChar50.cc:71
osl::book::MiniBoardChar50::MiniBoardChar50
MiniBoardChar50()
Definition miniBoardChar50.cc:12
osl::book::MiniBoardChar50::data
CArray< uint8_t, 50 > data
Definition miniBoardChar50.h:32
osl::book::MiniBoardChar50::toSimpleState
const SimpleState toSimpleState(Player turn=BLACK) const
Definition miniBoardChar50.cc:47
osl::book::OPiece::bits2Square
static Square bits2Square(const int bit_position)
Converts an integer (bits) to Square.
Definition compactBoard.cc:13
osl::book::OPiece::position2Bits
static int position2Bits(const Square &pos)
Converts a position to an integer (bits).
Definition compactBoard.cc:7
compactBoard.h
miniBoardChar50.h
osl::book::operator<
bool operator<(const MiniBoardChar50 &, const MiniBoardChar50 &)
Definition miniBoardChar50.cc:76
osl::book::operator==
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
Definition compactBoard.cc:73
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::Piece_Table
const PieceTable Piece_Table
Definition tables.cc:94
osl::indexToPlayer
constexpr Player indexToPlayer(int n)
Definition basic_type.h:19
osl::unpromote
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に，promote前の型を返す． promoteしていない型の時はそのまま返す
Definition basic_type.h:157
osl::playerToIndex
constexpr int playerToIndex(Player player)
Definition basic_type.h:16
osl::Player
Player
Definition basic_type.h:8
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::promote
Ptype promote(Ptype ptype)
promote可能なptypeに対して，promote後の型を返す promote不可のptypeを与えてはいけない．
Definition basic_type.h:173
pieceTable.h
ptypeTable.h