All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
piecePairEval.h
Go to the documentation of this file.
1 /* piecePairEval.h
2  */
3 #ifndef _PIECE_PAIR_EVAL_H
4 #define _PIECE_PAIR_EVAL_H
5 
7 #include "osl/eval/pieceEval.h"
8 #include "osl/eval/evalTraits.h"
10 #include "osl/move.h"
11 #include "osl/misc/carray.h"
12 #include <iosfwd>
13 
14 namespace osl
15 {
16  namespace container
17  {
18  class PieceValues;
19  } // container
20 
21  namespace eval
22  {
23  namespace ppair
24  {
30  {
31  protected:
32  int val;
34  {
35  }
37  {
38  }
39  public:
41  static const int ROUND_UP = 2;
42  static int roundUp(int v)
43  {
44  return v & (~(ROUND_UP-1));
45  }
46  int value() const { return roundUp(val); }
47  int rawValue() const { return val; }
48  static int infty()
49  {
50  // ProgressEval で足してもoverflowしない値にする
51  // PieceEval::infty() + 100*40*39/2
52  return 150000;
53  }
54 
55  static int captureValue(PtypeO ptypeo)
56  {
57  return PieceEval::captureValue(ptypeo);
58  }
59  };
60 
61  template <class Table>
63  {
64  protected:
65  // 必ず継承して使う
66  explicit PiecePairEvalTableBase(const SimpleState& state);
68  public:
75  static int adjustPairs(const SimpleState& state,
76  unsigned int new_index);
77  static int adjustPairs(const SimpleState& state,
78  unsigned int old_index, unsigned int new_index);
79  static int adjustPairs(const SimpleState& state,
80  unsigned int old_index, unsigned int old_index2,
81  unsigned int new_index);
82  static int diffAfterSimpleMove(const SimpleState& state,
83  Square from, Square to,
84  int promote_mask)
85  {
86  const Piece old_piece=state.pieceAt(from);
87  const PtypeO newPtypeO = promoteWithMask(old_piece.ptypeO(), promote_mask);
88  const unsigned int old_index = PiecePairIndex::indexOf(old_piece);
89  const unsigned int new_index = PiecePairIndex::indexOf(to, newPtypeO);
90  return adjustPairs(state, old_index, new_index);
91  }
92  static int diffAfterDropMove(const SimpleState& state,Square to,PtypeO ptypeo)
93  {
94  const unsigned int new_index = PiecePairIndex::indexOf(to, ptypeo);
95  return adjustPairs(state, new_index);
96  }
97  static int diffAfterCaptureMove(const SimpleState& state,
98  Square from, Square to,
99  PtypeO victim,int promote_mask)
100  {
101  const Piece old_piece=state.pieceAt(from);
102  const PtypeO newPtypeO = promoteWithMask(old_piece.ptypeO(), promote_mask);
103  const unsigned int old_index = PiecePairIndex::indexOf(old_piece);
104  const unsigned int new_index = PiecePairIndex::indexOf(to, newPtypeO);
105 
106  const unsigned int indexVictim = PiecePairIndex::indexOf(to, victim);
107  return adjustPairs(state, old_index, indexVictim,
108  new_index);
109  }
112  static int adjustPairsAfterMove(const SimpleState& state,
113  unsigned int new_index);
114  static int adjustPairsAfterMove(const SimpleState& state,
115  unsigned int old_index, unsigned int new_index);
116  static int adjustPairsAfterMove(const SimpleState& state,
117  unsigned int old_index, unsigned int old_index2,
118  unsigned int new_index);
119  static int diffWithUpdate(const SimpleState& new_state, Move last_move)
120  {
121  const unsigned int new_index = PiecePairIndex::indexOf(last_move.to(), last_move.ptypeO());
122  if (last_move.isDrop())
123  return adjustPairsAfterMove(new_state, new_index);
124 
125  const unsigned int old_index = PiecePairIndex::indexOf(last_move.from(), last_move.oldPtypeO());
126  if (last_move.capturePtype() == PTYPE_EMPTY)
127  return adjustPairsAfterMove(new_state, old_index, new_index);
128 
129  const unsigned int index_victim = PiecePairIndex::indexOf(last_move.to(), last_move.capturePtypeO());
130  return adjustPairsAfterMove(new_state, old_index, index_victim, new_index);
131  }
137  static void setValues(const SimpleState&, container::PieceValues&);
138 
139  private:
140  static bool& initializationFlag();
141  public:
142  static bool initialized() { return initializationFlag(); } // read only
143  static bool setUp(const char *filename);
144  static bool setUp();
145  };
146 
153  template <class Eval,class Table>
154  class PiecePairEval : public PiecePairEvalTableBase<Table>
155  {
156  protected:
157  // 必ず継承して使う
158  explicit PiecePairEval(const SimpleState& state);
159  public:
161  void changeTurn() {}
164  int expect(const SimpleState& state, Move m) const;
167  void update(const SimpleState& new_state, Move last_move)
168  {
169  base_t::val += Eval::diffWithUpdate(new_state, last_move);
170  }
171 
172  static int diffWithMove(const SimpleState& state, Move move)
173  {
174  // TRICK: Eval::diffXXX とすることでsubclassによるoverrideを可能にする
175  const Square from=move.from();
176  const Square to=move.to();
177  if (from.isPieceStand())
178  return Eval::diffAfterDropMove(state, to, move.ptypeO());
179 
180  const Ptype ptypeCaptured=move.capturePtype();
181  if (ptypeCaptured != PTYPE_EMPTY)
182  return Eval::diffAfterCaptureMove(state, from, to,
183  newPtypeO(alt(move.player()),
184  ptypeCaptured),
185  move.promoteMask());
186 
187  return Eval::diffAfterSimpleMove(state, from,to,move.promoteMask());
188  }
189 
190  };
191 
192  } // namespace ppair
193  } // namespace eval
194 } // namespace osl
195 
196 #endif /* _PIECE_PAIR_EVAL_H */
197 // ;;; Local Variables:
198 // ;;; mode:c++
199 // ;;; c-basic-offset:2
200 // ;;; End: