All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
miniBoardChar50.cc
Go to the documentation of this file.
1 /* miniBoardChar50.cc
2  */
5 #include "osl/ptypeTable.h"
6 #include "osl/pieceTable.h"
7 #include <boost/tuple/tuple.hpp>
8 #include <boost/tuple/tuple_comparison.hpp>
9 #include <algorithm>
10 #include <stdexcept>
11 
14 {
15  data.fill(0);
16 }
17 
19 MiniBoardChar50::MiniBoardChar50(const SimpleState& org)
20 {
21  data.fill(0);
22  SimpleState board = (org.turn() == BLACK) ? org : org.rotate180();
23  CArray<boost::tuple<int/* =Ptype*/,bool,int /* =Player*/,Square>, Piece::SIZE> pieces;
24  for (int i=0; i<Piece::SIZE; ++i)
25  {
26  const Piece p = board.pieceOf(i);
27  const int ptype_index = Ptype_Table.getIndexMin(unpromote(p.ptype()));
28  pieces[i] = boost::make_tuple(ptype_index, p.isPromoted(), p.owner(), p.square());
29  }
30  std::sort(pieces.begin(), pieces.end());
31  for (int i=0; i<Piece::SIZE; ++i)
32  {
33  data[i] = OPiece::position2Bits(pieces[i].get<3>());
34  data[Piece::SIZE + i/8] |= playerToIndex(static_cast<Player>(pieces[i].get<2>())) << (i%8);
35  data[Piece::SIZE + i/8 + 5] |= pieces[i].get<1>() << (i%8);
36  }
37 }
38 
40 MiniBoardChar50::MiniBoardChar50(const std::string& src)
41 {
42  if (src.size() != data.size())
43  throw std::runtime_error("bad argument in MiniBoardChar50::MiniBoardChar50(const std::string&)");
44  std::copy(src.begin(), src.end(), data.begin());
45 }
46 
47 const osl::SimpleState osl::record::
49 {
50  SimpleState state;
51  state.init();
52 
53  for (int i = 0; i<Piece::SIZE; i++)
54  {
55  const Square position = OPiece::bits2Square(data[i]);
56  const Player owner = indexToPlayer((data[40+i/8] >> (i%8)) & 1);
57  const bool promoted = (data[40+i/8+5] >> (i%8)) & 1;
58  Ptype ptype = Piece_Table.getPtypeOf(i);
59  if (promoted)
60  ptype = promote(ptype);
61  state.setPiece(owner, position, ptype);
62  }
63  state.setTurn(BLACK);
64  state.initPawnMask();
65  if (turn != BLACK)
66  state = state.rotate180();
67  assert(state.turn() == turn);
68  return state;
69 }
70 
71 const std::string osl::record::
73 {
74  return std::string(data.begin(), data.end());
75 }
76 
78 {
79  return std::lexicographical_compare(l.data.begin(), l.data.end(),
80  r.data.begin(), r.data.end());
81 }
83 {
84  return std::equal(l.data.begin(), l.data.end(), r.data.begin());
85 }
86 
87 // ;;; Local Variables:
88 // ;;; mode:c++
89 // ;;; c-basic-offset:2
90 // ;;; End: