simpleState.h
Go to the documentation of this file.
/* simpleState.h
*/
#ifndef OSL_SIMPLE_STATE_H
#define OSL_SIMPLE_STATE_H
#include "osl/basic_type.h"
#include "osl/bits/ptypeTable.h"
#include "osl/bits/boardTable.h"
#include "osl/bits/ptypeTraits.h"
#include "osl/bits/pieceMask.h"
#include "osl/bits/bitXmask.h"
#include "osl/bits/effectContent.h"
#include "osl/container.h"
#include <iosfwd>
namespace osl
{
enum Handicap{
HIRATE,
// KYOUOCHI,
// KAKUOCHI,
};
class SimpleState;
std::ostream& operator<<(std::ostream& os,const SimpleState& state);
bool operator==(const SimpleState& st1,const SimpleState& st2);
class SimpleState
{
private:
friend std::ostream& operator<<(std::ostream& os,const SimpleState& state);
friend bool operator==(const SimpleState& st1,const SimpleState& st2);
typedef SimpleState state_t;
public:
static const bool hasPawnMask=true;
protected:
CArray<Piece,Square::SIZE> board
#ifdef __GNUC__
__attribute__((aligned(16)))
#endif
;
CArray<Piece,Piece::SIZE> pieces
#ifdef __GNUC__
__attribute__((aligned(16)))
#endif
;
CArray<PieceMask,2> stand_mask;
CArray<BitXmask,2> pawnMask;
CArray<CArray<char,PTYPE_SIZE-PTYPE_BASIC_MIN>,2> stand_count;
Player player_to_move;
PieceMask used_mask;
public:
// 生成に関するもの
explicit SimpleState();
explicit SimpleState(Handicap h);
// public継承させるには，virtual destructorを定義する．
virtual ~SimpleState();
void init();
void init(Handicap h);
// private:
void initPawnMask();
public:
const Piece pieceOf(int num) const{
return pieces[num];
}
void setPieceOf(int num,Piece p) {
pieces[num]=p;
}
template<Player P>
const Piece kingPiece() const{
return pieceOf(KingTraits<P>::index);
}
const Piece kingPiece(Player P) const{
assert(isValid(P));
if (P==BLACK)
return kingPiece<BLACK>();
else
return kingPiece<WHITE>();
}
template<Player P>
Square kingSquare() const{
return kingPiece<P>().square();
}
Square kingSquare(Player player) const{
assert(isValid(player));
if (player==BLACK)
return kingSquare<BLACK>();
else
return kingSquare<WHITE>();
}
template <Ptype PTYPE>
static int nthLimit() {
return PtypeTraits<PTYPE>::indexLimit - PtypeTraits<PTYPE>::indexMin;
}
template <Ptype PTYPE>
const Piece nth(int n) const {
assert(0 <= n && n < nthLimit<PTYPE>());
return pieceOf(PtypeTraits<PTYPE>::indexMin+n);
}
void setBoard(Square sq,Piece piece)
{
board[sq.index()]=piece;
}
protected:
PieceMask& standMask(Player p) {
return stand_mask[p];
}
public:
const PieceMask& standMask(Player p) const {
return stand_mask[p];
}
const PieceMask& usedMask() const {return used_mask;}
bool isOffBoard(int num) const{
return standMask(BLACK).test(num)
|| standMask(WHITE).test(num);
}
// protected:
void clearPawn(Player pl,Square sq){
pawnMask[pl].clear(sq);
}
void setPawn(Player pl,Square sq){
pawnMask[pl].set(sq);
}
public:
bool isPawnMaskSet(Player player, int x) const
{
return pawnMask[player].isSet(x);
}
template<Player P>
bool isPawnMaskSet(int x)const {return isPawnMaskSet(P,x); }
bool canDropPawnTo(Player player, int x) const
{
return hasPieceOnStand<PAWN>(player) && ! isPawnMaskSet(player, x);
}
void setPiece(Player player,Square sq,Ptype ptype);
void setPieceAll(Player player);
const Piece pieceAt(Square sq) const { return board[sq.index()];}
const Piece operator[](Square sq) const { return pieceAt(sq);}
const Piece* getPiecePtr(Square sq) const { return &board[sq.index()];}
const Piece pieceOnBoard(Square sq) const
{
assert(sq.isOnBoard());
return pieceAt(sq);
}
bool isOnBoard(int num) const {
return pieceOf(num).isOnBoard();
}
int countPiecesOnStand(Player pl,Ptype ptype) const {
assert(isBasic(ptype));
return stand_count[pl][ptype-PTYPE_BASIC_MIN];
}
template <Ptype Type>
int countPiecesOnStand(Player pl) const {
return countPiecesOnStand(pl, Type);
}
bool hasPieceOnStand(Player player,Ptype ptype) const{
return countPiecesOnStand(player, ptype)!=0;
}
template<Ptype T>
bool hasPieceOnStand(Player P) const {
return countPiecesOnStand(P, T);
}
private:
int countPiecesOnStandBit(Player pl,Ptype ptype) const {
return (standMask(pl).getMask(0)
& Ptype_Table.getMaskLow(ptype)).countBit();
}
public:
Piece nextPiece(Square cur, Offset diff) const
{
assert(! diff.zero());
cur += diff;
while (pieceAt(cur) == Piece::EMPTY())
cur += diff;
return pieceAt(cur);
}
void setTurn(Player player) {
player_to_move=player;
}
Player turn() const{
return player_to_move;
}
void changeTurn() {
player_to_move = alt(player_to_move);
}
// check
bool isConsistent(bool show_error=true) const;
template <bool show_error>
bool isAlmostValidMove(Move move) const;
bool isAlmostValidMove(Move move,bool show_error=true) const;
bool isValidMove(Move move,bool show_error=true) const;
protected:
template <bool show_error> bool isAlmostValidDrop(Move move) const;
template <bool show_error> bool testValidityOtherThanEffect(Move move) const;
public:
static bool isValidMoveByRule(Move move,bool show_error);
bool isEmptyBetween(Square from, Square to,Offset offset,bool pieceExistsAtTo=false) const
#ifdef __GNUC__
__attribute__ ((pure))
#endif
{
assert(from.isOnBoard());
assert(! offset.zero());
assert(offset==Board_Table.getShortOffset(Offset32(to,from)));
Square sq=from+offset;
for (; pieceAt(sq).isEmpty(); sq+=offset) {
if (!pieceExistsAtTo && sq==to)
return true;
}
return sq==to;
}
bool
#ifdef __GNUC__
__attribute__ ((pure))
#endif
isEmptyBetween(Square from, Square to,bool noSpaceAtTo=false) const{
assert(from.isOnBoard());
Offset offset=Board_Table.getShortOffset(Offset32(to,from));
assert(! offset.zero());
return isEmptyBetween(from,to,offset,noSpaceAtTo);
}
bool dump() const;
const SimpleState emulateCapture(Piece from, Player new_owner) const;
const SimpleState emulateHandPiece(Player from, Player to, Ptype ptype) const;
const SimpleState rotate180() const;
const SimpleState flipHorizontal() const;
};
} // namespace osl
#endif /* OSL_SIMPLE_STATE_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
basic_type.h
bitXmask.h
boardTable.h
osl::CArray
Definition container.h:20
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Offset
座標の差分
Definition basic_type.h:430
osl::Offset::zero
bool zero() const
Definition basic_type.h:502
osl::PieceMask
駒番号のビットセット.
Definition pieceMask.h:21
osl::PieceMask::test
bool test(int num) const
Definition pieceMask.h:45
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::isEmpty
bool isEmpty() const
Definition basic_type.h:913
osl::Piece::EMPTY
static const Piece EMPTY()
Definition basic_type.h:797
osl::Piece::isOnBoard
bool isOnBoard() const
Definition basic_type.h:985
osl::SimpleState
Definition simpleState.h:35
osl::SimpleState::pawnMask
CArray< BitXmask, 2 > pawnMask
Definition simpleState.h:57
osl::SimpleState::kingSquare
Square kingSquare(Player player) const
Definition simpleState.h:97
osl::SimpleState::nextPiece
Piece nextPiece(Square cur, Offset diff) const
diff方向にあるPiece を求める.
Definition simpleState.h:208
osl::SimpleState::pieceOnBoard
const Piece pieceOnBoard(Square sq) const
Definition simpleState.h:170
osl::SimpleState::isValidMove
bool isValidMove(Move move, bool show_error=true) const
合法手かどうかを検査する． isValidMoveByRule, isAlmostValidMove をおこなう． 玉の素抜きや王手を防いでいるか， 千日手，打歩詰かどうかは検査しない．
Definition simpleState.cc:435
osl::SimpleState::isAlmostValidDrop
bool isAlmostValidDrop(Move move) const
osl::SimpleState::board
CArray< Piece, Square::SIZE > board
Definition simpleState.h:47
osl::SimpleState::state_t
SimpleState state_t
Definition simpleState.h:39
osl::SimpleState::player_to_move
Player player_to_move
手番
Definition simpleState.h:61
osl::SimpleState::hasPieceOnStand
bool hasPieceOnStand(Player player, Ptype ptype) const
Definition simpleState.h:191
osl::SimpleState::emulateHandPiece
const SimpleState emulateHandPiece(Player from, Player to, Ptype ptype) const
from からto に ptypeの持駒を一枚渡した局面を作る.
Definition simpleState.cc:480
osl::SimpleState::countPiecesOnStand
int countPiecesOnStand(Player pl) const
後方互換
Definition simpleState.h:188
osl::SimpleState::setTurn
void setTurn(Player player)
Definition simpleState.h:217
osl::SimpleState::nth
const Piece nth(int n) const
unpromote(PTYPE)のn番目の駒を帰す.
Definition simpleState.h:114
osl::SimpleState::hasPieceOnStand
bool hasPieceOnStand(Player P) const
Definition simpleState.h:195
osl::SimpleState::isValidMoveByRule
static bool isValidMoveByRule(Move move, bool show_error)
盤面以外の部分の反則のチェック
Definition simpleState.cc:372
osl::SimpleState::stand_count
CArray< CArray< char, PTYPE_SIZE-PTYPE_BASIC_MIN >, 2 > stand_count
Definition simpleState.h:58
osl::SimpleState::used_mask
PieceMask used_mask
Definition simpleState.h:62
osl::SimpleState::setPieceOf
void setPieceOf(int num, Piece p)
Definition simpleState.h:79
osl::SimpleState::operator<<
friend std::ostream & operator<<(std::ostream &os, const SimpleState &state)
osl::SimpleState::init
void init()
盤面が空の状態に初期化
Definition simpleState.cc:44
osl::SimpleState::clearPawn
void clearPawn(Player pl, Square sq)
(internal)
Definition simpleState.h:138
osl::SimpleState::SimpleState
SimpleState()
Definition simpleState.cc:12
osl::SimpleState::standMask
PieceMask & standMask(Player p)
Definition simpleState.h:124
osl::SimpleState::isOnBoard
bool isOnBoard(int num) const
Definition simpleState.h:176
osl::SimpleState::operator[]
const Piece operator[](Square sq) const
Definition simpleState.h:168
osl::SimpleState::kingPiece
const Piece kingPiece() const
Definition simpleState.h:83
osl::SimpleState::getPiecePtr
const Piece * getPiecePtr(Square sq) const
Definition simpleState.h:169
osl::SimpleState::standMask
const PieceMask & standMask(Player p) const
Definition simpleState.h:128
osl::SimpleState::isPawnMaskSet
bool isPawnMaskSet(int x) const
Definition simpleState.h:152
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::SimpleState::emulateCapture
const SimpleState emulateCapture(Piece from, Player new_owner) const
from で表現されたPieceをnew_ownerの持駒にした局面を作る.
Definition simpleState.cc:460
osl::SimpleState::setPieceAll
void setPieceAll(Player player)
Definition simpleState.cc:139
osl::SimpleState::~SimpleState
virtual ~SimpleState()
Definition simpleState.cc:112
osl::SimpleState::changeTurn
void changeTurn()
手番を変更する
Definition simpleState.h:226
osl::SimpleState::isEmptyBetween
bool isEmptyBetween(Square from, Square to, bool noSpaceAtTo=false) const
Definition simpleState.h:293
osl::SimpleState::canDropPawnTo
bool canDropPawnTo(Player player, int x) const
xの筋に歩を打てる
Definition simpleState.h:155
osl::SimpleState::operator==
friend bool operator==(const SimpleState &st1, const SimpleState &st2)
盤上の駒のみを比較する（持ち駒は見ない）.
osl::SimpleState::initPawnMask
void initPawnMask()
Definition simpleState.cc:20
osl::SimpleState::stand_mask
CArray< PieceMask, 2 > stand_mask
Definition simpleState.h:56
osl::SimpleState::setBoard
void setBoard(Square sq, Piece piece)
Definition simpleState.h:119
osl::SimpleState::kingPiece
const Piece kingPiece(Player P) const
Definition simpleState.h:86
osl::SimpleState::pieces
CArray< Piece, Piece::SIZE > pieces
全てのpieceが登録されている
Definition simpleState.h:55
osl::SimpleState::usedMask
const PieceMask & usedMask() const
Definition simpleState.h:131
osl::SimpleState::testValidityOtherThanEffect
bool testValidityOtherThanEffect(Move move) const
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::SimpleState::isConsistent
bool isConsistent(bool show_error=true) const
Definition simpleState.cc:157
osl::SimpleState::flipHorizontal
const SimpleState flipHorizontal() const
Definition simpleState.cc:518
osl::SimpleState::isOffBoard
bool isOffBoard(int num) const
Definition simpleState.h:132
osl::SimpleState::setPawn
void setPawn(Player pl, Square sq)
(internal)
Definition simpleState.h:142
osl::SimpleState::countPiecesOnStandBit
int countPiecesOnStandBit(Player pl, Ptype ptype) const
Definition simpleState.h:199
osl::SimpleState::isEmptyBetween
bool isEmptyBetween(Square from, Square to, Offset offset, bool pieceExistsAtTo=false) const
Definition simpleState.h:267
osl::SimpleState::nthLimit
static int nthLimit()
Definition simpleState.h:105
osl::SimpleState::hasPawnMask
static const bool hasPawnMask
Definition simpleState.h:41
osl::SimpleState::kingSquare
Square kingSquare() const
Definition simpleState.h:94
osl::SimpleState::setPiece
void setPiece(Player player, Square sq, Ptype ptype)
Definition simpleState.cc:114
osl::SimpleState::isPawnMaskSet
bool isPawnMaskSet(Player player, int x) const
Definition simpleState.h:146
osl::SimpleState::countPiecesOnStand
int countPiecesOnStand(Player pl, Ptype ptype) const
持駒の枚数を数える
Definition simpleState.h:182
osl::SimpleState::pieceAt
const Piece pieceAt(Square sq) const
Definition simpleState.h:167
osl::SimpleState::rotate180
const SimpleState rotate180() const
Definition simpleState.cc:505
osl::SimpleState::isAlmostValidMove
bool isAlmostValidMove(Move move) const
エラー表示をするかどうかをtemplateパラメータにした高速化版
Definition simpleState.cc:332
osl::SimpleState::dump
bool dump() const
dump: 自分を cerr に表示する。abort 前などにデバッグに使う
Definition simpleState.cc:450
osl::Square
Definition basic_type.h:532
osl::Square::index
unsigned int index() const
Definition basic_type.h:572
osl::Square::isOnBoard
bool isOnBoard() const
盤面上を表すかどうかの判定． 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する．
Definition basic_type.h:583
container.h
effectContent.h
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::PTYPE_BASIC_MIN
@ PTYPE_BASIC_MIN
Definition basic_type.h:103
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::PTYPE_SIZE
const int PTYPE_SIZE
Definition basic_type.h:107
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::Offset32
Offset32Base< 8, 9 > Offset32
Definition offset32.h:63
osl::Player
Player
Definition basic_type.h:8
osl::WHITE
@ WHITE
Definition basic_type.h:10
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::__attribute__
const PtypeO PTYPEO_EDGE __attribute__((unused))
osl::Handicap
Handicap
Definition simpleState.h:20
osl::HIRATE
@ HIRATE
Definition simpleState.h:21
osl::isBasic
bool isBasic(Ptype ptype)
ptypeが基本型(promoteしていない)かのチェック
Definition basic_type.h:128
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
osl::operator<<
std::ostream & operator<<(std::ostream &os, Player player)
Definition basic_type.cc:14
osl::operator==
bool operator==(Square l, Square r)
Definition basic_type.h:758
pieceMask.h
ptypeTable.h
ptypeTraits.h
osl::KingTraits::index
static const int index
Definition ptypeTraits.h:358
osl::PtypeTraits
Definition ptypeTraits.h:12