00001
00002
00003 #ifndef _PAWN_MASK_STATE_H
00004 #define _PAWN_MASK_STATE_H
00005
00006 #include "osl/container/bitXmask.h"
00007 #include "osl/misc/carray.h"
00008 #include "osl/apply_move/doUndoMoveLockForward.h"
00009 #include "osl/apply_move/applyDoUndoXMove.h"
00010 #include "osl/state/simpleState.h"
00011 #include "osl/direction.h"
00012 #include "osl/ptype.h"
00013 #include "osl/piece.h"
00014 #include "osl/move.h"
00015 #include "osl/player.h"
00016 #include "osl/handicap.h"
00017
00018 #include <iosfwd>
00019
00020 namespace osl
00021 {
00022 namespace state
00023 {
00024 class PawnMaskState;
00025 std::ostream& operator<<(std::ostream& os,const PawnMaskState& state);
00026 bool operator==(const PawnMaskState& st1,const PawnMaskState& st2);
00027
00028
00032 class PawnMaskState : public SimpleState
00033 {
00034 public:
00036 typedef PawnMaskState pawn_mask_state_t;
00037 typedef SimpleState simple_state_t;
00038 typedef SimpleState effect_state_t;
00039 static const bool hasPawnMask=true;
00040 private:
00041 friend std::ostream& operator<<(std::ostream& os,const PawnMaskState& state);
00042 friend bool operator==(const PawnMaskState& st1,const PawnMaskState& st2);
00043 typedef PawnMaskState state_t;
00044 CArray<BitXmask,2> pawnMask;
00045
00046 friend class apply_move::ApplyDoUndoSimpleMove<BLACK,PawnMaskState>;
00047 friend class apply_move::ApplyDoUndoCaptureMove<BLACK,PawnMaskState>;
00048 friend class apply_move::ApplyDoUndoDropMove<BLACK,PawnMaskState>;
00049 friend class apply_move::ApplyDoUndoSimpleMove<WHITE,PawnMaskState>;
00050 friend class apply_move::ApplyDoUndoCaptureMove<WHITE,PawnMaskState>;
00051 friend class apply_move::ApplyDoUndoDropMove<WHITE,PawnMaskState>;
00052 friend class apply_move::DoUndoMoveLockSimple<PawnMaskState>;
00053 friend class apply_move::DoUndoMoveLockCapture<PawnMaskState>;
00054 friend class apply_move::DoUndoMoveLockDrop<PawnMaskState>;
00055 public:
00056
00057 explicit PawnMaskState();
00058 explicit PawnMaskState(SimpleState const& st);
00059 explicit PawnMaskState(Handicap h);
00060
00061 virtual ~PawnMaskState();
00062
00063 private:
00065 void init();
00066 public:
00068 void init(Handicap h);
00070 void initPawnMask();
00071
00072 bool isPawnMaskSet(Player player, int x) const
00073 {
00074 return pawnMask[playerToIndex(player)].isSet(x);
00075 }
00076
00077 template<Player P>
00078 bool isPawnMaskSet(int x)const {return isPawnMaskSet(P,x); }
00079
00080 void clearPawn(Player pl,Position pos){
00081 pawnMask[playerToIndex(pl)].clear(pos);
00082 }
00083 void setPawn(Player pl,Position pos){
00084 pawnMask[playerToIndex(pl)].set(pos);
00085 }
00086
00087 void setPiece(Player player,Position pos,Ptype ptype);
00088 void setPieceAll(Player player);
00089
00090
00091 bool isConsistent(bool showError=true) const;
00092
00093 void doSimpleMove(Position from, Position to, int promoteMask);
00094 void doDropMove(Position to,Ptype ptype);
00095 void doCaptureMove(Position from, Position to, Piece target,int promoteMask);
00096 };
00097
00098
00099 }
00100 using state::PawnMaskState;
00101
00102 namespace apply_move
00103 {
00104 template<Player P>
00105 struct ApplyDoUndoSimpleMove<P,PawnMaskState>
00106 {
00107 static void prologue(PawnMaskState& s,
00108 Position from, Position to, int promoteMask,
00109 Piece& oldPiece, int& num)
00110 {
00111 oldPiece=s.getPieceOnBoard(from);
00112 Piece newPiece=oldPiece.promoteWithMask(promoteMask);
00113 newPiece+=(to-from);
00114 num=oldPiece.number();
00115 s.setPieceOf(num,newPiece);
00116 s.setBoard(to,newPiece);
00117 s.setBoard(from,Piece::EMPTY());
00118 }
00119 static void epilogue(PawnMaskState& s, Position from, Position to,
00120 Piece oldPiece, int num)
00121 {
00122
00123 s.setPieceOf(num,oldPiece);
00124 s.setBoard(from,oldPiece);
00125 s.setBoard(to,Piece::EMPTY());
00126 }
00127 template <typename F>
00128 static void doUndoSimpleMove(PawnMaskState& s,
00129 Position from, Position to, int promoteMask,F& func);
00130 };
00131
00132 template<Player P>
00133 template <typename F>
00134 void ApplyDoUndoSimpleMove<P,PawnMaskState>::
00135 doUndoSimpleMove(PawnMaskState& s,
00136 Position from, Position to, int promoteMask,F& func)
00137 {
00138 Piece oldPiece;
00139 int num;
00140 prologue(s, from, to, promoteMask, oldPiece, num);
00141 if (promoteMask!=0 && num < PtypeTraits<PAWN>::indexLimit)
00142 {
00143 s.clearPawn(P,from);
00144 s.changeTurn();
00145 func(to);
00146 s.changeTurn();
00147 s.setPawn(P,from);
00148 }
00149 else{
00150 s.changeTurn();
00151 func(to);
00152 s.changeTurn();
00153 }
00154 epilogue(s, from, to, oldPiece, num);
00155 }
00156
00157 template<Player P>
00158 struct ApplyDoUndoDropMove<P,PawnMaskState>
00159 {
00160 typedef PawnMaskState state_t;
00161
00162 static void prologue(PawnMaskState& s, Ptype ptype, Position to,
00163 Piece& oldPiece, int& num, int& numIndex, int& numLow)
00164 {
00165 numIndex=Ptype_Table.getIndex(ptype);
00166 const mask_t ownMochigoma=
00167 s.standMask(P).getMask(numIndex) & Ptype_Table.getMaskLow(ptype);
00168 assert(ownMochigoma.any() || (s.dump(), 0));
00169 numLow=ownMochigoma.bsf();
00170 num=numLow|(numIndex<<5);
00171 oldPiece=s.getPieceOf(num);
00172 Piece p=oldPiece;
00173 p+=to-Position::STAND();
00174
00175 s.setPieceOf(num,p);
00176 s.setBoard(to,p);
00177 s.standMask(P).xorMask(numIndex,PieceMask::numToMask(numLow));
00178 }
00179 static void epilogue(PawnMaskState& s, Position to,
00180 Piece oldPiece, int num,
00181 int numIndex, int numLow)
00182 {
00183 s.standMask(P).xorMask(numIndex,PieceMask::numToMask(numLow));
00184 s.setBoard(to,Piece::EMPTY());
00185 s.setPieceOf(num,oldPiece);
00186 }
00187 template <typename F>
00188 static void doUndoDropMove(PawnMaskState& s,
00189 Position to, Ptype ptype,F& func);
00190 };
00191
00192
00193 template<Player P>
00194 template <typename F>
00195 void ApplyDoUndoDropMove<P,PawnMaskState>::
00196 doUndoDropMove(PawnMaskState& s,
00197 Position to, Ptype ptype,F& func)
00198 {
00199 Piece oldPiece;
00200 int num, numIndex, numLow;
00201 prologue(s, ptype, to, oldPiece, num, numIndex, numLow);
00202
00203 if (ptype==PAWN)
00204 {
00205 s.setPawn(P,to);
00206 s.changeTurn();
00207 func(to);
00208 s.changeTurn();
00209 s.clearPawn(P,to);
00210 }
00211 else
00212 {
00213 s.changeTurn();
00214 func(to);
00215 s.changeTurn();
00216 }
00217 epilogue(s, to, oldPiece, num, numIndex, numLow);
00218 }
00219
00220
00221 template<Player P>
00222 struct ApplyDoUndoCaptureMove<P,PawnMaskState>
00223 {
00224 typedef PawnMaskState state_t;
00225 template <typename F>
00226 static void doUndoCaptureMove(state_t& s,
00227 Position from,Position to, Piece p1, int promoteMask,F& func);
00228
00229 static
00230 void prologue(PawnMaskState& s,
00231 Position from, Position to, Piece target, int promoteMask,
00232 Ptype& capturePtype, Piece& oldPiece, int& num0,
00233 int& num1, int& num1Index, mask_t& num1Mask)
00234 {
00235 capturePtype=target.ptype();
00236 num1=target.number();
00237 num1Index=PieceMask::numToIndex(num1);
00238 num1Mask=PieceMask::numToMask(num1);
00239
00240 s.standMask(P).xorMask(num1Index,num1Mask);
00241 oldPiece=s.getPieceOnBoard(from);
00242 Piece newPiece=oldPiece.promoteWithMask(promoteMask);
00243 newPiece+=(to-from);
00244 num0=oldPiece.number();
00245 s.setPieceOf(num0,newPiece);
00246 s.setPieceOf(num1,target.captured());
00247 s.setBoard(to,newPiece);
00248 s.setBoard(from,Piece::EMPTY());
00249 }
00250 static
00251 void epilogue(PawnMaskState& s, Position from, Position to, Piece target,
00252 Piece oldPiece, int num0,
00253 int num1, int num1Index, mask_t num1Mask)
00254 {
00255 s.standMask(P).xorMask(num1Index,num1Mask);
00256 s.setPieceOf(num0,oldPiece);
00257 s.setPieceOf(num1,target);
00258 s.setBoard(from,oldPiece);
00259 s.setBoard(to,target);
00260 }
00261 };
00262
00263 template<Player P>
00264 template <typename F>
00265 void ApplyDoUndoCaptureMove<P,PawnMaskState>::
00266 doUndoCaptureMove(state_t& s,
00267 Position from,Position to, Piece target, int promoteMask,F& func)
00268 {
00269 Piece oldPiece;
00270 int num0, num1;
00271 int num1Index;
00272 mask_t num1Mask;
00273 Ptype capturePtype;
00274 prologue(s, from, to, target, promoteMask,
00275 capturePtype, oldPiece, num0, num1, num1Index, num1Mask);
00276
00277 s.changeTurn();
00278 if (capturePtype==PAWN){
00279 s.clearPawn(PlayerTraits<P>::opponent,to);
00280 if (promoteMask!=0 && num0<PtypeTraits<PAWN>::indexLimit){
00281 s.clearPawn(P,from);
00282 func(to);
00283 s.setPawn(P,from);
00284 }
00285 else{
00286 func(to);
00287 }
00288 s.setPawn(PlayerTraits<P>::opponent,to);
00289 }
00290 else if (promoteMask!=0 && num0<PtypeTraits<PAWN>::indexLimit){
00291 s.clearPawn(P,from);
00292 func(to);
00293 s.setPawn(P,from);
00294 }
00295 else{
00296 func(to);
00297 }
00298 s.changeTurn();
00299
00300 epilogue(s, from, to, target, oldPiece, num0, num1, num1Index, num1Mask);
00301 }
00302
00303 template<> class DoUndoMoveLockSimple<PawnMaskState>;
00304 template<> class DoUndoMoveLockDrop<PawnMaskState>;
00305 template<> class DoUndoMoveLockCapture<PawnMaskState>;
00306
00307 }
00308 }
00309
00310 #endif
00311
00312
00313
00314