My Project
boardTable.h
Go to the documentation of this file.
1 /* directionTable.h
2  */
3 #ifndef OSL_BOARD_TABLE_H
4 #define OSL_BOARD_TABLE_H
5 
6 #include "osl/basic_type.h"
7 #include "osl/container.h"
8 #include "osl/bits/offset32.h"
9 
10 namespace osl
11 {
12  class BoardTable
13  {
15  // 上位はoffsetが,下位3bitはdirectionが入る. 
20 #ifndef MINIMAL
22 #endif
23  public:
27  private:
28  template<Direction Dir>
29  void setDirections();
30  template<Direction Dir>
31  void setKnightDirections();
32  void init();
33  public:
38  return offsets[static_cast<int>(dir)];
39  }
40  int getDxForBlack(Direction dir) const{
41  return dxs[static_cast<int>(dir)];
42  }
43  int getDyForBlack(Direction dir) const{
44  return dys[static_cast<int>(dir)];
45  }
46  template<Player P>
47  const Offset getOffset(Direction dir) const{
48  return getOffsetForBlack(dir)*sign(P);
49  }
50  const Offset getOffset(Player pl,Direction dir) const{
51  if (pl==BLACK)
52  return getOffset<BLACK>(dir);
53  else
54  return getOffset<WHITE>(dir);
55  }
56 
61  const Square nextSquare(Player P, Square pos, Direction dr) const
62  {
63  assert(pos.isOnBoard());
64  const Offset offset = getOffset(P, dr);
65  return pos + offset;
66  }
67 
68  BoardTable();
70  template <Player P>
72  {
73  assert(offset32.isValid());
74  const Offset32 blackOffset32 = offset32.blackOffset32<P>();
75  Direction ret=directions[blackOffset32.index()];
76  assert(isValid(ret));
77  return ret;
78  }
80  {
81  if (P == BLACK)
82  return getLongDirection<BLACK>(offset32);
83  else
84  return getLongDirection<WHITE>(offset32);
85  }
87  template <Player P>
89  {
90  return getLongDirection<P>(Offset32(to,from));
91  }
92 #ifndef MINIMAL
93 
99  int spaceCounts(Square from,Square to) const
100  {
101  Offset32Wide offset32(from,to);
102  return space_counts[offset32.index()];
103  }
104 #endif
105 
110  const Offset getShortOffset(Offset32 offset32) const{
111  assert(offset32.isValid());
112  return short_offsets[offset32.index()];
113  }
119  const Offset getShortOffsetNotKnight(Offset32 offset32) const{
120  assert(offset32.isValid());
121  return short_offsets_not_knight[offset32.index()];
122  }
127  int i=(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN;
128  return Offset::makeDirect(short8Offset[i]);
129  }
133  template<Player P>
135  if(P==BLACK)
136  return static_cast<Direction>(short8Dir[(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN]);
137  else
138  return static_cast<Direction>(short8Dir[(int)(from.uintValue())-(int)(to.uintValue())-Offset::ONBOARD_OFFSET_MIN]);
139  }
141  if(P==BLACK)
142  return getShort8Unsafe<BLACK>(from, to);
143  else
144  return getShort8Unsafe<WHITE>(from, to);
145  }
146  template<Player P>
148  assert(from.isOnBoard() && to.isOnBoard());
149  assert(from.x()==to.x() || from.y()==to.y() ||
150  abs(from.x()-to.x())==abs(from.y()-to.y()));
151  return getShort8Unsafe<P>(from,to);
152  }
153 
154  template<Player P>
156  assert(from.isOnBoard() && to.isOnBoard());
157  assert(from.x()==to.x() || from.y()==to.y() ||
158  abs(from.x()-to.x())==abs(from.y()-to.y()));
159  int i=(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN;
161  Direction d=static_cast<Direction>(short8Dir[i]);
162  if(P==BLACK)
163  return d;
164  else
165  return inverse(d);
166  }
172  bool isBetween(Square t,Square p0,Square p1) const
173  {
174  int i1=(int)(t.uintValue())-(int)(p0.uintValue())-Offset::ONBOARD_OFFSET_MIN;
175  int i2=(int)(p1.uintValue())-(int)(t.uintValue())-Offset::ONBOARD_OFFSET_MIN;
177  return short8Dir[i1]==short8Dir[i2];
178  }
179  bool isBetweenSafe(Square t,Square p0,Square p1) const
180  {
181  if (getShortOffsetNotKnight(Offset32(t, p0)).zero())
182  return false;
183  return isBetween(t, p0, p1);
184  }
185  };
186 
187  extern const BoardTable Board_Table;
188 } // namespace osl
189 
190 
191 #endif /* OSL_BOARD_TABLE_H */
192 // ;;; Local Variables:
193 // ;;; mode:c++
194 // ;;; c-basic-offset:2
195 // ;;; End:
osl::Square
Definition: basic_type.h:532
osl::BoardTable::space_counts
CArray< int, Offset32Wide::SIZE > space_counts
Definition: boardTable.h:21
osl::BoardTable::init
void init()
Definition: boardTable.cc:45
osl::BoardTable::short8Offset
CArray< signed char, Offset::ONBOARD_OFFSET_SIZE > short8Offset
Definition: boardTable.h:16
osl::Square::isOnBoard
bool isOnBoard() const
盤面上を表すかどうかの判定. 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する.
Definition: basic_type.h:583
osl::Board_Table
const BoardTable Board_Table
Definition: tables.cc:95
osl::BoardTable::isBetweenSafe
bool isBetweenSafe(Square t, Square p0, Square p1) const
Definition: boardTable.h:179
osl::inverse
constexpr Direction inverse(Direction d)
Definition: basic_type.h:358
osl::Offset::makeDirect
static const Offset makeDirect(int value)
Definition: basic_type.h:447
osl::BoardTable::getLongDirection
Direction getLongDirection(Square from, Square to) const
Definition: boardTable.h:88
osl::Offset32
Offset32Base< 8, 9 > Offset32
Definition: offset32.h:63
osl::Offset32Base::isValid
bool isValid() const
Definition: offset32.h:44
osl::BoardTable::getShort8Unsafe
Direction getShort8Unsafe(Player P, Square from, Square to) const
Definition: boardTable.h:140
basic_type.h
osl::BoardTable::getOffset
const Offset getOffset(Player pl, Direction dir) const
Definition: boardTable.h:50
osl::BoardTable::getLongDirection
Direction getLongDirection(Offset32 offset32) const
Definition: boardTable.h:71
osl::BoardTable::getShort8
Direction getShort8(Square from, Square to) const
Definition: boardTable.h:147
osl::Offset
座標の差分
Definition: basic_type.h:430
osl::BoardTable
Definition: boardTable.h:13
osl::BoardTable::getDyForBlack
int getDyForBlack(Direction dir) const
Definition: boardTable.h:43
osl::BoardTable::BoardTable
BoardTable()
Definition: boardTable.cc:65
osl::Offset32Base::blackOffset32
const Offset32Base blackOffset32() const
Player P からみた offset を黒番のものに変更する
Definition: offset32.h:52
osl::Square::uintValue
unsigned int uintValue() const
Definition: basic_type.h:539
osl::BoardTable::getShort8OffsetUnsafe
Offset getShort8OffsetUnsafe(Square from, Square to) const
8方向にいない場合も適当なものを返す.
Definition: boardTable.h:126
osl::DIRECTION_INVALID_VALUE
@ DIRECTION_INVALID_VALUE
Definition: basic_type.h:338
osl::BoardTable::getDxForBlack
int getDxForBlack(Direction dir) const
Definition: boardTable.h:40
osl::BoardTable::dys
static const CArray< int, DIRECTION_SIZE > dys
Definition: boardTable.h:26
container.h
osl::Offset32Base
差が uniqになるような座標の差分.
Definition: offset32.h:17
osl::Offset::ONBOARD_OFFSET_MIN
@ ONBOARD_OFFSET_MIN
Definition: basic_type.h:434
osl::BoardTable::getShortOffsetNotKnight
const Offset getShortOffsetNotKnight(Offset32 offset32) const
Longの利きの可能性のあるoffsetの場合は, 反復に使う offsetを Knight以外のShortの利きのoffsetの場合はそれ自身を返す.
Definition: boardTable.h:119
osl::BoardTable::short_offsets
CArray< Offset, Offset32::SIZE > short_offsets
Definition: boardTable.h:18
osl::BoardTable::getOffsetForBlack
const Offset getOffsetForBlack(Direction dir) const
黒にとってのoffsetを返す
Definition: boardTable.h:37
osl::BoardTable::offsets
static const CArray< Offset, DIRECTION_SIZE > offsets
Definition: boardTable.h:24
osl::isValid
bool isValid(Player player)
cast等で作られたplayerが正しいかどうかを返す
Definition: basic_type.cc:9
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
osl::BoardTable::setDirections
void setDirections()
Definition: boardTable.cc:7
osl::BoardTable::getOffset
const Offset getOffset(Direction dir) const
Definition: boardTable.h:47
osl::Direction
Direction
Definition: basic_type.h:310
osl::BoardTable::nextSquare
const Square nextSquare(Player P, Square pos, Direction dr) const
next position from pos for player P.
Definition: boardTable.h:61
osl::BoardTable::setKnightDirections
void setKnightDirections()
Definition: boardTable.cc:36
osl::BoardTable::getShort8Unsafe
Direction getShort8Unsafe(Square from, Square to) const
8方向にいない場合も適当なものを返す.
Definition: boardTable.h:134
osl::BoardTable::isBetween
bool isBetween(Square t, Square p0, Square p1) const
p0, p1の間にtがあるかどうか.
Definition: boardTable.h:172
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
offset32.h
osl::sign
constexpr int sign(Player player)
Definition: basic_type.h:23
osl::BoardTable::directions
CArray< Direction, Offset32::SIZE > directions
Definition: boardTable.h:14
osl::Offset32Base::index
unsigned int index() const
Definition: offset32.h:40
osl::BoardTable::getShortOffset
const Offset getShortOffset(Offset32 offset32) const
Longの利きの可能性のあるoffsetの場合は, 反復に使う offsetを Shortの利きのoffsetの場合はそれ自身を返す.
Definition: boardTable.h:110
osl::BLACK
@ BLACK
Definition: basic_type.h:9
osl::BoardTable::short8Dir
CArray< unsigned char, Offset::ONBOARD_OFFSET_SIZE > short8Dir
Definition: boardTable.h:17
osl::BoardTable::short_offsets_not_knight
CArray< Offset, Offset32::SIZE > short_offsets_not_knight
Definition: boardTable.h:19
osl::BoardTable::dxs
static const CArray< int, DIRECTION_SIZE > dxs
Definition: boardTable.h:25
osl::Player
Player
Definition: basic_type.h:8
osl::CArray< Direction, Offset32::SIZE >
osl::BoardTable::getShort8
Direction getShort8(Square from, Square to, Offset &o) const
Definition: boardTable.h:155
osl
Definition: additionalEffect.h:6
osl::BoardTable::getLongDirection
Direction getLongDirection(Player P, Offset32 offset32) const
Definition: boardTable.h:79
osl::BoardTable::spaceCounts
int spaceCounts(Square from, Square to) const
fromとtoが長い利きを持つ位置にある時,間のマスの数を求める 一致している時は0 となりも0 関係ない時は-1
Definition: boardTable.h:99