compactBoard.cc
Go to the documentation of this file.
#include "osl/book/compactBoard.h"
#include <iostream>
#include <algorithm>
#include <sstream>
int osl::book::
OPiece::position2Bits(const Square& pos)
{
return pos.isPieceStand() ? 0 : ((pos.x() << 4) | pos.y()); // 8 bits
}
osl::Square osl::book::
OPiece::bits2Square(const int bit_position)
{
if ((bit_position & 0xff) == 0)
return Square::STAND();
else
return Square((bit_position >> 4) & 0xf, bit_position & 0xf);
}
namespace osl
{
namespace book
{
struct opiece_sort
{
bool operator()(const OPiece& l, const OPiece& r) {
if (l.square() == Square::STAND() || r.square() == Square::STAND()) {
if (l.square() != r.square())
return l.square() == Square::STAND();
if (l.owner() != r.owner())
return l.owner() == WHITE;
return l.ptype() < r.ptype();
}
if (l.square().x() != r.square().x())
return l.square().x() < r.square().x();
return l.square().y() < r.square().y();
}
};
}
}
osl::book::
CompactBoard::CompactBoard(const SimpleState& state)
{
piece_vector.reserve(40);
for (int i = 0; i < 40; i++)
{
if(state.usedMask().test(i))
piece_vector.push_back(OPiece(state.pieceOf(i)));
}
std::sort(piece_vector.begin(), piece_vector.end(), opiece_sort());
player_to_move = state.turn();
}
osl::SimpleState osl::book::
CompactBoard::state() const
{
SimpleState state;
state.init();
for (const OPiece& p: piece_vector) {
state.setPiece(p.owner(), p.square(), p.ptype());
}
state.setTurn(turn());
state.initPawnMask();
return state;
}
bool osl::book::
operator==(const CompactBoard& lhs, const CompactBoard& rhs)
{
return (lhs.turn() == rhs.turn()) && (lhs.pieces() == rhs.pieces());
}
std::ostream& osl::book::
operator<<(std::ostream& os, const CompactBoard& c)
{
for (unsigned int i = 0; i < c.pieces().size(); i++)
{
writeInt(os, static_cast<int>(c.pieces()[i]));
}
writeInt(os, static_cast<int>(c.turn()));
return os;
}
std::istream& osl::book::
operator>>(std::istream& is, CompactBoard& c)
{
assert(c.piece_vector.size() == 0);
for (unsigned int i = 0; i < 40; i++)
{
c.piece_vector.push_back(OPiece(readInt(is)));
}
c.player_to_move = static_cast<Player>(readInt(is));
return is;
}
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::SimpleState
Definition simpleState.h:35
osl::Square
Definition basic_type.h:532
osl::Square::isPieceStand
bool isPieceStand() const
Definition basic_type.h:576
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::STAND
static const Square STAND()
Definition basic_type.h:548
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::book::CompactBoard
SimpleStateよりcompactな局面の表現
Definition compactBoard.h:60
osl::book::CompactBoard::CompactBoard
CompactBoard()
Definition compactBoard.h:62
osl::book::CompactBoard::player_to_move
Player player_to_move
Definition compactBoard.h:73
osl::book::CompactBoard::turn
Player turn() const
Definition compactBoard.h:66
osl::book::CompactBoard::pieces
const std::vector< OPiece > & pieces() const
Definition compactBoard.h:65
osl::book::CompactBoard::piece_vector
std::vector< OPiece > piece_vector
Definition compactBoard.h:72
osl::book::CompactBoard::state
SimpleState state() const
Definition compactBoard.cc:58
osl::book::OPiece
Definition compactBoard.h:12
osl::book::OPiece::square
Square square() const
Definition compactBoard.h:25
osl::book::OPiece::owner
Player owner() const
Definition compactBoard.h:33
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
osl::book::OPiece::ptype
Ptype ptype() const
Definition compactBoard.h:29
compactBoard.h
osl::book
Definition bookInMemory.h:12
osl::book::operator==
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
Definition compactBoard.cc:73
osl::book::operator<<
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
Definition compactBoard.cc:79
osl::book::operator>>
std::istream & operator>>(std::istream &os, CompactBoard &c)
Definition compactBoard.cc:91
osl::book::writeInt
void writeInt(std::ostream &os, int n)
Definition openingBook.cc:18
osl::book::readInt
int readInt(std::istream &is)
Definition openingBook.cc:7
osl
Definition additionalEffect.h:6
osl::Player
Player
Definition basic_type.h:8
osl::WHITE
@ WHITE
Definition basic_type.h:10
osl::book::opiece_sort
Definition compactBoard.cc:27
osl::book::opiece_sort::operator()
bool operator()(const OPiece &l, const OPiece &r)
Definition compactBoard.cc:28