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