miniBoardChar50.cc
Go to the documentation of this file.
00001 /* miniBoardChar50.cc
00002  */
00003 #include "osl/record/miniBoardChar50.h"
00004 #include "osl/record/compactBoard.h"
00005 #include "osl/ptypeTable.h"
00006 #include "osl/pieceTable.h"
00007 #include <boost/tuple/tuple.hpp>
00008 #include <boost/tuple/tuple_comparison.hpp>
00009 #include <algorithm>
00010 #include <stdexcept>
00011 
00012 osl::record::
00013 MiniBoardChar50::MiniBoardChar50()
00014 {
00015   data.fill(0);
00016 }
00017 
00018 osl::record::
00019 MiniBoardChar50::MiniBoardChar50(const SimpleState& org)
00020 {
00021   data.fill(0);
00022   SimpleState board = (org.turn() == BLACK) ? org : org.rotate180();
00023   CArray<boost::tuple<int/* =Ptype*/,bool,int /* =Player*/,Square>, Piece::SIZE> pieces;
00024   for (int i=0; i<Piece::SIZE; ++i) 
00025   {
00026     const Piece p = board.pieceOf(i);
00027     const int ptype_index = Ptype_Table.getIndexMin(unpromote(p.ptype()));
00028     pieces[i] = boost::make_tuple(ptype_index, p.isPromoted(), p.owner(), p.square());
00029   }
00030   std::sort(pieces.begin(), pieces.end());
00031   for (int i=0; i<Piece::SIZE; ++i) 
00032   {
00033     data[i] = OPiece::position2Bits(pieces[i].get<3>());
00034     data[Piece::SIZE + i/8] |= playerToIndex(static_cast<Player>(pieces[i].get<2>())) << (i%8);
00035     data[Piece::SIZE + i/8 + 5] |= pieces[i].get<1>() << (i%8);
00036   }
00037 }
00038 
00039 osl::record::
00040 MiniBoardChar50::MiniBoardChar50(const std::string& src)
00041 {
00042   if (src.size() != data.size())
00043     throw std::runtime_error("bad argument in MiniBoardChar50::MiniBoardChar50(const std::string&)");
00044   std::copy(src.begin(), src.end(), data.begin());
00045 }
00046 
00047 const osl::SimpleState osl::record::
00048 MiniBoardChar50::toSimpleState(Player turn) const
00049 {
00050   SimpleState state;
00051   state.init();
00052 
00053   for (int i = 0; i<Piece::SIZE; i++)
00054   {
00055     const Square position = OPiece::bits2Square(data[i]);
00056     const Player owner = indexToPlayer((data[40+i/8] >> (i%8)) & 1);
00057     const bool promoted = (data[40+i/8+5] >> (i%8)) & 1;
00058     Ptype ptype = Piece_Table.getPtypeOf(i);
00059     if (promoted)
00060       ptype = promote(ptype);
00061     state.setPiece(owner, position, ptype);
00062   }
00063   state.setTurn(BLACK);
00064   state.initPawnMask();
00065   if (turn != BLACK)
00066     state = state.rotate180();
00067   assert(state.turn() == turn);
00068   return state;
00069 }
00070 
00071 const std::string osl::record::
00072 MiniBoardChar50::toString() const
00073 {
00074   return std::string(data.begin(), data.end());
00075 }
00076 
00077 bool osl::record::operator<(const MiniBoardChar50& l, const MiniBoardChar50& r)
00078 {
00079   return std::lexicographical_compare(l.data.begin(), l.data.end(), 
00080                                       r.data.begin(), r.data.end());
00081 }
00082 bool osl::record::operator==(const MiniBoardChar50& l, const MiniBoardChar50& r)
00083 {
00084   return std::equal(l.data.begin(), l.data.end(), r.data.begin());
00085 }
00086 
00087 // ;;; Local Variables:
00088 // ;;; mode:c++
00089 // ;;; c-basic-offset:2
00090 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines