effect5x3Table.cc
Go to the documentation of this file.
/* effect5x3Table.cc
*/
#include "osl/progress/effect5x3Table.h"
#include "osl/bits/centering5x3.h"
#include "osl/basic_type.h"
#include "osl/bits/ptypeTable.h"
#include "osl/bits/boardTable.h"
#include "osl/oslConfig.h"
#include <iostream>
osl::progress::Effect5x3Table osl::progress::Effect5x3_Table;
static osl::SetUpRegister _initializer([](){ osl::progress::Effect5x3_Table.init(); });
namespace osl
{
namespace progress
{
namespace
{
int countShort(PtypeO ptypeO,int center_dx, int center_dy){
int ret=0;
for(int dy=std::max(-8,center_dy-1);
dy<=std::min(8,center_dy+1);dy++)
for(int dx=std::max(-8,center_dx-2);
dx<=std::min(8,center_dx+2);dx++){
EffectContent ec=Ptype_Table.getEffect(ptypeO,Offset32(dx,dy));
// unBlockableではなくて，ほんとのshortが欲しい
if(ec == EffectContent::DIRECT()){
ret+=8;
}
}
return ret;
}
LongEffect calcLong(Direction d,int center_dx, int center_dy){
LongEffect ret;
ret.minIndex=0;
ret.maxIndex=0;
int dy_min=std::max(-8,center_dy-1);
int dy_max=std::min(8,center_dy+1);
int dx_min=std::max(-8,center_dx-2);
int dx_max=std::min(8,center_dx+2);
int dx=Board_Table.getDxForBlack(d);
int dy=Board_Table.getDyForBlack(d);
assert(dx!=0 || dy!=0);
int i;
for(i=1;i<9;i++){
if(dx_min<=dx*i && dx*i <=dx_max &&
dy_min<=dy*i && dy*i <=dy_max) break;
}
if(i==9) return ret;
ret.minIndex=i;
for(;i<9;i++){
if(!(dx_min<=dx*i && dx*i <=dx_max &&
dy_min<=dy*i && dy*i <=dy_max)) break;
}
ret.maxIndex=i-1;
ret.offset=Offset(dx,dy);
assert(!ret.offset.zero());
return ret;
}
LongEffect4 calcLong4(PtypeO ptypeO,int center_dx, int center_dy){
LongEffect4 ret;
int index=0;
Player pl=getOwner(ptypeO);
for(int i=0;i<8;i++){
Direction d=static_cast<Direction>(i);
Offset32 o32(Board_Table.getDxForBlack(d)*sign(pl),
Board_Table.getDyForBlack(d)*sign(pl));
Direction longD=Board_Table.getLongDirection<BLACK>(o32);
if((Ptype_Table.getMoveMask(getPtype(ptypeO))&
dirToMask(shortToLong(d)))!=0){
assert(index<4);
ret[index]=calcLong(longD,center_dx,center_dy);
if(!ret[index].offset.zero())index++;
}
}
return ret;
}
}
#ifndef MINIMAL
std::ostream& operator<<(std::ostream& os,LongEffect const& longEffect){
return os << "(" << longEffect.offset << "," << longEffect.minIndex
<< "," << longEffect.maxIndex << ")";
}
#endif
}
}
void osl::progress::Effect5x3Table::setupOnStand()
{
onStand[PAWN] =StandPAWN;
onStand[LANCE] =StandLANCE;
onStand[KNIGHT]=StandKNIGHT;
onStand[SILVER]=StandSILVER;
onStand[GOLD] =StandGOLD;
onStand[BISHOP]=StandBISHOP;
onStand[ROOK] =StandROOK;
for(int i=PTYPE_PIECE_MIN;i<=PTYPE_MAX;i++){
Ptype ptype=static_cast<Ptype>(i);
if(unpromote(ptype)!=ptype)
onStand[ptype]=onStand[unpromote(ptype)];
}
}
void osl::progress::Effect5x3Table::setupShortEffect()
{
Player pl=BLACK;
for(int i=0;i<2;i++,pl=alt(pl)){
for(int j=PTYPE_PIECE_MIN;j<=PTYPE_MAX;j++){
PtypeO ptypeO=newPtypeO(pl,static_cast<Ptype>(j));
for(int dy= -8;dy<=8;dy++)
for(int dx= -8;dx<=8;dx++){
Offset32 o32(dx,dy);
shortEffect[ptypeOIndex(ptypeO)][o32.index()]=
countShort(ptypeO,dx,dy);
}
}
}
}
void osl::progress::Effect5x3Table::setupBlockEffect()
{
Player pl=BLACK;
for(int i=0;i<2;i++,pl=alt(pl)){
for(int j=0;j<8;j++){
Direction d=static_cast<Direction>(j);
for(int dy= -8;dy<=8;dy++)
for(int dx= -8;dx<=8;dx++){
Offset32 o32(dx,dy);
blockEffect[j][o32.index()]=
calcLong(d,dx,dy);
}
}
}
}
void osl::progress::Effect5x3Table::setupLongEffect()
{
Player pl=BLACK;
for(int i=0;i<2;i++,pl=alt(pl)){
for(int j=PTYPE_PIECE_MIN;j<=PTYPE_MAX;j++){
PtypeO ptypeO=newPtypeO(pl,static_cast<Ptype>(j));
for(int dy= -8;dy<=8;dy++)
for(int dx= -8;dx<=8;dx++){
Offset32 o32(dx,dy);
longEffect[ptypeOIndex(ptypeO)][o32.index()]=
calcLong4(ptypeO,dx,dy);
}
}
}
}
void osl::progress::Effect5x3Table::setupAttackEffect()
{
// 黒の攻める際のテーブルを作成
// 玉は白
for(int x=1;x<=9;x++)
for(int y=1;y<=9;y++){
const Square king(x,y);
const Square center = Centering5x3::adjustCenter(king);
for(int dx=0;dx<5;dx++){
for(int dy=0;dy<3;dy++){
const Square p(center.x()+dx-2,center.y()+dy-1);
int val=16-std::abs(p.x()-king.x());
attackEffect[BLACK][king.index()][dx*3+dy]=val;
}
}
}
for(int x=1;x<=9;x++)
for(int y=1;y<=9;y++){
const Square king(x,y);
const Square r_king=king.rotate180();
for(int dx=0;dx<5;dx++){
for(int dy=0;dy<3;dy++){
int r_dx=4-dx,r_dy=2-dy;
attackEffect[WHITE][king.index()][dx*3+dy]=
attackEffect[BLACK][r_king.index()][r_dx*3+r_dy];
}
}
}
}
void osl::progress::Effect5x3Table::setupDefenseEffect()
{
// 白が守る際のテーブルを作成
// 玉は白
for(int x=1;x<=9;x++)
for(int y=1;y<=9;y++){
const Square king(x,y);
const Square center = Centering5x3::adjustCenter(king);
for(int dx=0;dx<5;dx++){
for(int dy=0;dy<3;dy++){
const Square p(center.x()+dx-2,center.y()+dy-1);
int val=16-std::abs(p.x()-king.x());
defenseEffect[WHITE][king.index()][dx*3+dy]=val;
}
}
}
for(int x=1;x<=9;x++)
for(int y=1;y<=9;y++){
const Square king(x,y);
const Square r_king=king.rotate180();
for(int dx=0;dx<5;dx++){
for(int dy=0;dy<3;dy++){
int r_dx=4-dx,r_dy=2-dy;
defenseEffect[BLACK][king.index()][dx*3+dy]=
defenseEffect[WHITE][r_king.index()][r_dx*3+r_dy];
}
}
}
}
void osl::progress::Effect5x3Table::init()
{
setupOnStand();
setupShortEffect();
setupBlockEffect();
setupLongEffect();
setupAttackEffect();
setupDefenseEffect();
}
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
basic_type.h
boardTable.h
centering5x3.h
osl::BoardTable::getDxForBlack
int getDxForBlack(Direction dir) const
Definition boardTable.h:40
osl::BoardTable::getDyForBlack
int getDyForBlack(Direction dir) const
Definition boardTable.h:43
osl::BoardTable::getLongDirection
Direction getLongDirection(Offset32 offset32) const
Definition boardTable.h:71
osl::EffectContent::DIRECT
static const EffectContent DIRECT()
Definition effectContent.h:24
osl::Offset32Base::index
unsigned int index() const
Definition offset32.h:40
osl::PtypeTable::getEffect
const EffectContent getEffect(PtypeO ptypeo, Square from, Square to) const
fromにいるptypeoがtoに利きを持つか?
Definition ptypeTable.h:112
osl::PtypeTable::getMoveMask
int getMoveMask(Ptype ptype) const
Definition ptypeTable.h:84
osl::Square
Definition basic_type.h:532
osl::Square::index
unsigned int index() const
Definition basic_type.h:572
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::rotate180
const Square rotate180() const
Definition basic_type.h:613
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::progress::Effect5x3Table
Effect5x3の差分計算で使うテーブル.
Definition effect5x3Table.h:36
osl::progress::Effect5x3Table::setupBlockEffect
void setupBlockEffect()
Definition effect5x3Table.cc:146
osl::progress::Effect5x3Table::attackEffect
CArray3d< unsigned int, 2, Square::SIZE, 5 *3 > attackEffect
Definition effect5x3Table.h:52
osl::progress::Effect5x3Table::longEffect
CArray2d< LongEffect4, PTYPEO_SIZE, Offset32::SIZE > longEffect
Definition effect5x3Table.h:50
osl::progress::Effect5x3Table::setupOnStand
void setupOnStand()
Definition effect5x3Table.cc:114
osl::progress::Effect5x3Table::shortEffect
CArray2d< unsigned int, PTYPEO_SIZE, Offset32::SIZE > shortEffect
Definition effect5x3Table.h:49
osl::progress::Effect5x3Table::blockEffect
CArray2d< LongEffect, 8, Offset32::SIZE > blockEffect
Definition effect5x3Table.h:51
osl::progress::Effect5x3Table::setupLongEffect
void setupLongEffect()
Definition effect5x3Table.cc:161
osl::progress::Effect5x3Table::defenseEffect
CArray3d< unsigned int, 2, Square::SIZE, 5 *3 > defenseEffect
Definition effect5x3Table.h:53
osl::progress::Effect5x3Table::init
void init()
Definition effect5x3Table.cc:236
osl::progress::Effect5x3Table::onStand
CArray< unsigned int, PTYPE_SIZE > onStand
Definition effect5x3Table.h:48
osl::progress::Effect5x3Table::StandPAWN
@ StandPAWN
Definition effect5x3Table.h:39
osl::progress::Effect5x3Table::StandSILVER
@ StandSILVER
Definition effect5x3Table.h:42
osl::progress::Effect5x3Table::StandGOLD
@ StandGOLD
Definition effect5x3Table.h:43
osl::progress::Effect5x3Table::StandROOK
@ StandROOK
Definition effect5x3Table.h:45
osl::progress::Effect5x3Table::StandLANCE
@ StandLANCE
Definition effect5x3Table.h:40
osl::progress::Effect5x3Table::StandKNIGHT
@ StandKNIGHT
Definition effect5x3Table.h:41
osl::progress::Effect5x3Table::StandBISHOP
@ StandBISHOP
Definition effect5x3Table.h:44
osl::progress::Effect5x3Table::setupShortEffect
void setupShortEffect()
Definition effect5x3Table.cc:131
osl::progress::Effect5x3Table::setupAttackEffect
void setupAttackEffect()
Definition effect5x3Table.cc:177
osl::progress::Effect5x3Table::setupDefenseEffect
void setupDefenseEffect()
Definition effect5x3Table.cc:206
_initializer
static osl::SetUpRegister _initializer([](){ osl::progress::Effect5x3_Table.init();})
effect5x3Table.h
osl::progress
Definition progress.cc:754
osl::progress::operator<<
std::ostream & operator<<(std::ostream &os, LongEffect const &longEffect)
Definition effect5x3Table.cc:106
osl::progress::LongEffect4
CArray< LongEffect, 4 > LongEffect4
長い利きの方向は高々4なので，長さ4のCArrayで表現．
Definition effect5x3Table.h:30
osl::progress::Effect5x3_Table
Effect5x3Table Effect5x3_Table
Definition effect5x3Table.cc:13
osl
Definition additionalEffect.h:6
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::PAWN
@ PAWN
Definition basic_type.h:95
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::SILVER
@ SILVER
Definition basic_type.h:98
osl::GOLD
@ GOLD
Definition basic_type.h:94
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::getPtype
Ptype getPtype(PtypeO ptypeO)
Definition basic_type.h:217
osl::dirToMask
constexpr int dirToMask(Direction dir)
Definition basic_type.h:393
osl::unpromote
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に，promote前の型を返す． promoteしていない型の時はそのまま返す
Definition basic_type.h:157
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::getOwner
Player getOwner(PtypeO ptypeO)
Definition basic_type.h:256
osl::Direction
Direction
Definition basic_type.h:310
osl::ptypeOIndex
unsigned int ptypeOIndex(PtypeO ptypeo)
Definition basic_type.h:205
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::WHITE
@ WHITE
Definition basic_type.h:10
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
osl::shortToLong
constexpr Direction shortToLong(Direction d)
引数に longDirを与えてはいけない
Definition basic_type.h:388
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
osl::newPtypeO
PtypeO newPtypeO(Player player, Ptype ptype)
Definition basic_type.h:211
oslConfig.h
ptypeTable.h
osl::Centering5x3::adjustCenter
static const Square adjustCenter(Square src)
Definition centering5x3.h:23
osl::SetUpRegister
Definition oslConfig.h:128
osl::progress::LongEffect
5x3領域への長い利きの始まりと終わり． offset - 利きの方向が0の時は利きが無いことを示す minIndex - 利きが領域に入るindex maxIndex - 利きが領域から出る手前のin...
Definition effect5x3Table.h:22
osl::progress::LongEffect::maxIndex
unsigned short maxIndex
Definition effect5x3Table.h:25
osl::progress::LongEffect::offset
Offset offset
Definition effect5x3Table.h:23
osl::progress::LongEffect::minIndex
unsigned short minIndex
Definition effect5x3Table.h:24