1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include "osl/record/record.h"
#include "osl/record/compactBoard.h"
#include "osl/misc/base64.h"
#include <boost/dynamic_bitset.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <algorithm>
#include <sstream>
int osl::record::
OPiece::position2Bits(const Square& pos)
{
return pos.isPieceStand() ? 0 : pos.x() << 4 | pos.y(); // 8 bits
}
osl::Square osl::record::
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 record
{
struct opiece_sort
{
bool operator()(const OPiece& l, const OPiece& 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 l.getPtype() < r.getPtype();
}
}
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;
}
}
}
};
}
}
osl::record::
CompactBoard::CompactBoard(const SimpleState& state)
{
pieces.reserve(40);
for (int i = 0; i < 40; i++)
{
if(state.usedMask().test(i))
pieces.push_back(OPiece(state.pieceOf(i)));
}
std::sort(pieces.begin(), pieces.end(), opiece_sort());
player_to_move = state.turn();
}
osl::SimpleState osl::record::
CompactBoard::getState() const
{
SimpleState state;
state.init();
BOOST_FOREACH(const OPiece& p, pieces) {
state.setPiece(p.getOwner(), p.getSquare(), p.getPtype());
}
state.setTurn(turn());
state.initPawnMask();
return state;
}
bool osl::record::
operator==(const CompactBoard& lhs, const CompactBoard& rhs)
{
return (lhs.turn() == rhs.turn()) && (lhs.pieces == rhs.pieces);
}
std::ostream& osl::record::
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::record::
operator>>(std::istream& is, CompactBoard& c)
{
assert(c.pieces.size() == 0);
for (unsigned int i = 0; i < 40; i++)
{
c.pieces.push_back(OPiece(readInt(is)));
}
c.player_to_move = static_cast<Player>(readInt(is));
return is;
}
#ifndef MINIMAL
std::string osl::record::
CompactBoard::toBase64() const
{
const static size_t ninteger = 41;
const static size_t integer_size = 32;
const static size_t size = ninteger*integer_size;
std::stringstream ss;
ss << *this;
ss.clear();
ss.seekg(0, std::ios::beg);
boost::dynamic_bitset<> bits(size);
for (size_t i = 0; i < ninteger; ++i)
{
const unsigned int tmp = static_cast<unsigned int>(readInt(ss));
const boost::dynamic_bitset<> mask(size, static_cast<unsigned long>(tmp));
bits = (bits << integer_size) | mask;
}
return misc::base64Encode(bits);
}
const osl::record::CompactBoard osl::record::
CompactBoard::fromBase64(const std::string& str)
{
const boost::dynamic_bitset<> bits = misc::base64Decode(str);
std::stringstream ss;
assert(bits.size()%32 == 0);
const boost::dynamic_bitset<> mask(bits.size(), 4294967295ul);
for (size_t i=0; i<bits.size()/32; ++i)
{
const unsigned long tmp = ((bits >> ((bits.size()/32-1-i)*32)) & mask).to_ulong();
writeInt(ss, static_cast<int>(tmp));
}
ss.clear();
ss.seekg(0, std::ios::beg);
CompactBoard cb;
ss >> cb;
return cb;
}
#endif
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
|