basic_type.cc
Go to the documentation of this file.
#include "osl/basic_type.h"
#include "osl/bits/boardTable.h"
#include "osl/bits/ptypeTable.h"
#include "osl/simpleState.h"
#include "osl/bits/squareCompressor.h"
// #include "move-phash.c"
#include <iostream>
bool osl::isValid(Player player)
{
return player==BLACK || player==WHITE;
}
std::ostream& osl::operator<<(std::ostream& os,Player player)
{
if(player==BLACK)
return os << "+";
else
return os << "-";
}
␌
bool osl::isValid(Ptype ptype)
{
return static_cast<int>(ptype)>=PTYPE_MIN
&& static_cast<int>(ptype)<=PTYPE_MAX;
}
bool osl::isValidPtypeO(int ptypeO)
{
return (ptypeO >= PTYPEO_MIN) && (ptypeO <= PTYPEO_MAX);
}
std::istream& osl::operator>>(std::istream& is, osl::Ptype& ptype)
{
std::string s;
is >> s;
if (s == "PTYPE_EMPTY")
ptype = PTYPE_EMPTY;
else if (s == "PTYPE_EDGE")
ptype = PTYPE_EDGE;
else if (s == "PPAWN")
ptype = PPAWN;
else if (s == "PLANCE")
ptype = PLANCE;
else if (s == "PKNIGHT")
ptype = PKNIGHT;
else if (s == "PSILVER")
ptype = PSILVER;
else if (s == "PBISHOP")
ptype = PBISHOP;
else if (s == "PROOK")
ptype = PROOK;
else if (s == "KING")
ptype = KING;
else if (s == "GOLD")
ptype = GOLD;
else if (s == "PAWN")
ptype = PAWN;
else if (s == "LANCE")
ptype = LANCE;
else if (s == "KNIGHT")
ptype = KNIGHT;
else if (s == "SILVER")
ptype = SILVER;
else if (s == "BISHOP")
ptype = BISHOP;
else if (s == "ROOK")
ptype = ROOK;
else{
std::cerr << "Incorrect input : " << s << std::endl;
ptype = PTYPE_EMPTY;
}
return is;
}
std::ostream& osl::operator<<(std::ostream& os,const osl::Ptype ptype)
{
return os << Ptype_Table.getName(ptype);
}
std::ostream& osl::operator<<(std::ostream& os,const osl::PtypeO ptypeO)
{
if (isPiece(ptypeO))
return os << "PtypeO(" << getOwner(ptypeO) << ","
<< getPtype(ptypeO) << ")";
return os << "PtypeO(" << (int)ptypeO << "," << getPtype(ptypeO) << ")";
}
␌
bool osl::isValid(Direction d){
return DIRECTION_MIN<=d && d<=DIRECTION_MAX;
}
std::ostream& osl::operator<<(std::ostream& os,const Direction d){
static const char* names[]={
"UL","U","UR","L",
"R","DL","D","DR",
"UUL","UUR","LONG_UL",
"LONG_U","LONG_UR","LONG_L",
"LONG_R","LONG_DL","LONG_D","LONG_DR"
};
return os << names[static_cast<int>(d)];
}
#define OFFSET_INDEX(dx,dy) ((dx*BOARD_HEIGHT + dy) - OFFSET_MIN)
osl::Offset::Offset(Player player, Direction direction)
{
*this = Board_Table.getOffset(player, direction);
}
int osl::Offset::dx() const
{
switch (index())
{
case OFFSET_INDEX(-1,-2): return -1;
case OFFSET_INDEX(1,-2): return 1;
case OFFSET_INDEX(-1,-1): return -1;
case OFFSET_INDEX(0,-1): return 0;
case OFFSET_INDEX(1,-1): return 1;
case OFFSET_INDEX(-1,0): return -1;
case OFFSET_INDEX(1,0): return 1;
case OFFSET_INDEX(-1,1): return -1;
case OFFSET_INDEX(0,1): return 0;
case OFFSET_INDEX(1,1): return 1;
case OFFSET_INDEX(-1,2): return -1;
case OFFSET_INDEX(1,2): return 1;
default:
std::cerr << index() << " " << ZERO().index() << "\n";
assert(0);
}
return 0;
}
int osl::Offset::dy() const
{
switch (index())
{
case OFFSET_INDEX(-1,-2): return -2;
case OFFSET_INDEX(1,-2): return -2;
case OFFSET_INDEX(-1,-1): return -1;
case OFFSET_INDEX(0,-1): return -1;
case OFFSET_INDEX(1,-1): return -1;
case OFFSET_INDEX(-1,0): return 0;
case OFFSET_INDEX(1,0): return 0;
case OFFSET_INDEX(-1,1): return 1;
case OFFSET_INDEX(0,1): return 1;
case OFFSET_INDEX(1,1): return 1;
case OFFSET_INDEX(-1,2): return 2;
case OFFSET_INDEX(1,2): return 2;
default: assert(0);
}
return 0;
}
#ifndef MINIMAL
std::ostream& osl::operator<<(std::ostream& os, Offset offset)
{
return os << "offset(" << offset.intValue() << ')';
}
#endif
␌
static_assert(sizeof(osl::Square) == 4, "square size");
bool osl::Square::isOnBoardSlow() const
{
return (1<=x() && x() <=9
&& 1<=y() && y() <=9);
}
bool osl::Square::isValid() const
{
return isPieceStand() || isOnBoard();
}
const osl::Square osl::
Square::neighbor(Player P, Direction D) const
{
return Board_Table.nextSquare(P, *this, D);
}
const osl::Square osl::
Square::back(Player P, Direction D) const
{
return Board_Table.nextSquare(alt(P), *this, D);
}
bool osl::Square::isNeighboring8(Square to) const {
return (*this != to)
&& (to == *this+Board_Table.getShortOffsetNotKnight(Offset32(to,*this)));
}
std::ostream& osl::operator<<(std::ostream& os, Square square)
{
if (square.isPieceStand())
return os << "OFF";
return os << "Square(" << square.x() << square.y() << ")";
}
␌
static_assert(sizeof(osl::Piece) == 4, "piece size");
std::ostream& osl::operator<<(std::ostream& os,const Piece piece)
{
if (piece.isPiece())
os << "Piece(" << piece.owner() << "," << piece.ptype()
<< ",num=" << piece.number()
<< "," << piece.square() << ')';
else if (piece == Piece::EMPTY())
os << "PIECE_EMPTY";
else if (piece == Piece::EDGE())
os << "PIECE_EDGE";
else
os << "unknown piece?!";
return os;
}
const osl::Piece osl::Piece::makeKing(Player owner, Square position)
{
const int number = ((owner == BLACK)
? (int)KingTraits<BLACK>::index
: (int)KingTraits<WHITE>::index);
return Piece(owner, KING, number, position);
}
␌
namespace osl
{
static_assert(sizeof(Move) == 4, "move size");
} //namespace osl
bool osl::Move::isValid() const
{
if (! isNormal())
return false;
const Square from = this->from();
if (! from.isValid())
return false;
const Square to = this->to();
if (! to.isOnBoard())
return false;
return osl::isValid(ptype())
&& osl::isValid(capturePtype())
&& capturePtype()!=KING
&& osl::isValid(player());
}
const osl::Move osl::Move::rotate180() const
{
if (isPass())
return Move::PASS(alt(player()));
if (! isNormal())
return *this;
return Move(from().rotate180Safe(), to().rotate180(), ptype(),
capturePtype(), isPromotion(), alt(player()));
}
std::ostream& osl::operator<<(std::ostream& os,const Move move)
{
if (move == Move::DeclareWin())
return os << "MOVE_DECLARE_WIN";
if (move.isInvalid())
return os << "MOVE_INVALID";
if (move.isPass())
return os << "MOVE_PASS";
const Player turn = move.player();
if (move.isValid())
{
if (move.from().isPieceStand())
{
os << "Drop(" << turn << "," << move.ptype() << "," << move.to() << ")";
}
else
{
const Ptype capture_ptype=move.capturePtype();
os << "Move(" << turn << "," << move.ptype() << ","
<< move.from() << "->" << move.to() ;
if (move.promoteMask())
os << ",promote";
if (capture_ptype != PTYPE_EMPTY)
os << ",capture=" << capture_ptype;
os << ")";
}
}
else
{
os << "InvalidMove " << move.from() << " " << move.to()
<< " " << move.ptypeO() << " " << move.oldPtypeO()
<< " " << move.promoteMask()
<< " " << move.capturePtype() << "\n";
}
return os;
}
unsigned int osl::Move::hash() const
{
assert(capturePtype() == PTYPE_EMPTY);
// return move_phash(intValue());
return intValue();
}
const osl::Move osl::Move::
fromMove16(Move16 move16, const SimpleState& state)
{
if (move16==MOVE16_NONE)
return Move();
Player turn=state.turn();
Square to=SquareCompressor::melt((move16>>8)&0x7f);
if((move16&0x80)!=0){
Ptype ptype=(Ptype)(move16-0x80);
return Move(to,ptype,turn);
}
Square from=SquareCompressor::melt(move16&0x7f);
Ptype ptype=state[from].ptype();
Ptype capture_ptype=state[to].ptype();
bool is_promote=(move16&0x8000)!=0;
if(is_promote)
return Move(from,to,::osl::promote(ptype),capture_ptype,true,turn);
else
return Move(from,to,ptype,capture_ptype,false,turn);
}
osl::Move16 osl::Move::toMove16() const
{
if (isInvalid())
return MOVE16_NONE;
if (isDrop())
return Move16(0x80+(uint16_t)ptype()+((SquareCompressor::compress(to()))<<8));
if (isPromotion())
return Move16(SquareCompressor::compress(from())+(SquareCompressor::compress(to())<<8)+0x8000);
return Move16(SquareCompressor::compress(from())+(SquareCompressor::compress(to())<<8));
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
OFFSET_INDEX
#define OFFSET_INDEX(dx, dy)
Definition basic_type.cc:108
basic_type.h
boardTable.h
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition basic_type.h:1162
osl::Move::PASS
static const Move PASS(Player P)
Definition basic_type.h:1094
osl::Move::isValid
bool isValid() const
Definition basic_type.cc:246
osl::Move::isInvalid
bool isInvalid() const
state に apply 可能でない場合にtrue
Definition basic_type.h:1202
osl::Move::isPromotion
bool isPromotion() const
Definition basic_type.h:1147
osl::Move::ptype
Ptype ptype() const
Definition basic_type.h:1155
osl::Move::player
Player player() const
Definition basic_type.h:1195
osl::Move::oldPtypeO
PtypeO oldPtypeO() const
移動前のPtypeO, i.e., 成る手だった場合成る前
Definition basic_type.h:1168
osl::Move::toMove16
Move16 toMove16() const
Definition basic_type.cc:336
osl::Move::isDrop
bool isDrop() const
Definition basic_type.h:1150
osl::Move::DeclareWin
static const Move DeclareWin()
Definition basic_type.h:1096
osl::Move::isPass
bool isPass() const
Definition basic_type.h:1092
osl::Move::capturePtype
Ptype capturePtype() const
Definition basic_type.h:1180
osl::Move::fromMove16
static const Move fromMove16(Move16, const SimpleState &)
Definition basic_type.cc:317
osl::Move::Move
Move(int value)
Definition basic_type.h:1057
osl::Move::isNormal
bool isNormal() const
INVALID でも PASS でもない.
Definition basic_type.h:1088
osl::Move::promoteMask
int promoteMask() const
pieceに使うためのmaskなので
Definition basic_type.h:1143
osl::Move::rotate180
const Move rotate180() const
Definition basic_type.cc:262
osl::Move::hash
unsigned int hash() const
駒を取らない手を [0, 16305] にmap
Definition basic_type.cc:309
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::Move::intValue
int intValue() const
Definition basic_type.h:1065
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::Offset
座標の差分
Definition basic_type.h:430
osl::Offset::index
unsigned int index() const
Definition basic_type.h:471
osl::Offset::Offset
Offset()
Definition basic_type.h:455
osl::Offset::intValue
int intValue() const
Definition basic_type.h:448
osl::Offset::dx
int dx() const
Offsetから一般に dxは求まらないので, ここでの入力は12近傍のみとする
Definition basic_type.cc:119
osl::Offset::dy
int dy() const
Offsetから一般に dyは求まらないので, ここでの入力は12近傍のみとする
Definition basic_type.cc:146
osl::Offset::ZERO
static const Offset ZERO()
Definition basic_type.h:460
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptype
Ptype ptype() const
Definition basic_type.h:821
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::owner
Player owner() const
Definition basic_type.h:963
osl::Piece::isPiece
bool isPiece() const
Definition basic_type.h:953
osl::Piece::Piece
Piece(int p)
Definition basic_type.h:790
osl::Piece::number
int number() const
Definition basic_type.h:828
osl::Piece::EMPTY
static const Piece EMPTY()
Definition basic_type.h:797
osl::Piece::makeKing
static const Piece makeKing(Player owner, Square square)
玉を作る
Definition basic_type.cc:231
osl::Piece::EDGE
static const Piece EDGE()
Definition basic_type.h:798
osl::SimpleState
Definition simpleState.h:35
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::Square
Definition basic_type.h:532
osl::Square::isPieceStand
bool isPieceStand() const
Definition basic_type.h:576
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::isNeighboring8
bool isNeighboring8(Square to) const
Definition basic_type.cc:202
osl::Square::isOnBoardSlow
bool isOnBoardSlow() const
Definition basic_type.cc:178
osl::Square::neighbor
const Square neighbor() const
Definition basic_type.h:746
osl::Square::back
const Square back() const
Definition basic_type.h:750
osl::Square::isOnBoard
bool isOnBoard() const
盤面上を表すかどうかの判定． 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する．
Definition basic_type.h:583
osl::Square::isValid
bool isValid() const
Definition basic_type.cc:184
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::Square::Square
Square(int p)
Definition basic_type.h:534
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::PTYPE_MAX
@ PTYPE_MAX
Definition basic_type.h:105
osl::ROOK
@ ROOK
Definition basic_type.h:100
osl::PPAWN
@ PPAWN
Definition basic_type.h:87
osl::BISHOP
@ BISHOP
Definition basic_type.h:99
osl::PROOK
@ PROOK
Definition basic_type.h:92
osl::PAWN
@ PAWN
Definition basic_type.h:95
osl::PTYPE_EDGE
@ PTYPE_EDGE
Definition basic_type.h:86
osl::KING
@ KING
Definition basic_type.h:93
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition basic_type.h:85
osl::SILVER
@ SILVER
Definition basic_type.h:98
osl::PKNIGHT
@ PKNIGHT
Definition basic_type.h:89
osl::GOLD
@ GOLD
Definition basic_type.h:94
osl::PLANCE
@ PLANCE
Definition basic_type.h:88
osl::PBISHOP
@ PBISHOP
Definition basic_type.h:91
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::PTYPE_MIN
@ PTYPE_MIN
Definition basic_type.h:102
osl::PSILVER
@ PSILVER
Definition basic_type.h:90
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::Move16
Move16
16bit 表現
Definition basic_type.h:1023
osl::MOVE16_NONE
@ MOVE16_NONE
Definition basic_type.h:1024
osl::getPtype
Ptype getPtype(PtypeO ptypeO)
Definition basic_type.h:217
osl::isValidPtypeO
bool isValidPtypeO(int ptypeO)
Definition basic_type.cc:30
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::getOwner
Player getOwner(PtypeO ptypeO)
Definition basic_type.h:256
osl::isValid
bool isValid(Player player)
cast等で作られたplayerが正しいかどうかを返す
Definition basic_type.cc:9
osl::Direction
Direction
Definition basic_type.h:310
osl::D
@ D
Definition basic_type.h:319
osl::DIRECTION_MAX
@ DIRECTION_MAX
Definition basic_type.h:337
osl::DIRECTION_MIN
@ DIRECTION_MIN
Definition basic_type.h:334
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::isPiece
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition basic_type.h:120
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
osl::PTYPEO_MAX
@ PTYPEO_MAX
Definition basic_type.h:201
osl::PTYPEO_MIN
@ PTYPEO_MIN
Definition basic_type.h:200
osl::operator>>
std::istream & operator>>(std::istream &is, Ptype &ptype)
Definition basic_type.cc:35
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
osl::promote
Ptype promote(Ptype ptype)
promote可能なptypeに対して，promote後の型を返す promote不可のptypeを与えてはいけない．
Definition basic_type.h:173
osl::operator<<
std::ostream & operator<<(std::ostream &os, Player player)
Definition basic_type.cc:14
ptypeTable.h
simpleState.h
squareCompressor.h
osl::KingTraits::index
static const int index
Definition ptypeTraits.h:358
osl::SquareCompressor::compress
static int compress(Square pos)
Definition squareCompressor.h:24
osl::SquareCompressor::melt
static Square melt(int index)
Definition squareCompressor.h:34