説明を見る。00001 #include "osl/book/compactBoard.h"
00002 #include <iostream>
00003 #include <algorithm>
00004 #include <sstream>
00005
00006 int osl::book::
00007 OPiece::position2Bits(const Square& pos)
00008 {
00009 return pos.isPieceStand() ? 0 : ((pos.x() << 4) | pos.y());
00010 }
00011
00012 osl::Square osl::book::
00013 OPiece::bits2Square(const int bit_position)
00014 {
00015 if ((bit_position & 0xff) == 0)
00016 return Square::STAND();
00017 else
00018 return Square((bit_position >> 4) & 0xf, bit_position & 0xf);
00019 }
00020
00021 namespace osl
00022 {
00023 namespace book
00024 {
00025
00026 struct opiece_sort
00027 {
00028 bool operator()(const OPiece& l, const OPiece& r) {
00029 if (l.square() == Square::STAND() || r.square() == Square::STAND()) {
00030 if (l.square() != r.square())
00031 return l.square() == Square::STAND();
00032 if (l.owner() != r.owner())
00033 return l.owner() == WHITE;
00034 return l.ptype() < r.ptype();
00035 }
00036 if (l.square().x() != r.square().x())
00037 return l.square().x() < r.square().x();
00038 return l.square().y() < r.square().y();
00039 }
00040 };
00041 }
00042 }
00043
00044 osl::book::
00045 CompactBoard::CompactBoard(const SimpleState& state)
00046 {
00047 piece_vector.reserve(40);
00048 for (int i = 0; i < 40; i++)
00049 {
00050 if(state.usedMask().test(i))
00051 piece_vector.push_back(OPiece(state.pieceOf(i)));
00052 }
00053 std::sort(piece_vector.begin(), piece_vector.end(), opiece_sort());
00054 player_to_move = state.turn();
00055 }
00056
00057 osl::SimpleState osl::book::
00058 CompactBoard::state() const
00059 {
00060
00061 SimpleState state;
00062 state.init();
00063
00064 for (const OPiece& p: piece_vector) {
00065 state.setPiece(p.owner(), p.square(), p.ptype());
00066 }
00067 state.setTurn(turn());
00068 state.initPawnMask();
00069 return state;
00070 }
00071
00072 bool osl::book::
00073 operator==(const CompactBoard& lhs, const CompactBoard& rhs)
00074 {
00075 return (lhs.turn() == rhs.turn()) && (lhs.pieces() == rhs.pieces());
00076 }
00077
00078 std::ostream& osl::book::
00079 operator<<(std::ostream& os, const CompactBoard& c)
00080 {
00081
00082 for (unsigned int i = 0; i < c.pieces().size(); i++)
00083 {
00084 writeInt(os, static_cast<int>(c.pieces()[i]));
00085 }
00086 writeInt(os, static_cast<int>(c.turn()));
00087 return os;
00088 }
00089
00090 std::istream& osl::book::
00091 operator>>(std::istream& is, CompactBoard& c)
00092 {
00093 assert(c.piece_vector.size() == 0);
00094
00095 for (unsigned int i = 0; i < 40; i++)
00096 {
00097 c.piece_vector.push_back(OPiece(readInt(is)));
00098 }
00099 c.player_to_move = static_cast<Player>(readInt(is));
00100 return is;
00101 }
00102
00103
00104
00105
00106
00107