patternGroup.cc
Go to the documentation of this file.
/* patternGroup.cc
*/
#include "osl/rating/group/patternGroup.h"
#include <sstream>
osl::rating::PatternGroup::PatternGroup(Direction d, Direction d2)
: Group(name(d, d2)), direction(d), direction2(d2)
{
for (int attack=0; attack<3; ++attack) {
for (int defense=0; defense<3; ++defense) {
for (int s=PTYPE_PIECE_MIN; s<= PTYPE_MAX; ++s) {
for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
push_back(new Pattern(d, d2, static_cast<Ptype>(s), static_cast<Ptype>(t), true, attack, defense));
push_back(new Pattern(d, d2, static_cast<Ptype>(s), static_cast<Ptype>(t), false, attack, defense));
}
push_back(new Pattern(d, d2, static_cast<Ptype>(s), PTYPE_EMPTY, true, attack, defense));
push_back(new Pattern(d, d2, static_cast<Ptype>(s), PTYPE_EDGE, true, attack, defense)); // redundant
}
}
}
target_table.fill(0);
for (int x=1; x<=9; ++x) {
for (int y=1; y<=9; ++y) {
const Square src(x,y);
const Square target_b = Pattern::nextSquare(BLACK, src, direction, direction2);
const Square target_w = Pattern::nextSquare(WHITE, src, direction, direction2);
target_table[playerToIndex(BLACK)][src.index()] = target_b.uintValue();
target_table[playerToIndex(WHITE)][src.index()] = target_w.uintValue();
}
}
}
int osl::rating::PatternGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
{
const Ptype self = move.ptype();
const Square position
= Square::makeDirect(target_table[playerToIndex(move.player())][move.to().index()]);
assert(position == Pattern::nextSquare(move, direction, direction2));
const Piece p = (position == move.from()) ? Piece::EMPTY() : state.pieceAt(position);
const Ptype target = p.ptype();
if (env.pattern_cache[position.index()] < 0)
env.pattern_cache[position.index()] = CountEffect2::index(state, position, env)
* (PTYPE_MAX+1-PTYPE_PIECE_MIN) * ((PTYPE_MAX+1 - PTYPE_PIECE_MIN)*2 +2);
const int base = env.pattern_cache[position.index()];
int index = base + (self - PTYPE_PIECE_MIN)*((PTYPE_MAX+1 - PTYPE_PIECE_MIN)*2 +2);
if (!isPiece(target)) {
index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*2 + (target == PTYPE_EMPTY ? 0 : 1);
}
else {
index += (target - PTYPE_PIECE_MIN)*2 + (p.owner() != move.player());
}
return index;
}
std::string osl::rating::PatternGroup::name(Direction direction, Direction direction2)
{
std::ostringstream ss;
ss << "Pattern" << direction;
if (direction2 != Pattern::INVALID)
ss << direction2;
return ss.str();
}
const osl::CArray<osl::Direction,4> osl::rating::PatternLongGroup::rook_direction4 = {{ U, L, D, R }};
const osl::CArray<osl::Direction,4> osl::rating::PatternLongGroup::bishop_direction4 = {{ UL, DL, DR, UR }};
std::string osl::rating::PatternLongGroup::name(int direction_id)
{
std::ostringstream ss;
ss << "PatLong" << direction_id;
return ss.str();
}
osl::rating::PatternLongGroup::PatternLongGroup(int d)
: Group(name(d)), direction_id(d)
{
const CArray<Ptype,5> self_list = {{ ROOK, PROOK, BISHOP, PBISHOP, LANCE }};
for (int s=0; s<((d == 0) ? 5 : 4); ++s) {
const Ptype self = self_list[s];
const Direction direction = makeDirection(self);
for (int attack=0; attack<3; ++attack) {
for (int defense=0; defense<3; ++defense) {
for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), true, true, attack, defense)));
push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), true, false, attack, defense)));
push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), false, true, attack, defense)));
push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), false, false, attack, defense)));
}
push_back(new PatternLong(direction, self, LongTarget(PTYPE_EMPTY, true, true, attack, defense)));
push_back(new PatternLong(direction, self, LongTarget(PTYPE_EMPTY, false, true, attack, defense)));
}
}
push_back(new PatternLong(direction, self, LongTarget(PTYPE_EDGE, true, true, 0, 0)));
}
}
int osl::rating::PatternLongGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
{
const size_t unit = ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*9+1;
const Ptype self = move.ptype();
int base = 0;
switch (self) {
case ROOK:
break;
case PROOK:
base += unit; break;
case BISHOP:
base += unit*2; break;
case PBISHOP:
base += unit*3; break;
case LANCE:
if (direction_id != 0)
return -1;
base += unit*4; break;
default:
return -1;
}
const Direction direction = makeDirection(self);
const PieceSquare pp = PatternLong::find(state, move, direction);
int index = base;
if (pp.first.isEdge()) {
index += unit - 1;
} else {
index += ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*CountEffect2::index(state, pp.second, env);
if (pp.first.isEmpty()) {
index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*4;
index += ! LongTarget::isPromotable(move, pp.second);
}
else {
assert(pp.first.isPiece());
index += (pp.first.ptype()-PTYPE_PIECE_MIN)*4;
index += (! LongTarget::isPromotable(move, pp.second))*2;
index += (pp.first.owner() != move.player());
}
}
return index;
}
std::string osl::rating::PatternLongGroup2::name(int direction_id)
{
std::ostringstream ss;
ss << "PatLong2" << direction_id;
return ss.str();
}
osl::rating::PatternLongGroup2::PatternLongGroup2(int d)
: Group(name(d)), direction_id(d)
{
const CArray<Ptype,5> self_list = {{ ROOK, PROOK, BISHOP, PBISHOP, LANCE }};
for (int s=0; s<((d == 0) ? 5 : 4); ++s) {
const Ptype self = self_list[s];
const Direction direction = makeDirection(self);
for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
push_back(new PatternLong2(direction, self, LongTarget2(static_cast<Ptype>(t), true)));
push_back(new PatternLong2(direction, self, LongTarget2(static_cast<Ptype>(t), false)));
}
push_back(new PatternLong2(direction, self, LongTarget2(PTYPE_EDGE, true)));
}
}
int osl::rating::PatternLongGroup2::findMatch(const NumEffectState& state, Move move, const RatingEnv& ) const
{
const size_t unit = (PTYPE_MAX+1-PTYPE_PIECE_MIN)*2+1;
const Ptype self = move.ptype();
int base = 0;
switch (self) {
case ROOK:
break;
case PROOK:
base += unit; break;
case BISHOP:
base += unit*2; break;
case PBISHOP:
base += unit*3; break;
case LANCE:
if (direction_id != 0)
return -1;
base += unit*4; break;
default:
return -1;
}
const Direction direction = makeDirection(self);
const Piece p = PatternLong2::find(state, move, direction);
int index = base;
if (! p.isPiece()) {
index += unit - 1;
} else {
assert(p.isPiece());
index += (p.ptype()-PTYPE_PIECE_MIN)*2;
index += (p.owner() != move.player());
}
return index;
}
osl::rating::
PatternBlockGroup::PatternBlockGroup(Ptype a)
: Group(std::string("PatternBlock")+Ptype_Table.getCsaName(a)), attacker(a)
{
assert(a == LANCE || a == ROOK || a == BISHOP);
for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
const Ptype self = static_cast<Ptype>(s);
for (int attack=0; attack<3; ++attack) {
for (int defense=0; defense<3; ++defense) {
for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), true, true, attack, defense)));
push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), true, false, attack, defense)));
push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), false, true, attack, defense)));
push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), false, false, attack, defense)));
}
push_back(new PatternBlock(self, a, LongTarget(PTYPE_EMPTY, true, true, attack, defense)));
push_back(new PatternBlock(self, a, LongTarget(PTYPE_EMPTY, false, true, attack, defense)));
}
}
}
}
int osl::rating::
PatternBlockGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
{
const size_t unit = ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*9;
const PieceSquare pp = PatternBlock::find(state, move, attacker);
if (pp.first.isEdge())
return -1;
int index = (move.ptype() - PTYPE_PIECE_MIN)*unit;
index += ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*CountEffect2::index(state, pp.second, env);
if (pp.first.isEmpty()) {
index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*4;
index += ! pp.second.canPromote(alt(state.turn()));
}
else {
assert(pp.first.isPiece());
index += (pp.first.ptype()-PTYPE_PIECE_MIN)*4;
index += (! pp.second.canPromote(pp.first.isPiece() ? alt(pp.first.owner()) : alt(move.player())))*2;
index += (pp.first.owner() != move.player());
}
return index;
}
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::CArray
Definition container.h:20
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptype
Ptype ptype() const
Definition basic_type.h:1155
osl::Move::player
Player player() const
Definition basic_type.h:1195
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptype
Ptype ptype() const
Definition basic_type.h:821
osl::Piece::owner
Player owner() const
Definition basic_type.h:963
osl::Piece::isPiece
bool isPiece() const
Definition basic_type.h:953
osl::Piece::EMPTY
static const Piece EMPTY()
Definition basic_type.h:797
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::SimpleState::pieceAt
const Piece pieceAt(Square sq) const
Definition simpleState.h:167
osl::Square
Definition basic_type.h:532
osl::Square::index
unsigned int index() const
Definition basic_type.h:572
osl::Square::uintValue
unsigned int uintValue() const
Definition basic_type.h:539
osl::Square::makeDirect
static const Square makeDirect(int value)
Definition basic_type.h:538
osl::rating::Group::Group
Group(const std::string &name)
Definition group.cc:15
osl::rating::PatternBlockGroup::attacker
Ptype attacker
Definition patternGroup.h:78
osl::rating::PatternBlockGroup::PatternBlockGroup
PatternBlockGroup(Ptype attacker)
Definition patternGroup.cc:199
osl::rating::PatternBlockGroup::findMatch
int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
Definition patternGroup.cc:221
osl::rating::PatternBlock
Definition pattern.h:174
osl::rating::PatternBlock::find
static const PieceSquare find(const NumEffectState &state, Move move, Ptype attacker_ptype)
Definition pattern.cc:105
osl::rating::PatternLong2
Definition pattern.h:150
osl::rating::PatternLong2::find
static const Piece find(const NumEffectState &state, Move move, Direction direction)
Definition pattern.h:157
osl::rating::PatternLongGroup2::PatternLongGroup2
PatternLongGroup2(int d)
Definition patternGroup.cc:148
osl::rating::PatternLongGroup2::name
static std::string name(int direction_id)
Definition patternGroup.cc:141
osl::rating::PatternLongGroup2::findMatch
int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
Definition patternGroup.cc:163
osl::rating::PatternLongGroup2::direction_id
int direction_id
Definition patternGroup.h:57
osl::rating::PatternLongGroup2::makeDirection
Direction makeDirection(Ptype ptype) const
Definition patternGroup.h:61
osl::rating::PatternLongGroup::findMatch
int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
Definition patternGroup.cc:98
osl::rating::PatternLongGroup::PatternLongGroup
PatternLongGroup(int d)
Definition patternGroup.cc:75
osl::rating::PatternLongGroup::makeDirection
Direction makeDirection(Ptype ptype) const
Definition patternGroup.h:40
osl::rating::PatternLongGroup::bishop_direction4
static const CArray< Direction, 4 > bishop_direction4
Definition patternGroup.h:66
osl::rating::PatternLongGroup::direction_id
int direction_id
Definition patternGroup.h:36
osl::rating::PatternLongGroup::rook_direction4
static const CArray< Direction, 4 > rook_direction4
Definition patternGroup.h:65
osl::rating::PatternLongGroup::name
static std::string name(int direction_id)
Definition patternGroup.cc:68
osl::rating::PatternLong
Definition pattern.h:123
osl::rating::PatternLong::find
static const PieceSquare find(const NumEffectState &state, Move move, Direction direction)
Definition pattern.cc:66
osl::rating::Pattern
Definition pattern.h:14
osl::rating::Pattern::nextSquare
static Square nextSquare(Player player, Square start, Direction direction, Direction direction2)
Definition pattern.h:33
osl::rating::Pattern::INVALID
static const Direction INVALID
Definition pattern.h:16
osl::rating::RatingEnv
Definition ratingEnv.h:16
osl::rating::RatingEnv::pattern_cache
CArray< int, Square::SIZE > pattern_cache
Definition ratingEnv.h:24
osl::rating::PieceSquare
std::pair< Piece, Square > PieceSquare
Definition pattern.h:60
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::PTYPE_PIECE_MIN
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
osl::PTYPE_MAX
@ PTYPE_MAX
Definition basic_type.h:105
osl::ROOK
@ ROOK
Definition basic_type.h:100
osl::BISHOP
@ BISHOP
Definition basic_type.h:99
osl::PROOK
@ PROOK
Definition basic_type.h:92
osl::PTYPE_EDGE
@ PTYPE_EDGE
Definition basic_type.h:86
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition basic_type.h:85
osl::PBISHOP
@ PBISHOP
Definition basic_type.h:91
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::playerToIndex
constexpr int playerToIndex(Player player)
Definition basic_type.h:16
osl::Direction
Direction
Definition basic_type.h:310
osl::R
@ R
Definition basic_type.h:317
osl::D
@ D
Definition basic_type.h:319
osl::UL
@ UL
Definition basic_type.h:313
osl::DR
@ DR
Definition basic_type.h:320
osl::U
@ U
Definition basic_type.h:314
osl::L
@ L
Definition basic_type.h:316
osl::UR
@ UR
Definition basic_type.h:315
osl::DL
@ DL
Definition basic_type.h:318
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::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
std
Definition basic_type.h:1353
patternGroup.h
osl::rating::CountEffect2::index
static int index(const NumEffectState &state, Square position, const RatingEnv &env)
Definition countEffect2.h:43
osl::rating::LongTarget2
Definition pattern.h:104
osl::rating::LongTarget
Definition pattern.h:62
osl::rating::LongTarget::isPromotable
static bool isPromotable(Move move, Square position)
Definition pattern.h:69
osl::rating::PatternGroup::direction2
Direction direction2
Definition patternGroup.h:16
osl::rating::PatternGroup::target_table
CArray2d< unsigned char, 2, Square::SIZE > target_table
Definition patternGroup.h:17
osl::rating::PatternGroup::findMatch
int findMatch(const NumEffectState &state, Move m, const RatingEnv &) const
Definition patternGroup.cc:33
osl::rating::PatternGroup::direction
Direction direction
Definition patternGroup.h:16
osl::rating::PatternGroup::PatternGroup
PatternGroup(Direction d, Direction d2=Pattern::INVALID)
Definition patternGroup.cc:6
osl::rating::PatternGroup::name
static std::string name(Direction direction, Direction direction2)
Definition patternGroup.cc:56