moveGenerator.h
Go to the documentation of this file.
00001 /* moveGenerator.h
00002  */
00003 #ifndef OSL_MOVEGENERATOR_H
00004 #define OSL_MOVEGENERATOR_H
00005 
00006 #include "osl/search/simpleHashRecord.h"
00007 #include "osl/search/searchState2.h"
00008 #include "osl/rating/ratingEnv.h"
00009 #include "osl/progress/progress32.h"
00010 #include "osl/container/moveLogProbVector.h"
00011 #include "osl/container/moveStack.h"
00012 #include "osl/misc/carray.h"
00013 #include "osl/misc/carray2d.h"
00014 #include "osl/misc/cstdint.h"
00015 
00016 namespace osl
00017 {
00018   namespace search
00019   {
00020     namespace analyzer
00021     {
00022       class CategoryMoveVector;
00023     }
00024     class SearchState2;
00025     class MoveMarker
00026     {
00027       typedef uint8_t value_t;
00028       CArray2d<value_t,Offset::BOARD_HEIGHT*9,Piece::SIZE*2+PTYPE_SIZE> marker;
00029       value_t cur;
00030     public:
00031       MoveMarker();
00032       void clear();
00033       static unsigned int pieceIndex(const NumEffectState& state, Move m)
00034       {
00035         if (m.isPass() || m.isDrop()) 
00036           return Piece::SIZE*2+m.ptype();
00037         int base = state.pieceOnBoard(m.from()).number();
00038         if (m.isPromotion())
00039           return base+ Piece::SIZE;
00040         return base;
00041       }
00042       static unsigned int toIndex(Move m)
00043       {
00044         return m.to().index()-Square::onBoardMin().index();
00045       }
00046       void registerMove(const NumEffectState& state, Move m)
00047       {
00048         marker(toIndex(m), pieceIndex(state,m)) = cur;
00049       }
00050       bool registerIfNew(const NumEffectState& state, Move m);
00051       bool registered(const NumEffectState& state, Move m) const;
00052     };
00053     class MoveGenerator
00054     {
00055       enum State { 
00056         INITIAL, KING_ESCAPE, TAKE_BACK, BREAK_THREATMATE, CAPTURE, TACTICAL_FINISH, 
00057         TESUJI, ALL, FINISH 
00058       };
00059       typedef void (MoveGenerator::*generator_t)(const SearchState2&);
00060       static const CArray2d<generator_t, 2, FINISH> Generators;
00061       static const CArray<const char *, FINISH> GeneratorNames;
00062       MoveLogProbVector moves;
00063       int cur_state;
00064       size_t cur_index;
00065       const SimpleHashRecord *record;
00066       int limit;
00067       int tried;
00068       MoveMarker marker;
00069       RatingEnv env;
00070       Progress32 progress;
00071       Move eval_suggestion;
00072 #ifndef MINIMAL
00073       bool in_quiesce;
00074 #endif
00075       bool in_pv;
00076     public:
00077       MoveGenerator();
00078       template <class EvalT>
00079       void init(int limit, const SimpleHashRecord *record, const EvalT&,
00080                 const NumEffectState&, bool in_pv, Move hash_move, bool quiesce=false);
00082       template <Player P>
00083       const MoveLogProb nextTacticalMove(const SearchState2& state) 
00084       {
00085         assert(cur_state < TACTICAL_FINISH);
00086         if (cur_index < moves.size()) {
00087           ++tried;
00088           return moves[cur_index++];
00089         }
00090         return nextTacticalMoveWithGeneration<P>(state);
00091       }
00092       template <Player P>
00093       const MoveLogProb nextMove(const SearchState2& state) 
00094       {
00095         assert(cur_state >= TACTICAL_FINISH);
00096         if (cur_index < moves.size()) {
00097           ++tried;
00098           return moves[cur_index++];
00099         }
00100         if (cur_state < FINISH)
00101           return nextMoveWithGeneration<P>(state);
00102         return MoveLogProb();
00103       }
00104 
00106       void registerMove(const NumEffectState& state, Move m)
00107       {
00108         ++tried;
00109         if (! m.isNormal())
00110           return;
00111         marker.registerMove(state, m);
00112       }
00113       
00114       int triedMoves() const { return tried; }
00115       const PieceMask& myPins() const { return env.my_pin; }
00116       void dump() const;
00117 
00118       // construct直後に呼ぶこと
00119       void generateAll(Player P, const SearchState2& state, 
00120                        analyzer::CategoryMoveVector&);
00121       template <Player P>
00122       void generateAll(const SearchState2&, MoveLogProbVector&);
00123       void generateAll(Player P, const SearchState2& state, MoveLogProbVector& out);
00124 
00125       const MoveLogProbVector& generated() const { return moves; }
00126       static int captureValue(Ptype);
00127       template <Player P>
00128       void quiesceCapture(const NumEffectState&, Square);
00129     private:
00130       template <Player P>
00131       const MoveLogProb nextMoveWithGeneration(const SearchState2&) ;
00132       template <Player P>
00133       const MoveLogProb nextTacticalMoveWithGeneration(const SearchState2&) ;
00134       template <Player P>
00135       void generateKingEscape(const SearchState2& state);
00136       template <Player P>
00137       void generateTakeBack(const SearchState2& state);
00138       template <Player P>
00139       void generateBreakThreatmate(const SearchState2& state);
00140       template <Player P>
00141       void generateCapture(const SearchState2& state);
00142       template <Player P>
00143       void generateTesuji(const SearchState2& state);
00144       template <Player P>
00145       void generateAllExp(const SearchState2& state);
00146       template <Player P>
00147       void generateAll(const SearchState2& state);
00148       template <Player P>
00149       void addCapture(const NumEffectState&, const RatingEnv&, const MoveVector&);
00150     public:
00152       static void initOnce();
00153     };
00154   }
00155 }
00156 
00157 
00158 #endif /* OSL_MOVEGENERATOR_H */
00159 // ;;; Local Variables:
00160 // ;;; mode:c++
00161 // ;;; c-basic-offset:2
00162 // ;;; coding:utf-8
00163 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines