pattern.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pattern.cc
Go to the documentation of this file.
1 /* pattern.cc
2 */
3 #include "osl/rating/feature/pattern.h"
4 #include <sstream>
5
6 const std::string osl::rating::Pattern::name(Direction d, Direction d2, Ptype self, Ptype target, bool same)
7 {
8 std::ostringstream os;
9 os << d;
10 if (d2 != Pattern::INVALID)
11 os << d2;
12 os << "-" << Ptype_Table.getCsaName(self) << "-"
13 << Ptype_Table.getCsaName(target) << (same ? "=" : "!");
14 return os.str();
15 }
16
17 const std::string osl::rating::LongTarget::name() const
18 {
19 std::ostringstream os;
20 os << Ptype_Table.getCsaName(target)
21 << (same ? "=" : "!") << (promotable ? "p" : "-");
22 return os.str() + CountEffect2::name(attack, defense);
23 }
24 const std::string osl::rating::LongTarget2::name() const
25 {
26 std::ostringstream os;
27 os << Ptype_Table.getCsaName(target)
28 << (same ? "=" : "!");
29 return os.str();
30 }
31
32 osl::rating::
33 PatternLong::PatternLong(Direction d, Ptype s, LongTarget t)
34 : Feature(name(d, s)+"-"+t.name()), direction(d), self(s), target(t)
35 {
36 assert(unpromote(s) == LANCE || unpromote(s) == BISHOP || unpromote(s) == ROOK);
37 }
38
39 const osl::rating::PieceSquare osl::rating::
40 PatternLong::nextPieceOrEnd(const SimpleState& state, Square start, Offset diff)
41 {
42 Square cur = start;
43 assert(cur.isOnBoard());
44 assert(! diff.zero());
45 cur += diff;
46 while (state.pieceAt(cur) == Piece::EMPTY())
47 cur += diff;
48 const Piece p = state.pieceAt(cur);
49 if (! p.isEdge())
50 return std::make_pair(p, cur);
51 cur -= diff;
52 assert(cur.isOnBoard());
53 if (cur == start)
54 return std::make_pair(p, cur); // EDGE
55 return std::make_pair(state.pieceOnBoard(cur), cur); // EMPTY
56 }
57
58 const osl::rating::PieceSquare osl::rating::
59 PatternLong::nextPieceOrEnd(const SimpleState& state, Square start, Player player, Direction direction)
60 {
61 const Offset diff = Board_Table.getOffset(player, direction);
62 return nextPieceOrEnd(state, start, diff);
63 }
64
65 const osl::rating::PieceSquare osl::rating::
66 PatternLong::find(const NumEffectState& state, Move move, Direction direction)
67 {
68 PieceSquare p = nextPieceOrEnd(state, move.to(), move.player(), direction);
69 if (p.second == move.from())
70 p = nextPieceOrEnd(state, p.second, move.player(), direction);
71 return p;
72 }
73
74 const std::string osl::rating::PatternLong::name(Direction d, Ptype self)
75 {
76 std::ostringstream os;
77 os << d << "-" << Ptype_Table.getCsaName(self);
78 return os.str();
79 }
80
81
82 osl::rating::
83 PatternLong2::PatternLong2(Direction d, Ptype s, LongTarget2 t2)
84 : Feature(name(d, s)+"--"+t2.name()), direction(d), self(s), target2(t2)
85 {
86 assert(unpromote(s) == LANCE || unpromote(s) == BISHOP || unpromote(s) == ROOK);
87 }
88
89 const std::string osl::rating::
90 PatternLong2::name(Direction d, Ptype self)
91 {
92 std::ostringstream os;
93 os << d << "-" << Ptype_Table.getCsaName(self);
94 return os.str();
95 }
96
97 osl::rating::
98 PatternBlock::PatternBlock(Ptype s, Ptype a, LongTarget t)
99 : Feature(std::string(Ptype_Table.getCsaName(s))/*+"-"+Ptype_Table.getCsaName(a)+">"*/+t.name()),
100 self(s), attack(a), target(t)
101 {
102 }
103
104 const osl::rating::PieceSquare osl::rating::
105 PatternBlock::find(const NumEffectState& state, Move move, Ptype ap)
106 {
107 Piece attack;
108 if (ap == LANCE) {
109 attack = state.findAttackAt<LANCE>(alt(state.turn()), move.to());
110 if (attack.ptype() == LANCE)
111 return PatternLong::nextPieceOrEnd
112 (state, move.to(),
113 Board_Table.getShortOffset(Offset32(move.to(), attack.square())));
114 } else if (ap == BISHOP) {
115 attack = state.findAttackAt<BISHOP>(alt(state.turn()), move.to());
116 if (attack.isPiece())
117 return PatternLong::nextPieceOrEnd
118 (state, move.to(),
119 Board_Table.getShortOffset(Offset32(move.to(), attack.square())));
120 } else if (ap == ROOK) {
121 attack = state.findAttackAt<ROOK>(alt(state.turn()), move.to());
122 if (attack.isPiece())
123 return PatternLong::nextPieceOrEnd
124 (state, move.to(),
125 Board_Table.getShortOffset(Offset32(move.to(), attack.square())));
126 }
127 return std::make_pair(Piece::EDGE(), Square::STAND());
128 }
129
130 /* ------------------------------------------------------------------------- */
131 // ;;; Local Variables:
132 // ;;; mode:c++
133 // ;;; c-basic-offset:2
134 // ;;; End: