My Project
pieceOnBoard.h
Go to the documentation of this file.
1 #ifndef OSL_PIECE_ON_BOARD_H
2 #define OSL_PIECE_ON_BOARD_H
3 #include "osl/numEffectState.h"
4 
5 namespace osl
6 {
7  namespace move_generator
8  {
9  enum PromoteType{
14  };
18  template<class Action,bool noCapturePromote=false>
19  struct PieceOnBoard
20  {
24  template<Player P>
25  static void generatePieceUnsafe(const NumEffectState& state,Piece p, Square target, Piece p1,Action& action)
26  {
27  assert(state.hasEffectByPiece(p, target));
28  Ptype ptype=p.ptype();
29  Square from=p.square();
30  if(canPromote(ptype)){
31  if(target.canPromote<P>()){
32  action.unknownMove(from,target,p1,promote(ptype),true,P);
33  int y=(P==BLACK ? target.y() : 10-target.y());
34  if(!Ptype_Table.isBetterToPromote(ptype) &&
35  (((ptype==LANCE || ptype==PAWN) ? y==3 : true )) &&
36  Ptype_Table.canDropTo(P,ptype,target))
37  action.unknownMove(from,target,p1,ptype,false,P);
38  }
39  else if(from.canPromote<P>()){
40  action.unknownMove(from,target,p1,promote(ptype),true,P);
41  if(!Ptype_Table.isBetterToPromote(ptype))
42  action.unknownMove(from,target,p1,ptype,false,P);
43  }
44  else
45  action.unknownMove(from,target,p1,ptype,false,P);
46  }
47  else{
48  action.unknownMove(from,target,p1,ptype,false,P);
49  }
50  }
51  template<Player P>
52  static void generatePiece(const NumEffectState& state,Piece p, Square target, Piece p1,Action& action)
53  {
54  if(p.ptype()==KING){
55  // 王手がかかっているときには自分の影になっている手も生成してしまう
56  const Player altP=alt(P);
57 // assert(!state.hasEffectAt<altP>(p.square()));
58  // 自殺手
59  if(state.hasEffectAt<altP>(target)) return;
60  }
61  if(state.pinOrOpen(P).test(p.number())){
62  Direction d=state.pinnedDir<P>(p);
63  Direction d1=Board_Table.getShort8Unsafe<P>(p.square(),target);
64  if(primDir(d)!=primDirUnsafe(d1)) return;
65  }
66  generatePieceUnsafe<P>(state,p,target,p1,action);
67  }
72  template<Player P,Ptype T>
73  static void generatePiecePtypeUnsafe(const NumEffectState& state,Piece p, Square target, Piece p1,Action& action)
74  {
75  assert(state.hasEffectByPiece(p, target));
76  assert(p.ptype()==T);
77 // Ptype ptype=p.ptype();
78  Square from=p.square();
79  if(canPromote(T) & (target.canPromote<P>() || from.canPromote<P>())){
80  action.unknownMove(from,target,p1,promote(T),true,P);
81  if(((T==PAWN || T==LANCE) &&
82  (P==BLACK ? target.y()==1 : target.y()==9))||
83  (T==KNIGHT && (P==BLACK ? target.y()<=2 : target.y()>=8)))
84  return;
85  if((T==ROOK || T==BISHOP || T==PAWN ||
86  (T==LANCE && (P==BLACK ? target.y()==2 : target.y()==8))))
87  return;
88  }
89  action.unknownMove(from,target,p1,T,false,P);
90  }
91  template<Player P,Ptype T>
92  static void generatePiecePtype(const NumEffectState& state,Piece p, Square target, Piece p1,Action& action)
93  {
94  if(T==KING){
95  assert(!state.hasEffectAt(alt(P),p.square()));
96  if(state.hasEffectAt(alt(P),target)) return;
97  }
98  else if(state.pin(P).test(p.number())){
99  Direction d=state.pinnedDir<P>(p);
100  Direction d1=Board_Table.getShort8Unsafe<P>(p.square(),target);
101  if(primDir(d)!=primDirUnsafe(d1)) return;
102  }
103  generatePiecePtypeUnsafe<P,T>(state,p,target,p1,action);
104  }
113  template <Player P,Ptype T,bool useDirMask>
114  static void generatePtype(const NumEffectState& state,Piece p, Action& action,int dirMask=0);
115 
116  template <Player P,Ptype T>
117  static void generatePtype(const NumEffectState& state,Piece p, Action& action)
118  {
119  int dummy=0;
120  generatePtype<P,T,false>(state,p,action,dummy);
121  }
130  template <Player P,Ptype T,bool useDirMask>
131  static void generatePtypeUnsafe(const NumEffectState& state,Piece p, Action& action,int dirMask);
132  template <Player P,Ptype T>
133  static void generatePtypeUnsafe(const NumEffectState& state,Piece p, Action& action)
134  {
135  int dummy=0;
136  generatePtypeUnsafe<P,T,false>(state,p,action,dummy);
137  }
138 
146  template <Player P,bool useDirMask>
147  static void generate(const NumEffectState& state,Piece p, Action& action,int dirMask=0);
148  };
149 
151  {
152  static void generate(Player turn, const NumEffectState& state, Piece target,
153  MoveVector&);
154  };
155  } // namespace move_generator
157 } // namespace osl
158 
159 #endif /* OSL_PIECE_ON_BOARD_H */
160 // ;;; Local Variables:
161 // ;;; mode:c++
162 // ;;; c-basic-offset:2
163 // ;;; End:
osl::Piece::number
int number() const
Definition: basic_type.h:828
osl::Square
Definition: basic_type.h:532
osl::primDirUnsafe
constexpr Direction primDirUnsafe(Direction d)
8方向について,primitiveな4方向を求める dとしてknight, INVALIDなども来る
Definition: basic_type.h:374
osl::Board_Table
const BoardTable Board_Table
Definition: tables.cc:95
osl::move_generator::PieceOnBoard::generatePieceUnsafe
static void generatePieceUnsafe(const NumEffectState &state, Piece p, Square target, Piece p1, Action &action)
駒pがマスtargetに利きをもっているとして,手を生成する.
Definition: pieceOnBoard.h:25
osl::alt
constexpr Player alt(Player player)
Definition: basic_type.h:13
osl::move_generator::PieceOnBoard::generatePtype
static void generatePtype(const NumEffectState &state, Piece p, Action &action)
Definition: pieceOnBoard.h:117
osl::promote
Ptype promote(Ptype ptype)
promote可能なptypeに対して,promote後の型を返す promote不可のptypeを与えてはいけない.
Definition: basic_type.h:173
osl::move_generator::GeneratePieceOnBoard
Definition: pieceOnBoard.h:151
osl::PtypeTable::isBetterToPromote
bool isBetterToPromote(Ptype ptype) const
Definition: ptypeTable.h:58
osl::move_generator::PieceOnBoard::generatePtype
static void generatePtype(const NumEffectState &state, Piece p, Action &action, int dirMask=0)
Generate moves without stating the Ptype as template param.
osl::move_generator::PieceOnBoard::generatePiece
static void generatePiece(const NumEffectState &state, Piece p, Square target, Piece p1, Action &action)
Definition: pieceOnBoard.h:52
osl::move_generator::CanPromoteType
@ CanPromoteType
Definition: pieceOnBoard.h:11
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
osl::LANCE
@ LANCE
Definition: basic_type.h:96
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition: numEffectState.h:324
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition: tables.cc:97
osl::Piece
駒.
Definition: basic_type.h:788
osl::move_generator::PieceOnBoard::generatePtypeUnsafe
static void generatePtypeUnsafe(const NumEffectState &state, Piece p, Action &action, int dirMask)
Generate moves without stating the Ptype as template param.
osl::move_generator::MustPromoteType
@ MustPromoteType
Definition: pieceOnBoard.h:13
osl::move_generator::PromoteType
PromoteType
Definition: pieceOnBoard.h:9
osl::KING
@ KING
Definition: basic_type.h:93
osl::BISHOP
@ BISHOP
Definition: basic_type.h:99
osl::NumEffectState::pinnedDir
Direction pinnedDir(Piece p) const
pinされた駒がPのKingから見てどの方向か? Pから見たdirectionを返す
Definition: numEffectState.h:203
osl::PAWN
@ PAWN
Definition: basic_type.h:95
osl::NumEffectState::hasEffectByPiece
bool hasEffectByPiece(Piece attack, Square target) const
駒attack が target に利きを持つか (旧hasEffectToと統合)
Definition: numEffectState.h:450
osl::move_generator::NoPromoteType
@ NoPromoteType
Definition: pieceOnBoard.h:10
osl::PieceMask::test
bool test(int num) const
Definition: pieceMask.h:45
osl::NumEffectState::pinOrOpen
PieceMask pinOrOpen(Player king) const
Definition: numEffectState.h:74
osl::NumEffectState
利きを持つ局面
Definition: numEffectState.h:34
osl::NumEffectState::pin
const PieceMask pin(Player king) const
Definition: numEffectState.h:65
osl::Direction
Direction
Definition: basic_type.h:310
osl::move_generator::CheckPromoteType
@ CheckPromoteType
Definition: pieceOnBoard.h:12
osl::ROOK
@ ROOK
Definition: basic_type.h:100
osl::BoardTable::getShort8Unsafe
Direction getShort8Unsafe(Square from, Square to) const
8方向にいない場合も適当なものを返す.
Definition: boardTable.h:134
osl::Square::canPromote
bool canPromote() const
Definition: basic_type.h:659
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
osl::primDir
constexpr Direction primDir(Direction d)
8方向について,primitiveな4方向を求める
Definition: basic_type.h:366
osl::Piece::square
const Square square() const
Definition: basic_type.h:832
osl::move_generator::PieceOnBoard::generate
static void generate(const NumEffectState &state, Piece p, Action &action, int dirMask=0)
Generate moves without stating the Ptype as template param.
osl::Piece::ptype
Ptype ptype() const
Definition: basic_type.h:821
osl::PtypeTable::canDropTo
bool canDropTo(Player pl, Ptype ptype, Square pos) const
Definition: ptypeTable.h:68
osl::BLACK
@ BLACK
Definition: basic_type.h:9
osl::canPromote
bool canPromote(Ptype ptype)
ptypeがpromote可能な型かどうかのチェック promote済みの場合はfalseを返す
Definition: basic_type.h:147
osl::move_generator::PieceOnBoard::generatePiecePtype
static void generatePiecePtype(const NumEffectState &state, Piece p, Square target, Piece p1, Action &action)
Definition: pieceOnBoard.h:92
osl::MoveVector
Definition: container.h:293
osl::move_generator::GeneratePieceOnBoard::generate
static void generate(Player turn, const NumEffectState &state, Piece target, MoveVector &)
Definition: pieceOnBoard.cc:20
osl::Player
Player
Definition: basic_type.h:8
numEffectState.h
osl::move_generator::PieceOnBoard::generatePtypeUnsafe
static void generatePtypeUnsafe(const NumEffectState &state, Piece p, Action &action)
Definition: pieceOnBoard.h:133
osl::move_generator::PieceOnBoard
特定のpieceを動かす手を生成
Definition: pieceOnBoard.h:20
osl
Definition: additionalEffect.h:6
osl::move_generator::PieceOnBoard::generatePiecePtypeUnsafe
static void generatePiecePtypeUnsafe(const NumEffectState &state, Piece p, Square target, Piece p1, Action &action)
PtypeがTの駒pがマスtargetに利きをもっているとして,手を生成する. p1 - targetにある駒
Definition: pieceOnBoard.h:73
osl::KNIGHT
@ KNIGHT
Definition: basic_type.h:97