説明を見る。00001
00002
00003 #ifndef OSL_SIMPLE_STATE_H
00004 #define OSL_SIMPLE_STATE_H
00005
00006 #include "osl/basic_type.h"
00007 #include "osl/bits/ptypeTable.h"
00008 #include "osl/bits/boardTable.h"
00009 #include "osl/bits/ptypeTraits.h"
00010 #include "osl/bits/pieceMask.h"
00011 #include "osl/bits/bitXmask.h"
00012 #include "osl/bits/effectContent.h"
00013 #include "osl/container.h"
00014
00015
00016 #include <iosfwd>
00017
00018 namespace osl
00019 {
00020 enum Handicap{
00021 HIRATE,
00022
00023
00024 };
00025 class SimpleState;
00026 std::ostream& operator<<(std::ostream& os,const SimpleState& state);
00032 bool operator==(const SimpleState& st1,const SimpleState& st2);
00033
00034 class SimpleState
00035 {
00036 private:
00037 friend std::ostream& operator<<(std::ostream& os,const SimpleState& state);
00038 friend bool operator==(const SimpleState& st1,const SimpleState& st2);
00039 typedef SimpleState state_t;
00040 public:
00041 static const bool hasPawnMask=true;
00042 protected:
00043 CArray<Piece,Square::SIZE> board
00044 #ifdef __GNUC__
00045 __attribute__((aligned(16)))
00046 #endif
00047 ;
00051 CArray<Piece,Piece::SIZE> pieces
00052 #ifdef __GNUC__
00053 __attribute__((aligned(16)))
00054 #endif
00055 ;
00056 CArray<PieceMask,2> stand_mask;
00057 CArray<BitXmask,2> pawnMask;
00058 CArray<CArray<char,PTYPE_SIZE-PTYPE_BASIC_MIN>,2> stand_count;
00059
00061 Player player_to_move;
00062 PieceMask used_mask;
00063 public:
00064
00065 explicit SimpleState();
00066 explicit SimpleState(Handicap h);
00067
00068 virtual ~SimpleState();
00070 void init();
00072 void init(Handicap h);
00073
00074 void initPawnMask();
00075 public:
00076 const Piece pieceOf(int num) const{
00077 return pieces[num];
00078 }
00079 void setPieceOf(int num,Piece p) {
00080 pieces[num]=p;
00081 }
00082 template<Player P>
00083 const Piece kingPiece() const{
00084 return pieceOf(KingTraits<P>::index);
00085 }
00086 const Piece kingPiece(Player P) const{
00087 assert(isValid(P));
00088 if (P==BLACK)
00089 return kingPiece<BLACK>();
00090 else
00091 return kingPiece<WHITE>();
00092 }
00093 template<Player P>
00094 Square kingSquare() const{
00095 return kingPiece<P>().square();
00096 }
00097 Square kingSquare(Player player) const{
00098 assert(isValid(player));
00099 if (player==BLACK)
00100 return kingSquare<BLACK>();
00101 else
00102 return kingSquare<WHITE>();
00103 }
00104 template <Ptype PTYPE>
00105 static int nthLimit() {
00106 return PtypeTraits<PTYPE>::indexLimit - PtypeTraits<PTYPE>::indexMin;
00107 }
00113 template <Ptype PTYPE>
00114 const Piece nth(int n) const {
00115 assert(0 <= n && n < nthLimit<PTYPE>());
00116 return pieceOf(PtypeTraits<PTYPE>::indexMin+n);
00117 }
00118
00119 void setBoard(Square sq,Piece piece)
00120 {
00121 board[sq.index()]=piece;
00122 }
00123 protected:
00124 PieceMask& standMask(Player p) {
00125 return stand_mask[p];
00126 }
00127 public:
00128 const PieceMask& standMask(Player p) const {
00129 return stand_mask[p];
00130 }
00131 const PieceMask& usedMask() const {return used_mask;}
00132 bool isOffBoard(int num) const{
00133 return standMask(BLACK).test(num)
00134 || standMask(WHITE).test(num);
00135 }
00136
00138 void clearPawn(Player pl,Square sq){
00139 pawnMask[pl].clear(sq);
00140 }
00142 void setPawn(Player pl,Square sq){
00143 pawnMask[pl].set(sq);
00144 }
00145 public:
00146 bool isPawnMaskSet(Player player, int x) const
00147 {
00148 return pawnMask[player].isSet(x);
00149 }
00150
00151 template<Player P>
00152 bool isPawnMaskSet(int x)const {return isPawnMaskSet(P,x); }
00153
00155 bool canDropPawnTo(Player player, int x) const
00156 {
00157 return hasPieceOnStand<PAWN>(player) && ! isPawnMaskSet(player, x);
00158 }
00159
00160 void setPiece(Player player,Square sq,Ptype ptype);
00161 void setPieceAll(Player player);
00162
00167 const Piece pieceAt(Square sq) const { return board[sq.index()];}
00168 const Piece operator[](Square sq) const { return pieceAt(sq);}
00169 const Piece* getPiecePtr(Square sq) const { return &board[sq.index()];}
00170 const Piece pieceOnBoard(Square sq) const
00171 {
00172 assert(sq.isOnBoard());
00173 return pieceAt(sq);
00174 }
00175
00176 bool isOnBoard(int num) const {
00177 return pieceOf(num).isOnBoard();
00178 }
00182 int countPiecesOnStand(Player pl,Ptype ptype) const {
00183 assert(isBasic(ptype));
00184 return stand_count[pl][ptype-PTYPE_BASIC_MIN];
00185 }
00187 template <Ptype Type>
00188 int countPiecesOnStand(Player pl) const {
00189 return countPiecesOnStand(pl, Type);
00190 }
00191 bool hasPieceOnStand(Player player,Ptype ptype) const{
00192 return countPiecesOnStand(player, ptype)!=0;
00193 }
00194 template<Ptype T>
00195 bool hasPieceOnStand(Player P) const {
00196 return countPiecesOnStand(P, T);
00197 }
00198 private:
00199 int countPiecesOnStandBit(Player pl,Ptype ptype) const {
00200 return (standMask(pl).getMask(0)
00201 & Ptype_Table.getMaskLow(ptype)).countBit();
00202 }
00203 public:
00208 Piece nextPiece(Square cur, Offset diff) const
00209 {
00210 assert(! diff.zero());
00211 cur += diff;
00212 while (pieceAt(cur) == Piece::EMPTY())
00213 cur += diff;
00214 return pieceAt(cur);
00215 }
00216
00217 void setTurn(Player player) {
00218 player_to_move=player;
00219 }
00220 Player turn() const{
00221 return player_to_move;
00222 }
00226 void changeTurn() {
00227 player_to_move = alt(player_to_move);
00228 }
00229
00230 bool isConsistent(bool show_error=true) const;
00232 template <bool show_error>
00233 bool isAlmostValidMove(Move move) const;
00241 bool isAlmostValidMove(Move move,bool show_error=true) const;
00248 bool isValidMove(Move move,bool show_error=true) const;
00249 protected:
00250 template <bool show_error> bool isAlmostValidDrop(Move move) const;
00251 template <bool show_error> bool testValidityOtherThanEffect(Move move) const;
00252 public:
00257 static bool isValidMoveByRule(Move move,bool show_error);
00258
00267 bool isEmptyBetween(Square from, Square to,Offset offset,bool pieceExistsAtTo=false) const
00268 #ifdef __GNUC__
00269 __attribute__ ((pure))
00270 #endif
00271 {
00272 assert(from.isOnBoard());
00273 assert(! offset.zero());
00274 assert(offset==Board_Table.getShortOffset(Offset32(to,from)));
00275 Square sq=from+offset;
00276 for (; pieceAt(sq).isEmpty(); sq+=offset) {
00277 if (!pieceExistsAtTo && sq==to)
00278 return true;
00279 }
00280 return sq==to;
00281
00282 }
00289 bool
00290 #ifdef __GNUC__
00291 __attribute__ ((pure))
00292 #endif
00293 isEmptyBetween(Square from, Square to,bool noSpaceAtTo=false) const{
00294 assert(from.isOnBoard());
00295 Offset offset=Board_Table.getShortOffset(Offset32(to,from));
00296 assert(! offset.zero());
00297 return isEmptyBetween(from,to,offset,noSpaceAtTo);
00298 }
00299
00301 bool dump() const;
00305 const SimpleState emulateCapture(Piece from, Player new_owner) const;
00306
00310 const SimpleState emulateHandPiece(Player from, Player to, Ptype ptype) const;
00311 const SimpleState rotate180() const;
00312 const SimpleState flipHorizontal() const;
00313 };
00314
00315 }
00316
00317 #endif
00318
00319
00320
00321