miniBoard.cc
Go to the documentation of this file.
#include "osl/record/record.h"
#include "osl/book/miniBoard.h"
#include "osl/misc/base64.h"
#include <boost/dynamic_bitset.hpp>
#include <iostream>
#include <algorithm>
#include <sstream>
namespace osl
{
namespace book
{
struct oposition_sort
{
bool operator()(const OSquare& l, const OSquare& r)
{
// need to special case pieces on stand
if (l.getSquare() == Square::STAND() || r.getSquare() == Square::STAND())
{
if (l.getSquare() == Square::STAND() && r.getSquare() != Square::STAND())
return true;
else if (l.getSquare() != Square::STAND() && r.getSquare() == Square::STAND())
return false;
else
{
if (l.getOwner() != r.getOwner())
return l.getOwner() == WHITE;
return true;
}
}
else
{
if (l.getSquare().x() < r.getSquare().x())
return true;
else if (l.getSquare().x() > r.getSquare().x())
return false;
else
{
if (l.getSquare().y() <= r.getSquare().y())
return true;
else
return false;
}
}
}
};
const size_t MiniBoard::total_bits = 400;
const size_t OSquare::total_bits = 9;
const size_t OPSquare::total_bits = 10;
MiniBoard::MiniBoard(const SimpleState& state)
{
pawn_pieces.reserve(18);
lance_pieces.reserve(4);
knight_pieces.reserve(4);
silver_pieces.reserve(4);
bishop_pieces.reserve(2);
rook_pieces.reserve(2);
gold_pieces.reserve(4);
for (int i = 0; i < 40; ++i)
{
if(!state.usedMask().test(i)) continue;
const Piece p = state.pieceOf(i);
switch (unpromote(p.ptype()))
{
case PAWN:
pawn_pieces.push_back(OPSquare(p));
break;
case LANCE:
lance_pieces.push_back(OPSquare(p));
break;
case KNIGHT:
knight_pieces.push_back(OPSquare(p));
break;
case SILVER:
silver_pieces.push_back(OPSquare(p));
break;
case BISHOP:
bishop_pieces.push_back(OPSquare(p));
break;
case ROOK:
rook_pieces.push_back(OPSquare(p));
break;
case GOLD:
gold_pieces.push_back(OSquare(p));
break;
case KING:
if (p.owner() == BLACK)
king_pieces[0] = static_cast<char>(OPiece::position2Bits(p.square()));
else
king_pieces[1] = static_cast<char>(OPiece::position2Bits(p.square()));
break;
default:
assert(false);
}
}
turn = state.turn();
std::sort(pawn_pieces.begin(), pawn_pieces.end(), oposition_sort());
std::sort(lance_pieces.begin(), lance_pieces.end(), oposition_sort());
std::sort(knight_pieces.begin(), knight_pieces.end(), oposition_sort());
std::sort(silver_pieces.begin(), silver_pieces.end(), oposition_sort());
std::sort(bishop_pieces.begin(), bishop_pieces.end(), oposition_sort());
std::sort(rook_pieces.begin(), rook_pieces.end(), oposition_sort());
std::sort(gold_pieces.begin(), gold_pieces.end(), oposition_sort());
}
SimpleState
MiniBoard::getState() const
{
SimpleState state;
state.init();
for (PawnArray::const_iterator p = pawn_pieces.begin();
p != pawn_pieces.end(); ++p)
{
Ptype ptype = PAWN;
if (p->isPromoted())
ptype = promote(ptype);
state.setPiece(p->getOwner(), p->getSquare(), ptype);
}
for (LanceArray::const_iterator p = lance_pieces.begin();
p != lance_pieces.end(); ++p)
{
Ptype ptype = LANCE;
if (p->isPromoted())
ptype = promote(ptype);
state.setPiece(p->getOwner(), p->getSquare(), ptype);
}
for (KnightArray::const_iterator p = knight_pieces.begin();
p != knight_pieces.end(); ++p)
{
Ptype ptype = KNIGHT;
if (p->isPromoted())
ptype = promote(ptype);
state.setPiece(p->getOwner(), p->getSquare(), ptype);
}
for (SilverArray::const_iterator p = silver_pieces.begin();
p != silver_pieces.end(); ++p)
{
Ptype ptype = SILVER;
if (p->isPromoted())
ptype = promote(ptype);
state.setPiece(p->getOwner(), p->getSquare(), ptype);
}
for (BishopArray::const_iterator p = bishop_pieces.begin();
p != bishop_pieces.end(); ++p)
{
Ptype ptype = BISHOP;
if (p->isPromoted())
ptype = promote(ptype);
state.setPiece(p->getOwner(), p->getSquare(), ptype);
}
for (RookArray::const_iterator p = rook_pieces.begin();
p != rook_pieces.end(); ++p)
{
Ptype ptype = ROOK;
if (p->isPromoted())
ptype = promote(ptype);
state.setPiece(p->getOwner(), p->getSquare(), ptype);
}
for (GoldArray::const_iterator p = gold_pieces.begin();
p != gold_pieces.end(); ++p)
{
state.setPiece(p->getOwner(), p->getSquare(), GOLD);
}
state.setPiece(BLACK, OPiece::bits2Square(king_pieces[0]), KING);
state.setPiece(WHITE, OPiece::bits2Square(king_pieces[1]), KING);
state.setTurn(turn);
return state;
}
boost::dynamic_bitset<>
MiniBoard::toBits() const
{
boost::dynamic_bitset<> bits(total_bits);
for (PawnArray::const_iterator p = pawn_pieces.begin();
p != pawn_pieces.end(); ++p)
{
const int value = static_cast<int>(*p);
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << OPSquare::total_bits | mask;
}
for (LanceArray::const_iterator p = lance_pieces.begin();
p != lance_pieces.end(); ++p)
{
const int value = static_cast<int>(*p);
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << OPSquare::total_bits | mask;
}
for (KnightArray::const_iterator p = knight_pieces.begin();
p != knight_pieces.end(); ++p)
{
const int value = static_cast<int>(*p);
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << OPSquare::total_bits | mask;
}
for (SilverArray::const_iterator p = silver_pieces.begin();
p != silver_pieces.end(); ++p)
{
const int value = static_cast<int>(*p);
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << OPSquare::total_bits | mask;
}
for (BishopArray::const_iterator p = bishop_pieces.begin();
p != bishop_pieces.end(); ++p)
{
const int value = static_cast<int>(*p);
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << OPSquare::total_bits | mask;
}
for (RookArray::const_iterator p = rook_pieces.begin();
p != rook_pieces.end(); ++p)
{
const int value = static_cast<int>(*p);
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << OPSquare::total_bits | mask;
}
for (GoldArray::const_iterator p = gold_pieces.begin();
p != gold_pieces.end(); ++p)
{
const int value = static_cast<int>(*p);
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << OSquare::total_bits | mask;
}
for (KingArray::const_iterator p = king_pieces.begin();
p != king_pieces.end(); ++p)
{
const char value = static_cast<char>(*p);
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << 8 | mask;
}
unsigned long value = 0;
if (turn == BLACK)
value = 0;
else
value = 1;
const boost::dynamic_bitset<> mask(total_bits, static_cast<unsigned long>(value));
bits = bits << 8 | mask;
return bits;
}
std::string
MiniBoard::toBase64() const
{
const boost::dynamic_bitset<> bits = toBits();
return misc::base64Encode(bits);
}
int fromBase64(const std::string& base64, MiniBoard& mb)
{
const boost::dynamic_bitset<> bits = misc::base64Decode(base64);
if (bits.size() == 0)
return 1;
assert(bits.size() == MiniBoard::total_bits);
for (int i=0; i<18; ++i)
{
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 1023ul);
const unsigned long value =
(bits >> (MiniBoard::total_bits - OPSquare::total_bits*(i+1))
& mask).to_ulong();
const OPSquare p(static_cast<int>(value));
mb.pawn_pieces.push_back(p);
}
for (int i=0; i<4; ++i)
{
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 1023ul);
const unsigned long value =
(bits >> (MiniBoard::total_bits - OPSquare::total_bits*(i+19))
& mask).to_ulong();
const OPSquare p(static_cast<int>(value));
mb.lance_pieces.push_back(p);
}
for (int i=0; i<4; ++i)
{
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 1023ul);
const unsigned long value =
(bits >> (MiniBoard::total_bits - OPSquare::total_bits*(i+23))
& mask).to_ulong();
const OPSquare p(static_cast<int>(value));
mb.knight_pieces.push_back(p);
}
for (int i=0; i<4; ++i)
{
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 1023ul);
const unsigned long value =
(bits >> (MiniBoard::total_bits - OPSquare::total_bits*(i+27))
& mask).to_ulong();
const OPSquare p(static_cast<int>(value));
mb.silver_pieces.push_back(p);
}
for (int i=0; i<2; ++i)
{
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 1023ul);
const unsigned long value =
(bits >> (MiniBoard::total_bits - OPSquare::total_bits*(i+31))
& mask).to_ulong();
const OPSquare p(static_cast<int>(value));
mb.bishop_pieces.push_back(p);
}
for (int i=0; i<2; ++i)
{
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 1023ul);
const unsigned long value =
(bits >> (MiniBoard::total_bits - OPSquare::total_bits*(i+33))
& mask).to_ulong();
const OPSquare p(static_cast<int>(value));
mb.rook_pieces.push_back(p);
}
for (int i=0; i<4; ++i)
{
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 511ul);
const unsigned long value =
(bits >> (MiniBoard::total_bits - OPSquare::total_bits*34 - OSquare::total_bits*(i+1)) & mask).to_ulong();
const OSquare p(static_cast<int>(value));
mb.gold_pieces.push_back(p);
}
for (int i=0; i<2; ++i)
{
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 255ul);
const unsigned long value =
(bits >> (MiniBoard::total_bits - OPSquare::total_bits*34 - OSquare::total_bits*4 - 8*(i+1)) & mask).to_ulong();
mb.king_pieces[i] = static_cast<char>(value);
}
const boost::dynamic_bitset<> mask(MiniBoard::total_bits, 255ul);
const unsigned long value = (bits & mask).to_ulong();
if ((value&1) == 0)
mb.turn = BLACK;
else
mb.turn = WHITE;
return 0;
}
} // namespace record
} // namespace osl
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
base64.h
osl::CArray< char, 2 >::const_iterator
std::array< char, Capacity >::const_iterator const_iterator
Definition container.h:77
osl::PieceMask::test
bool test(int num) const
Definition pieceMask.h:45
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptype
Ptype ptype() const
Definition basic_type.h:821
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::owner
Player owner() const
Definition basic_type.h:963
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::usedMask
const PieceMask & usedMask() const
Definition simpleState.h:131
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::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::MiniBoard::total_bits
static const size_t total_bits
Definition miniBoard.h:106
osl::book::MiniBoard::lance_pieces
LanceArray lance_pieces
Definition miniBoard.h:124
osl::book::MiniBoard::toBits
boost::dynamic_bitset toBits() const
Definition miniBoard.cc:178
osl::book::MiniBoard::toBase64
std::string toBase64() const
Definition miniBoard.cc:251
osl::book::MiniBoard::turn
Player turn
Definition miniBoard.h:131
osl::book::MiniBoard::silver_pieces
SilverArray silver_pieces
Definition miniBoard.h:126
osl::book::MiniBoard::king_pieces
KingArray king_pieces
Definition miniBoard.h:130
osl::book::MiniBoard::bishop_pieces
BishopArray bishop_pieces
Definition miniBoard.h:127
osl::book::MiniBoard::getState
SimpleState getState() const
Definition miniBoard.cc:112
osl::book::MiniBoard::rook_pieces
RookArray rook_pieces
Definition miniBoard.h:128
osl::book::MiniBoard::knight_pieces
KnightArray knight_pieces
Definition miniBoard.h:125
osl::book::MiniBoard::gold_pieces
GoldArray gold_pieces
Definition miniBoard.h:129
osl::book::MiniBoard::pawn_pieces
PawnArray pawn_pieces
Definition miniBoard.h:123
osl::book::MiniBoard::MiniBoard
MiniBoard()
Definition miniBoard.h:107
osl::book::OPSquare
Square, Owner, Promoted : 10 bits.
Definition miniBoard.h:65
osl::book::OPSquare::total_bits
static const size_t total_bits
Definition miniBoard.h:67
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::OSquare
Square, Owner: 9 bits.
Definition miniBoard.h:20
osl::book::OSquare::getSquare
Square getSquare() const
Definition miniBoard.h:39
osl::book::OSquare::getOwner
Player getOwner() const
Definition miniBoard.h:43
osl::book::OSquare::total_bits
static const size_t total_bits
Definition miniBoard.h:22
miniBoard.h
osl::book
Definition bookInMemory.h:12
osl::book::fromBase64
int fromBase64(const std::string &base64, MiniBoard &mb)
Definition miniBoard.cc:257
osl::misc::base64Encode
std::string base64Encode(boost::dynamic_bitset<> src)
Definition base64.cc:5
osl::misc::base64Decode
boost::dynamic_bitset base64Decode(std::string src)
Definition base64.cc:56
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::ROOK
@ ROOK
Definition basic_type.h:100
osl::BISHOP
@ BISHOP
Definition basic_type.h:99
osl::PAWN
@ PAWN
Definition basic_type.h:95
osl::KING
@ KING
Definition basic_type.h:93
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::SILVER
@ SILVER
Definition basic_type.h:98
osl::GOLD
@ GOLD
Definition basic_type.h:94
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::unpromote
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に，promote前の型を返す． promoteしていない型の時はそのまま返す
Definition basic_type.h:157
osl::WHITE
@ WHITE
Definition basic_type.h:10
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::promote
Ptype promote(Ptype ptype)
promote可能なptypeに対して，promote後の型を返す promote不可のptypeを与えてはいけない．
Definition basic_type.h:173
record.h
osl::book::oposition_sort
Definition miniBoard.cc:15
osl::book::oposition_sort::operator()
bool operator()(const OSquare &l, const OSquare &r)
Definition miniBoard.cc:16