boardTable.h
Go to the documentation of this file.
/* directionTable.h
*/
#ifndef OSL_BOARD_TABLE_H
#define OSL_BOARD_TABLE_H
#include "osl/basic_type.h"
#include "osl/container.h"
#include "osl/bits/offset32.h"
namespace osl
{
class BoardTable
{
CArray<Direction,Offset32::SIZE> directions;
// 上位はoffsetが，下位3bitはdirectionが入る．
CArray<signed char,Offset::ONBOARD_OFFSET_SIZE> short8Offset;
CArray<unsigned char,Offset::ONBOARD_OFFSET_SIZE> short8Dir;
CArray<Offset, Offset32::SIZE> short_offsets;
CArray<Offset, Offset32::SIZE> short_offsets_not_knight;
#ifndef MINIMAL
CArray<int,Offset32Wide::SIZE> space_counts;
#endif
public:
static const CArray<Offset, DIRECTION_SIZE> offsets;
static const CArray<int, DIRECTION_SIZE> dxs;
static const CArray<int, DIRECTION_SIZE> dys;
private:
template<Direction Dir>
void setDirections();
template<Direction Dir>
void setKnightDirections();
void init();
public:
const Offset getOffsetForBlack(Direction dir) const{
return offsets[static_cast<int>(dir)];
}
int getDxForBlack(Direction dir) const{
return dxs[static_cast<int>(dir)];
}
int getDyForBlack(Direction dir) const{
return dys[static_cast<int>(dir)];
}
template<Player P>
const Offset getOffset(Direction dir) const{
return getOffsetForBlack(dir)*sign(P);
}
const Offset getOffset(Player pl,Direction dir) const{
if (pl==BLACK)
return getOffset<BLACK>(dir);
else
return getOffset<WHITE>(dir);
}
const Square nextSquare(Player P, Square pos, Direction dr) const
{
assert(pos.isOnBoard());
const Offset offset = getOffset(P, dr);
return pos + offset;
}
BoardTable();
template <Player P>
Direction getLongDirection(Offset32 offset32) const
{
assert(offset32.isValid());
const Offset32 blackOffset32 = offset32.blackOffset32<P>();
Direction ret=directions[blackOffset32.index()];
assert(isValid(ret));
return ret;
}
Direction getLongDirection(Player P, Offset32 offset32) const
{
if (P == BLACK)
return getLongDirection<BLACK>(offset32);
else
return getLongDirection<WHITE>(offset32);
}
template <Player P>
Direction getLongDirection(Square from, Square to) const
{
return getLongDirection<P>(Offset32(to,from));
}
#ifndef MINIMAL
int spaceCounts(Square from,Square to) const
{
Offset32Wide offset32(from,to);
return space_counts[offset32.index()];
}
#endif
const Offset getShortOffset(Offset32 offset32) const{
assert(offset32.isValid());
return short_offsets[offset32.index()];
}
const Offset getShortOffsetNotKnight(Offset32 offset32) const{
assert(offset32.isValid());
return short_offsets_not_knight[offset32.index()];
}
Offset getShort8OffsetUnsafe(Square from,Square to) const{
int i=(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN;
return Offset::makeDirect(short8Offset[i]);
}
template<Player P>
Direction getShort8Unsafe(Square from,Square to) const{
if(P==BLACK)
return static_cast<Direction>(short8Dir[(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN]);
else
return static_cast<Direction>(short8Dir[(int)(from.uintValue())-(int)(to.uintValue())-Offset::ONBOARD_OFFSET_MIN]);
}
Direction getShort8Unsafe(Player P, Square from,Square to) const{
if(P==BLACK)
return getShort8Unsafe<BLACK>(from, to);
else
return getShort8Unsafe<WHITE>(from, to);
}
template<Player P>
Direction getShort8(Square from,Square to) const{
assert(from.isOnBoard() && to.isOnBoard());
assert(from.x()==to.x() || from.y()==to.y() ||
abs(from.x()-to.x())==abs(from.y()-to.y()));
return getShort8Unsafe<P>(from,to);
}
template<Player P>
Direction getShort8(Square from,Square to,Offset& o) const{
assert(from.isOnBoard() && to.isOnBoard());
assert(from.x()==to.x() || from.y()==to.y() ||
abs(from.x()-to.x())==abs(from.y()-to.y()));
int i=(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN;
o=Offset::makeDirect(short8Offset[i]);
Direction d=static_cast<Direction>(short8Dir[i]);
if(P==BLACK)
return d;
else
return inverse(d);
}
bool isBetween(Square t,Square p0,Square p1) const
{
int i1=(int)(t.uintValue())-(int)(p0.uintValue())-Offset::ONBOARD_OFFSET_MIN;
int i2=(int)(p1.uintValue())-(int)(t.uintValue())-Offset::ONBOARD_OFFSET_MIN;
assert(short8Dir[i1]!=DIRECTION_INVALID_VALUE || short8Dir[i2]!=DIRECTION_INVALID_VALUE);
return short8Dir[i1]==short8Dir[i2];
}
bool isBetweenSafe(Square t,Square p0,Square p1) const
{
if (getShortOffsetNotKnight(Offset32(t, p0)).zero())
return false;
return isBetween(t, p0, p1);
}
};
extern const BoardTable Board_Table;
} // namespace osl
#endif /* OSL_BOARD_TABLE_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
basic_type.h
osl::BoardTable::dxs
static const CArray< int, DIRECTION_SIZE > dxs
Definition boardTable.h:25
osl::BoardTable::getOffset
const Offset getOffset(Direction dir) const
Definition boardTable.h:47
osl::BoardTable::short_offsets
CArray< Offset, Offset32::SIZE > short_offsets
Definition boardTable.h:18
osl::BoardTable::getDxForBlack
int getDxForBlack(Direction dir) const
Definition boardTable.h:40
osl::BoardTable::setKnightDirections
void setKnightDirections()
Definition boardTable.cc:36
osl::BoardTable::getShortOffsetNotKnight
const Offset getShortOffsetNotKnight(Offset32 offset32) const
Longの利きの可能性のあるoffsetの場合は, 反復に使う offsetを Knight以外のShortの利きのoffsetの場合はそれ自身を返す.
Definition boardTable.h:119
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::BoardTable::spaceCounts
int spaceCounts(Square from, Square to) const
fromとtoが長い利きを持つ位置にある時，間のマスの数を求める 一致している時は0 となりも0 関係ない時は-1
Definition boardTable.h:99
osl::BoardTable::getDyForBlack
int getDyForBlack(Direction dir) const
Definition boardTable.h:43
osl::BoardTable::offsets
static const CArray< Offset, DIRECTION_SIZE > offsets
Definition boardTable.h:24
osl::BoardTable::dys
static const CArray< int, DIRECTION_SIZE > dys
Definition boardTable.h:26
osl::BoardTable::short_offsets_not_knight
CArray< Offset, Offset32::SIZE > short_offsets_not_knight
Definition boardTable.h:19
osl::BoardTable::getShort8
Direction getShort8(Square from, Square to, Offset &o) const
Definition boardTable.h:155
osl::BoardTable::getLongDirection
Direction getLongDirection(Square from, Square to) const
Definition boardTable.h:88
osl::BoardTable::space_counts
CArray< int, Offset32Wide::SIZE > space_counts
Definition boardTable.h:21
osl::BoardTable::directions
CArray< Direction, Offset32::SIZE > directions
Definition boardTable.h:14
osl::BoardTable::BoardTable
BoardTable()
Definition boardTable.cc:65
osl::BoardTable::getShortOffset
const Offset getShortOffset(Offset32 offset32) const
Longの利きの可能性のあるoffsetの場合は, 反復に使う offsetを Shortの利きのoffsetの場合はそれ自身を返す.
Definition boardTable.h:110
osl::BoardTable::getLongDirection
Direction getLongDirection(Offset32 offset32) const
Definition boardTable.h:71
osl::BoardTable::getLongDirection
Direction getLongDirection(Player P, Offset32 offset32) const
Definition boardTable.h:79
osl::BoardTable::short8Offset
CArray< signed char, Offset::ONBOARD_OFFSET_SIZE > short8Offset
Definition boardTable.h:16
osl::BoardTable::init
void init()
Definition boardTable.cc:45
osl::BoardTable::getShort8Unsafe
Direction getShort8Unsafe(Player P, Square from, Square to) const
Definition boardTable.h:140
osl::BoardTable::short8Dir
CArray< unsigned char, Offset::ONBOARD_OFFSET_SIZE > short8Dir
Definition boardTable.h:17
osl::BoardTable::getShort8OffsetUnsafe
Offset getShort8OffsetUnsafe(Square from, Square to) const
8方向にいない場合も適当なものを返す．
Definition boardTable.h:126
osl::BoardTable::getOffset
const Offset getOffset(Player pl, Direction dir) const
Definition boardTable.h:50
osl::BoardTable::getOffsetForBlack
const Offset getOffsetForBlack(Direction dir) const
黒にとってのoffsetを返す
Definition boardTable.h:37
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::setDirections
void setDirections()
Definition boardTable.cc:7
osl::BoardTable::isBetweenSafe
bool isBetweenSafe(Square t, Square p0, Square p1) const
Definition boardTable.h:179
osl::BoardTable::getShort8
Direction getShort8(Square from, Square to) const
Definition boardTable.h:147
osl::CArray
Definition container.h:20
osl::Offset32Base::isValid
bool isValid() const
Definition offset32.h:44
osl::Offset32Base::index
unsigned int index() const
Definition offset32.h:40
osl::Offset32Base::blackOffset32
const Offset32Base blackOffset32() const
Player P からみた offset を黒番のものに変更する
Definition offset32.h:52
osl::Offset
座標の差分
Definition basic_type.h:430
osl::Offset::ONBOARD_OFFSET_MIN
@ ONBOARD_OFFSET_MIN
Definition basic_type.h:434
osl::Offset::makeDirect
static const Offset makeDirect(int value)
Definition basic_type.h:447
osl::Square
Definition basic_type.h:532
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::uintValue
unsigned int uintValue() const
Definition basic_type.h:539
osl::Square::isOnBoard
bool isOnBoard() const
盤面上を表すかどうかの判定． 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する．
Definition basic_type.h:583
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
container.h
osl
Definition additionalEffect.h:6
osl::Offset32Wide
Offset32Base< 10, 10 > Offset32Wide
Definition offset32.h:64
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::isValid
bool isValid(Player player)
cast等で作られたplayerが正しいかどうかを返す
Definition basic_type.cc:9
osl::Direction
Direction
Definition basic_type.h:310
osl::DIRECTION_INVALID_VALUE
@ DIRECTION_INVALID_VALUE
Definition basic_type.h:338
osl::Offset32
Offset32Base< 8, 9 > Offset32
Definition offset32.h:63
osl::sign
constexpr int sign(Player player)
Definition basic_type.h:23
osl::Player
Player
Definition basic_type.h:8
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::inverse
constexpr Direction inverse(Direction d)
Definition basic_type.h:358
offset32.h