10 #include <boost/foreach.hpp>
14 osl::SimpleState::SimpleState() {
22 void osl::SimpleState::initPawnMask(){
28 pawnMask[0].clearAll();
29 pawnMask[1].clearAll();
31 num< PtypeTraits<PAWN>::indexLimit; num++){
36 if (isPawnMaskSet(player,pos.
x()))
38 throw CsaIOError(
"2FU!");
40 pawnMask[player].set(pos);
43 assert(isConsistent(
true));
46 void osl::SimpleState::init() {
48 for (
int ipos=0;ipos<Square::SIZE;ipos++) {
49 setBoard(Square::nth(ipos),Piece::EDGE());
51 for (
int y=1;y<=9;y++)
52 for (
int x=9;x>0;x--) {
53 setBoard(
Square(x,y),Piece::EMPTY());
56 stand_mask[
BLACK].resetAll();
57 stand_mask[
WHITE].resetAll();
58 stand_count[
BLACK].fill(0);
59 stand_count[
WHITE].fill(0);
61 pawnMask[0].clearAll();
62 pawnMask[1].clearAll();
63 for (
int num=0;num<Piece::SIZE;num++){
72 std::cerr <<
"unsupported handicap\n";
73 throw std::runtime_error(
"unsupported handicap");
76 for (
int x=9;x>0;x--) {
114 osl::SimpleState::~SimpleState() {}
118 for (num=0;num<40;num++) {
124 Piece p(player,ptype,num,pos);
127 stand_mask[player].set(num);
131 pawnMask[player].set(pos);
136 std::cerr <<
"osl::SimpleState::setPiece! maybe too many pieces "
137 << ptype <<
" " << pos <<
" " << player <<
"\n";
141 void osl::SimpleState::setPieceAll(
Player player) {
142 for (
int num=0;num<40;num++) {
143 if (!used_mask.test(num)) {
145 stand_mask[player].set(num);
159 bool osl::SimpleState::isConsistent(
bool show_error)
const
162 for (
int y=1;y<=9;y++)
164 for (
int x=9;x>=1;x--)
167 const Piece p0=pieceAt(pos);
173 std::cerr << p0 <<
" must be put at " << pos << std::endl;
178 if (! PieceTable::validNumber(num) || !used_mask.test(num)) {
179 if (show_error) std::cerr <<
"NotUsed, num=" << num << std::endl;
182 Piece p1=pieceOf(num);
184 if (show_error) std::cerr <<
"board[" << pos <<
"]!="
185 <<
"piece[" << num <<
"]" << std::endl;
192 for (
int num0=0; num0<Piece::SIZE; num0++)
194 if(!usedMask().test(num0))
continue;
197 Piece p0=pieceOf(num0);
200 if (show_error) std::cerr <<
"ptype of piece[" << num0 <<
"]="
201 << ptype << std::endl;
205 if (show_error) std::cerr <<
"mochigoma[" << num0 <<
"]=true" << std::endl;
210 if (show_error) std::cerr <<
"position " << pos <<
" is not onboard" << std::endl;
213 Piece p1=pieceAt(pos);
216 if (show_error) std::cerr <<
"pieces[" << num0 <<
"]=" << p0 <<
",board[" << pos <<
"] is " << p1 << std::endl;
222 Piece p0=pieceOf(num0);
224 #ifdef ALLOW_KING_ABSENCE
230 std::cerr <<
"pieces[" << num0 <<
"] ("
237 if (show_error) std::cerr <<
"ptype of piece[" << num0 <<
"]="
238 << ptype << std::endl;
242 if (show_error) std::cerr << p0 <<
" must be offboard" << std::endl;
249 if (countPiecesOnStand(
BLACK, ptype)
250 != countPiecesOnStandBit(
BLACK, ptype)) {
251 if (show_error) std::cerr <<
"count stand BLACK " << ptype <<
" inconsistent\n"
252 << *
this << countPiecesOnStand(
BLACK, ptype)
253 <<
" " << countPiecesOnStandBit(
BLACK, ptype) << std::endl;
256 if (countPiecesOnStand(
WHITE, ptype)
257 != countPiecesOnStandBit(
WHITE, ptype)) {
258 if (show_error) std::cerr <<
"count stand WHITE " << ptype <<
" inconsistent\n"
259 << *
this << countPiecesOnStand(
WHITE, ptype)
260 <<
" " << countPiecesOnStandBit(
WHITE, ptype) << std::endl;
266 CArray<BitXmask,2> pawnMask1;
267 pawnMask1[0].clearAll();
268 pawnMask1[1].clearAll();
270 num<PtypeTraits<PAWN>::indexLimit;num++){
272 Piece p=pieceOf(num);
278 if ((pawnMask[0]!=pawnMask1[0])
279 || (pawnMask[1]!=pawnMask1[1]))
282 std::cerr <<
"pawnMask "
283 << pawnMask[0] <<
"!=" << pawnMask1[0]
284 <<
" || " << pawnMask[1] <<
"!=" << pawnMask1[1]
290 for (
int i=0; i<nthLimit<PAWN>(); ++i) {
291 const Piece pawn = nth<PAWN>(i);
295 std::cerr <<
"pawn " << pawn << std::endl;
299 for (
int i=0; i<nthLimit<LANCE>(); ++i) {
300 const Piece lance = nth<LANCE>(i);
304 std::cerr <<
"lance " << lance << std::endl;
308 for (
int i=0; i<nthLimit<KNIGHT>(); ++i) {
309 const Piece knight = nth<KNIGHT>(i);
313 std::cerr <<
"knight " << knight << std::endl;
320 bool osl::SimpleState::isAlmostValidMove(
Move move,
bool show_error)
const
324 const bool valid = isAlmostValidMove<true>(move);
326 std::cerr << *
this <<
" " << move << std::endl;
330 return isAlmostValidMove<false>(move);
333 template <
bool show_error>
334 bool osl::SimpleState::isAlmostValidMove(
Move move)
const
337 assert(turn() == move.
player());
338 assert(isValidMoveByRule(move,
true));
342 return isAlmostValidDrop<show_error>(move);
345 if (! testValidityOtherThanEffect<show_error>(move))
348 const Piece from_piece = pieceAt(from);
356 std::cerr <<
" No such move2 : " << move << std::endl;
361 for (
Square p=from+o;p!=to;p+=o) {
362 if (! pieceAt(p).isEmpty()) {
364 std::cerr <<
" Not space to move : " << move << std::endl;
370 assert(isValidMoveByRule(move,
true));
374 bool osl::SimpleState::isValidMoveByRule(
Move move,
bool show_error)
382 if (from.isPieceStand())
387 if (show_error) std::cerr <<
" can't drop to : " << move << std::endl;
395 if (show_error) std::cerr <<
" inconsistent promote " << move << std::endl;
407 std::cerr <<
" No such move1 : " << move << std::endl;
417 || from.canPromote(move.
player()))))
420 std::cerr <<
" illegal promote type or position : " << move << std::endl;
430 std::cerr <<
" must promote to this position : " << move << std::endl;
437 bool osl::SimpleState::isValidMove(
Move move,
bool show_error)
const
439 if (turn() != move.
player()) {
441 std::cerr <<
"invalid player move : " << move << std::endl;
446 if (! isValidMoveByRule(move, show_error) || ! move.
isValid())
448 return isAlmostValidMove(move, show_error);
454 return std::cerr << *
this <<
"\n";
461 const osl::SimpleState
462 osl::SimpleState::emulateCapture(
Piece from,
Player new_owner)
const {
463 osl::SimpleState newState;
464 for(
int i=0;i<40;i++){
467 newState.setPiece(new_owner,Square::STAND(),
unpromote(p.
ptype()));
473 newState.setTurn(turn());
474 newState.initPawnMask();
481 const osl::SimpleState
483 assert(hasPieceOnStand(from, ptype));
484 assert(from==
alt(to));
485 osl::SimpleState newState;
487 for(
int i=0;i<40;i++){
488 if(!usedMask().test(i))
continue;
494 newState.setPiece(to,Square::STAND(),ptype);
502 newState.setTurn(turn());
503 newState.initPawnMask();
510 for (
int i=0; i<40; ++i) {
523 for (
int i=0; i<40; ++i) {
524 if(!usedMask().test(i))
continue;
525 const Piece p = pieceOf(i);
541 for (
int y=1;y<=9;y++)
542 for (
int x=9;x>0;x--) {
557 using namespace osl::record;
563 for (
unsigned int j=0; j<stand.
get(ptype); ++j)
575 for (
int y=1;y<=9;y++) {
577 for (
int x=9;x>0;x--) {
585 showStand(os,
BLACK, black_stand);
586 showStand(os,
WHITE, white_stand);
588 os << state.
turn() << std::endl;