All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
miniBoard.h
Go to the documentation of this file.
1 #ifndef _MINI_BOARD_H
2 #define _MINI_BOARD_H
5 #include "osl/stl/vector.h"
6 #include "boost/dynamic_bitset.hpp"
7 #include <string>
8 
9 namespace osl
10 {
11  namespace record
12  {
19  class OSquare
20  {
21  public:
22  static const size_t total_bits;
23  OSquare() : value(0) {}
24  OSquare(const Piece& p)
25  {
26  const Square pos = p.square();
27  const int bitPos = OPiece::position2Bits(pos); // 8 bits
28  int owner = 0;
29  if (p.owner() == BLACK)
30  owner = 0;
31  else
32  owner = 1;
33  value = owner << 8 | bitPos; // 9 bits
34  }
35  OSquare(const int i)
36  {
37  value = i;
38  }
39  Square getSquare() const
40  {
41  return OPiece::bits2Square(value);
42  }
43  Player getOwner() const
44  {
45  const int owner = value >> 8 & 1;
46  if (owner == 0)
47  return BLACK;
48  else
49  return WHITE;
50  }
51  operator int() const { return value; }
52  protected:
53  int value;
54  };
55 
64  class OPSquare : public OSquare
65  {
66  public:
67  static const size_t total_bits;
68  OPSquare() : OSquare() {}
69  OPSquare(const Piece& p)
70  : OSquare(p)
71  {
72  int is_promoted = 0;
73  if (p.isPromoted())
74  is_promoted = 1;
75  value = is_promoted << 9 | value; // 10 bits
76  }
77  OPSquare(const int i)
78  : OSquare(i) {}
79  bool isPromoted() const
80  {
81  const int is_promoted = value >> 9 & 1;
82  if (is_promoted == 0)
83  return false;
84  else
85  return true;
86  }
87  };
88 
103  class MiniBoard
104  {
105  public:
106  static const size_t total_bits;
108  explicit MiniBoard(const state::SimpleState& state);
109  SimpleState getState() const;
110  boost::dynamic_bitset<> toBits() const;
111  std::string toBase64() const;
112  private:
113  typedef osl::vector<OPSquare> PawnArray; // 10 bits x 18 = 180
114  typedef osl::vector<OPSquare> LanceArray; // 10 x 4 = 40
115  typedef osl::vector<OPSquare> KnightArray; // 10 x 4 = 40
116  typedef osl::vector<OPSquare> SilverArray; // 10 x 4 = 40
117  typedef osl::vector<OPSquare> BishopArray; // 10 x 2 = 20
118  typedef osl::vector<OPSquare> RookArray; // 10 x 2 = 20
119  typedef osl::vector<OSquare> GoldArray; // 9 x 4 = 36
120  typedef osl::CArray<char, 2> KingArray; // 8 x 2 = 16
121  // ------------------
122  // 392
132 
135  friend int fromBase64(const std::string& base64, MiniBoard& mb);
136  };
137  }
138 }
139 
140 #endif // _MINI_BOARD_H
141 /* ------------------------------------------------------------------------- */
142 // ;;; Local Variables:
143 // ;;; mode:c++
144 // ;;; c-basic-offset:2
145 // ;;; End: