moveScore.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
moveScore.cc
Go to the documentation of this file.
1 /* moveScore.cc
2 */
3 #include "osl/search/moveScore.h"
4 #include "osl/move_generator/capture_.h"
5 #include "osl/move_generator/allMoves.h"
6 #include "osl/move_generator/escape_.h"
7 #include "osl/move_generator/escape_.tcc"
8 #include "osl/move_generator/addEffectWithEffect.h"
9 #include "osl/move_generator/addEffectWithEffect.tcc"
10 #include "osl/move_generator/capture_.h"
11 #include "osl/move_generator/capture_.tcc"
12 #include "osl/move_generator/allMoves.h"
13 #include "osl/move_generator/allMoves.tcc"
14 #include "osl/move_generator/drop.h"
15 #include "osl/move_generator/drop.tcc"
16 #include <algorithm>
17
18 osl::search::MoveScore* osl::search::MoveScore::
19 sortPositive(MoveScore *first, MoveScore *last)
20 {
21 MoveScore *p=first-1, *q=last;
22 while (p != q) {
23 while (++p != q && p->score > 0)
24 ;
25 if (p != q) {
26 while (--q != p && q->score <= 0)
27 ;
28 std::swap(*p, *q);
29 }
30 }
31 std::stable_sort(first, p, std::greater<MoveScore>());
32 return p;
33 }
34
35 namespace osl
36 {
37 namespace search
38 {
39 struct Store{
40 MoveScore* out;
41 Store(MoveScore* o) :out(o) {}
42 void simpleMove(Square /*from*/,Square /*to*/,Ptype /*ptype*/, bool /*isPromote*/,Player /*p*/,Move move){
43 (*out++).move = move;
44 }
45 void unknownMove(Square /*from*/,Square /*to*/,Piece /*p1*/,Ptype /*ptype*/,bool /*isPromote*/,Player /*p*/,Move move)
46 {
47 (*out++).move = move;
48 }
49 void dropMove(Square /*to*/,Ptype /*ptype*/,Player /*p*/,Move move)
50 {
51 (*out++).move = move;
52 }
53 // old interfaces
54 void simpleMove(Square from,Square to,Ptype ptype,
55 bool isPromote,Player p)
56 {
57 simpleMove(from,to,ptype,isPromote,p,
58 Move(from,to,ptype,PTYPE_EMPTY,isPromote,p));
59 }
60 void unknownMove(Square from,Square to,Piece captured,
61 Ptype ptype,bool isPromote,Player p)
62 {
63 unknownMove(from,to,captured,ptype,isPromote,p,
64 Move(from,to,ptype,captured.ptype(),isPromote,p));
65 }
66 void dropMove(Square to,Ptype ptype,Player p)
67 {
68 dropMove(to,ptype,p,
69 Move(to,ptype,p));
70 }
71 };
72 struct NoCaptureStore{
73 MoveScore* out;
74 NoCaptureStore(MoveScore* o) :out(o) {}
75 void simpleMove(Square /*from*/,Square /*to*/,Ptype /*ptype*/, bool /*isPromote*/,Player /*p*/,Move move){
76 (*out++).move = move;
77 }
78 void unknownMove(Square /*from*/,Square /*to*/,Piece p1,Ptype /*ptype*/,bool /*isPromote*/,Player /*p*/,Move move)
79 {
80 if(p1.isEmpty())
81 (*out++).move = move;
82 }
83 void dropMove(Square /*to*/,Ptype /*ptype*/,Player /*p*/,Move move)
84 {
85 (*out++).move = move;
86 }
87 // old interfaces
88 void simpleMove(Square from,Square to,Ptype ptype,
89 bool isPromote,Player p)
90 {
91 (*out++).move = Move(from,to,ptype,PTYPE_EMPTY,isPromote,p);
92 }
93 void unknownMove(Square from,Square to,Piece captured,
94 Ptype ptype,bool isPromote,Player p)
95 {
96 if(captured.isEmpty())
97 (*out++).move = Move(from,to,ptype,captured.ptype(),isPromote,p);
98 }
99 void dropMove(Square to,Ptype ptype,Player p)
100 {
101 (*out++).move = Move(to,ptype,p);
102 }
103 };
104 }
105 }
106
107 template <osl::Player P>
108 osl::search::MoveScore* osl::search::MoveScore::
109 generateCapture(const NumEffectState& state, MoveScore *out)
110 {
111 Store store(out);
112 for(int num=0;num<Piece::SIZE;num++){
113 Piece p=state.pieceOf(num);
114 if(p.isOnBoardByOwner<PlayerTraits<P>::opponent>())
115 move_generator::Capture<Store>::generate<P>
116 (state,p.square(),store);
117 }
118 return store.out;
119 }
120 osl::search::MoveScore* osl::search::MoveScore::
121 generateCapture(const NumEffectState& state, MoveScore *out)
122 {
123 if (state.turn() == BLACK)
124 return generateCapture<BLACK>(state, out);
125 else
126 return generateCapture<WHITE>(state, out);
127 }
128
129 osl::search::MoveScore* osl::search::MoveScore::
130 generateNoCapture(const NumEffectState& state, MoveScore *out)
131 {
132 NoCaptureStore store(out);
133 if (state.turn() == BLACK)
134 move_generator::AllMoves<NoCaptureStore>::generate<BLACK>(state,store);
135 else
136 move_generator::AllMoves<NoCaptureStore>::generate<WHITE>(state,store);
137 return store.out;
138 }
139
140 osl::search::MoveScore* osl::search::MoveScore::
141 generateAll(const NumEffectState& state, MoveScore *out)
142 {
143 Store store(out);
144 if (state.turn() == BLACK)
145 move_generator::AllMoves<Store>::generate<BLACK>(state,store);
146 else
147 move_generator::AllMoves<Store>::generate<WHITE>(state,store);
148 return store.out;
149 }
150
151 osl::search::MoveScore* osl::search::MoveScore::
152 generateCheckNoCapture(const NumEffectState& state, MoveScore *out)
153 {
154 const Square king = state.kingSquare(alt(state.turn()));
155 NoCaptureStore store(out);
156 if (state.turn() == BLACK)
157 move_generator::AddEffectWithEffect<NoCaptureStore>::
158 generate<BLACK,true>(state,king,store);
159 else
160 move_generator::AddEffectWithEffect<NoCaptureStore>::
161 generate<WHITE,true>(state,king,store);
162 return store.out;
163 }
164
165 osl::search::MoveScore* osl::search::MoveScore::
166 generateKingEscape(const NumEffectState& state, MoveScore *out)
167 {
168 Store store(out);
169 if (state.turn() == BLACK)
170 move_generator::Escape<Store>::generateKingEscape<BLACK,false>
171 (state,store);
172 else
173 move_generator::Escape<Store>::generateKingEscape<WHITE,false>
174 (state,store);
175 return store.out;
176 }
177
178 // ;;; Local Variables:
179 // ;;; mode:c++
180 // ;;; c-basic-offset:2
181 // ;;; End: