00001 #ifndef _MINI_BOARD_H 00002 #define _MINI_BOARD_H 00003 #include "osl/book/compactBoard.h" 00004 #include "osl/simpleState.h" 00005 #include "boost/dynamic_bitset.hpp" 00006 #include <string> 00007 #include <vector> 00008 00009 namespace osl 00010 { 00011 namespace book 00012 { 00019 class OSquare 00020 { 00021 public: 00022 static const size_t total_bits; 00023 OSquare() : value(0) {} 00024 OSquare(const Piece& p) 00025 { 00026 const Square pos = p.square(); 00027 const int bitPos = OPiece::position2Bits(pos); // 8 bits 00028 int owner = 0; 00029 if (p.owner() == BLACK) 00030 owner = 0; 00031 else 00032 owner = 1; 00033 value = owner << 8 | bitPos; // 9 bits 00034 } 00035 OSquare(const int i) 00036 { 00037 value = i; 00038 } 00039 Square getSquare() const 00040 { 00041 return OPiece::bits2Square(value); 00042 } 00043 Player getOwner() const 00044 { 00045 const int owner = value >> 8 & 1; 00046 if (owner == 0) 00047 return BLACK; 00048 else 00049 return WHITE; 00050 } 00051 operator int() const { return value; } 00052 protected: 00053 int value; 00054 }; 00055 00064 class OPSquare : public OSquare 00065 { 00066 public: 00067 static const size_t total_bits; 00068 OPSquare() : OSquare() {} 00069 OPSquare(const Piece& p) 00070 : OSquare(p) 00071 { 00072 int is_promoted = 0; 00073 if (p.isPromoted()) 00074 is_promoted = 1; 00075 value = is_promoted << 9 | value; // 10 bits 00076 } 00077 OPSquare(const int i) 00078 : OSquare(i) {} 00079 bool isPromoted() const 00080 { 00081 const int is_promoted = value >> 9 & 1; 00082 if (is_promoted == 0) 00083 return false; 00084 else 00085 return true; 00086 } 00087 }; 00088 00103 class MiniBoard 00104 { 00105 public: 00106 static const size_t total_bits; 00107 MiniBoard() {} 00108 explicit MiniBoard(const SimpleState& state); 00109 SimpleState getState() const; 00110 boost::dynamic_bitset<> toBits() const; 00111 std::string toBase64() const; 00112 private: 00113 typedef std::vector<OPSquare> PawnArray; // 10 bits x 18 = 180 00114 typedef std::vector<OPSquare> LanceArray; // 10 x 4 = 40 00115 typedef std::vector<OPSquare> KnightArray; // 10 x 4 = 40 00116 typedef std::vector<OPSquare> SilverArray; // 10 x 4 = 40 00117 typedef std::vector<OPSquare> BishopArray; // 10 x 2 = 20 00118 typedef std::vector<OPSquare> RookArray; // 10 x 2 = 20 00119 typedef std::vector<OSquare> GoldArray; // 9 x 4 = 36 00120 typedef osl::CArray<char, 2> KingArray; // 8 x 2 = 16 00121 // ------------------ 00122 // 392 00123 PawnArray pawn_pieces; 00124 LanceArray lance_pieces; 00125 KnightArray knight_pieces; 00126 SilverArray silver_pieces; 00127 BishopArray bishop_pieces; 00128 RookArray rook_pieces; 00129 GoldArray gold_pieces; 00130 KingArray king_pieces; 00131 Player turn; 00132 00135 friend int fromBase64(const std::string& base64, MiniBoard& mb); 00136 }; 00137 } 00138 } 00139 00140 #endif // _MINI_BOARD_H 00141 /* ------------------------------------------------------------------------- */ 00142 // ;;; Local Variables: 00143 // ;;; mode:c++ 00144 // ;;; c-basic-offset:2 00145 // ;;; End: