My Project
feature.cc
Go to the documentation of this file.
1/* feature.cc
2 */
4#include "osl/simpleState.tcc"
5#include <sstream>
6
10
11const osl::CArray<const char*,4> osl::rating::Check::check_property = {{ "Di", "DO", "OC", "Bo" }};
12osl::rating::Check::Check(int p) : Feature(check_property[p]), property(p)
13{
14}
15
16bool osl::rating::Check::match(const NumEffectState& state, Move move, const RatingEnv&) const
17{
18 if (property == 0 || property == 1) {
19 if (! (state.isDirectCheck(move) && ! state.isOpenCheck(move)))
20 return false;
21 return property == openLong(state, move);
22 }
23 if (property == 2)
24 return ! state.isDirectCheck(move) && state.isOpenCheck(move);
25 return state.isDirectCheck(move) && state.isOpenCheck(move);
26}
27
28const std::string osl::rating::
29Block::name(int self, int opponent)
30{
31 std::ostringstream os;
32 os << "B" << self << opponent;
33 return os.str();
34}
35
36const std::string osl::rating::Open::name(int property)
37{
38 std::ostringstream os;
39 os << "Open" << property / 4 << property % 4;
40 return os.str();
41}
42
44{
45 bool *result;
48 Test(bool *r, const NumEffectState& s, Move m)
49 : result(r), state(s), move(m)
50 {
51 }
52 template <Player P>
53 void doAction(Piece /*piece*/, Square last_attack)
54 {
55 const Piece attacked = state.pieceAt(last_attack);
56 if (attacked.isOnBoardByOwner(state.turn())
57 && state.hasEffectIf(move.ptypeO(), move.to(), last_attack))
58 *result = true;
59 }
60};
61
64 : Feature(std::string(Ptype_Table.getCsaName(s))+":"+Ptype_Table.getCsaName(a)),
65 self(s), attack(a)
66{
67}
68
70ImmediateAddSupport::match(const NumEffectState& state, Move move, const RatingEnv& env) const
71{
72 if (move.ptype() != self)
73 return false;
74 const Move last_move=env.history.lastMove();
75 if (! last_move.isNormal())
76 return false;
77 if (last_move.ptype() != attack)
78 return false;
79 const Square last_to = last_move.to();
80 if (last_to==move.to())
81 return false; // TakeBack は除く
82 bool result = false;
83 Test action(&result, state, move);
84 state.forEachEffectOfPiece(state.pieceOnBoard(last_to), action);
85 return result;
86}
87
89ImmediateAddSupport::index(const NumEffectState& state, Move move, const RatingEnv& env)
90{
91 const Move last_move=env.history.lastMove();
92 if (! last_move.isNormal())
93 return -1;
94 const Square last_to = last_move.to();
95 if (last_to==move.to())
96 return -1; // TakeBack は除く
97 bool result = false;
98 const Piece last_piece = state.pieceOnBoard(last_to);
99 // BoardMask state.changedEffects(alt(Turn))?
100 if (! Ptype_Table.hasLongMove(last_piece.ptype()))
101 {
102 Test action(&result, state, move);
103 state.forEachEffectOfPiece(last_piece, action);
104 }
105 else
106 {
107 const Player Turn = state.turn();
108 PieceMask pieces = state.piecesOnBoard(Turn) & state.effectedMask(alt(Turn));
109 mask_t m = pieces.getMask(0);
110 while (m.any()) {
111 const Piece p = state.pieceOf(m.takeOneBit());
112 if (state.hasEffectByPiece(last_piece, p.square())
113 && state.hasEffectIf(move.ptypeO(), move.to(), p.square())) {
114 result = true;
115 break;
116 }
117 }
118#if OSL_WORDSIZE == 32
119 if (! result) {
120 m = pieces.getMask(1);
121 while (m.any()) {
122 const Piece p = state.pieceOf(m.takeOneBit()+32);
123 if (state.hasEffectByPiece(last_piece, p.square())
124 && state.hasEffectIf(move.ptypeO(), move.to(), p.square())) {
125 result = true;
126 break;
127 }
128 }
129 }
130#endif
131 }
132 if (! result)
133 return -1;
134 return (move.ptype() - PTYPE_PIECE_MIN) * (PTYPE_MAX+1 - PTYPE_PIECE_MIN)
135 + last_move.ptype() - PTYPE_PIECE_MIN;
136}
137
138const std::string osl::rating::Chase::name(Ptype self, Ptype target, bool drop, OpponentType opponent_type)
139{
140 return std::string(Ptype_Table.getCsaName(self))
141 +(drop ? "d" : "m")+">"+Ptype_Table.getCsaName(target)
142 +(opponent_type == CAPTURE ? "c" : (opponent_type == DROP ? "d" : "e"));
143}
144
145/* ------------------------------------------------------------------------- */
146// ;;; Local Variables:
147// ;;; mode:c++
148// ;;; c-basic-offset:2
149// ;;; End:
圧縮していない moveの表現 .
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Ptype ptype() const
bool isNormal() const
INVALID でも PASS でもない.
const Square to() const
利きを持つ局面
bool isOpenCheck(Move move) const
const PieceMask effectedMask(Player pl) const
pl からの利きが(1つ以上)ある駒一覧
bool hasEffectByPiece(Piece attack, Square target) const
駒attack が target に利きを持つか (旧hasEffectToと統合)
bool isDirectCheck(Move move) const
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
void forEachEffectOfPiece(Square pieceSquare, Action &action) const
pieceSquareにある駒によって利きを受けるすべてのsquare (空白含む)について actionを実行する
const PieceMask & piecesOnBoard(Player p) const
駒番号のビットセット.
Definition pieceMask.h:21
const mask_t getMask(int num) const
Definition pieceMask.h:59
Ptype ptype() const
Definition basic_type.h:821
const Square square() const
Definition basic_type.h:832
bool isOnBoardByOwner() const
piece がプレイヤーPの持ち物でかつボード上にある駒の場合は true.
Definition basic_type.h:852
bool hasLongMove(Ptype ptype) const
遅くて良い?
Definition ptypeTable.h:54
const char * getCsaName(Ptype ptype) const
Definition ptypeTable.h:80
const Piece pieceOnBoard(Square sq) const
Player turn() const
const Piece pieceOf(int num) const
Definition simpleState.h:76
const Piece pieceAt(Square sq) const
const Move lastMove(size_t last=1) const
Definition moveStack.h:28
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition feature.cc:16
static const CArray< const char *, 4 > check_property
virtual ~Feature()
Definition feature.cc:7
const std::string & name() const
ImmediateAddSupport(Ptype self, Ptype attack)
Definition feature.cc:63
static int index(const NumEffectState &state, Move move, const RatingEnv &env)
Definition feature.cc:89
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition feature.cc:70
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
@ PTYPE_MAX
Definition basic_type.h:105
const PtypeTable Ptype_Table
Definition tables.cc:97
Player
Definition basic_type.h:8
constexpr Player alt(Player player)
Definition basic_type.h:13
const NumEffectState & state
Definition feature.cc:46
void doAction(Piece, Square last_attack)
Definition feature.cc:53
Test(bool *r, const NumEffectState &s, Move m)
Definition feature.cc:48