piecePairEval.h
Go to the documentation of this file.
00001 /* piecePairEval.h
00002  */
00003 #ifndef _PIECE_PAIR_EVAL_H
00004 #define _PIECE_PAIR_EVAL_H
00005 
00006 #include "osl/eval/ppair/piecePairIndex.h"
00007 #include "osl/eval/pieceEval.h"
00008 #include "osl/eval/evalTraits.h"
00009 #include "osl/state/simpleState.h"
00010 #include "osl/move.h"
00011 #include "osl/misc/carray.h"
00012 #include <iosfwd>
00013 
00014 namespace osl
00015 {
00016   namespace container
00017   {
00018     class PieceValues;
00019   } // container
00020   
00021   namespace eval
00022   {
00023     namespace ppair
00024     {
00029       class PiecePairEvalBase
00030       {
00031       protected:
00032         int val;
00033         PiecePairEvalBase() : val(0)
00034         {
00035         }
00036         ~PiecePairEvalBase()
00037         {
00038         }
00039       public:
00041         static const int ROUND_UP = 2;
00042         static int roundUp(int v)
00043         {
00044           return v & (~(ROUND_UP-1)); 
00045         }
00046         int value() const       { return roundUp(val); }
00047         int rawValue() const { return val; }
00048         static int infty()
00049         {
00050           // ProgressEval で足してもoverflowしない値にする
00051           // PieceEval::infty() + 100*40*39/2
00052           return 150000;
00053         }
00054 
00055         static int captureValue(PtypeO ptypeo)
00056         {
00057           return PieceEval::captureValue(ptypeo); 
00058         }
00059       };
00060 
00061       template <class Table>
00062       class PiecePairEvalTableBase : public PiecePairEvalBase
00063       {
00064       protected:
00065         // 必ず継承して使う
00066         explicit PiecePairEvalTableBase(const SimpleState& state);
00067         ~PiecePairEvalTableBase() {}
00068       public:
00075         static int adjustPairs(const SimpleState& state,
00076                                unsigned int new_index);    
00077         static int adjustPairs(const SimpleState& state,
00078                                unsigned int old_index, unsigned int new_index);    
00079         static int adjustPairs(const SimpleState& state, 
00080                                unsigned int old_index, unsigned int old_index2, 
00081                                unsigned int new_index);
00082         static int diffAfterSimpleMove(const SimpleState& state,
00083                                        Square from, Square to, 
00084                                        int promote_mask)
00085         {
00086           const Piece old_piece=state.pieceAt(from);
00087           const PtypeO newPtypeO = promoteWithMask(old_piece.ptypeO(), promote_mask);
00088           const unsigned int old_index = PiecePairIndex::indexOf(old_piece);
00089           const unsigned int new_index = PiecePairIndex::indexOf(to, newPtypeO);
00090           return adjustPairs(state, old_index, new_index);
00091         }
00092         static int diffAfterDropMove(const SimpleState& state,Square to,PtypeO ptypeo)
00093         {
00094           const unsigned int new_index = PiecePairIndex::indexOf(to, ptypeo);
00095           return adjustPairs(state, new_index);
00096         }
00097         static int diffAfterCaptureMove(const SimpleState& state,
00098                                         Square from, Square to, 
00099                                         PtypeO victim,int promote_mask)
00100         {
00101           const Piece old_piece=state.pieceAt(from);
00102           const PtypeO newPtypeO = promoteWithMask(old_piece.ptypeO(), promote_mask);
00103           const unsigned int old_index = PiecePairIndex::indexOf(old_piece);
00104           const unsigned int new_index = PiecePairIndex::indexOf(to, newPtypeO);
00105 
00106           const unsigned int indexVictim = PiecePairIndex::indexOf(to, victim);
00107           return adjustPairs(state, old_index, indexVictim, 
00108                              new_index);
00109         }
00112         static int adjustPairsAfterMove(const SimpleState& state,
00113                                         unsigned int new_index);    
00114         static int adjustPairsAfterMove(const SimpleState& state,
00115                                         unsigned int old_index, unsigned int new_index);    
00116         static int adjustPairsAfterMove(const SimpleState& state, 
00117                                         unsigned int old_index, unsigned int old_index2, 
00118                                         unsigned int new_index);
00119         static int diffWithUpdate(const SimpleState& new_state, Move last_move)
00120         {
00121           const unsigned int new_index = PiecePairIndex::indexOf(last_move.to(), last_move.ptypeO());
00122           if (last_move.isDrop())
00123             return adjustPairsAfterMove(new_state, new_index);
00124           
00125           const unsigned int old_index = PiecePairIndex::indexOf(last_move.from(), last_move.oldPtypeO());
00126           if (last_move.capturePtype() == PTYPE_EMPTY)
00127             return adjustPairsAfterMove(new_state, old_index, new_index);
00128 
00129           const unsigned int index_victim = PiecePairIndex::indexOf(last_move.to(), last_move.capturePtypeO());
00130           return adjustPairsAfterMove(new_state, old_index, index_victim, new_index);
00131         }
00137         static void setValues(const SimpleState&, container::PieceValues&);
00138 
00139       private:
00140         static bool& initializationFlag();
00141       public:
00142         static bool initialized() { return initializationFlag(); } // read only
00143         static bool setUp(const char *filename);
00144         static bool setUp();
00145       };
00146 
00153       template <class Eval,class Table>
00154       class PiecePairEval : public PiecePairEvalTableBase<Table>
00155       {
00156       protected:
00157         // 必ず継承して使う
00158         explicit PiecePairEval(const SimpleState& state);
00159       public:
00160         typedef PiecePairEvalTableBase<Table> base_t;
00161         void changeTurn() {}
00164         int expect(const SimpleState& state, Move m) const;
00167         void update(const SimpleState& new_state, Move last_move)
00168         {
00169           base_t::val += Eval::diffWithUpdate(new_state, last_move);
00170         }
00171           
00172         static int diffWithMove(const SimpleState& state, Move move)
00173         {
00174           // TRICK: Eval::diffXXX とすることでsubclassによるoverrideを可能にする
00175           const Square from=move.from();
00176           const Square to=move.to();
00177           if (from.isPieceStand())
00178             return Eval::diffAfterDropMove(state, to, move.ptypeO());
00179 
00180           const Ptype ptypeCaptured=move.capturePtype();
00181           if (ptypeCaptured != PTYPE_EMPTY)
00182             return Eval::diffAfterCaptureMove(state, from, to,
00183                                               newPtypeO(alt(move.player()),
00184                                                         ptypeCaptured),
00185                                               move.promoteMask());
00186 
00187           return Eval::diffAfterSimpleMove(state, from,to,move.promoteMask());
00188         }
00189         
00190       };
00191 
00192     } // namespace ppair
00193   } // namespace eval
00194 } // namespace osl
00195 
00196 #endif /* _PIECE_PAIR_EVAL_H */
00197 // ;;; Local Variables:
00198 // ;;; mode:c++
00199 // ;;; c-basic-offset:2
00200 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines