//==========================================================
struct Pace{
int _line, _col;
enum Dir{
d_Up, d_Down, d_Left, d_Right,
}_dir;
Pace( int line, int col, Dir dir ):_line(line), _col(col), _dir(dir){};
};
//==========================================================
// A 7x7 JumpChess, with emptyblock, cardblock,
// and some blocks were prohibit to place card.
//
class JumpChess{
public:
enum State{
s_Empty = 0,
s_Card = 1,
s_Prohibit = 10,
};
//==========================================================
// Control func
//------------------------------------------------
// get all aviliable paces in a vector
std::vector< Pace >* JumpChess::getAviPaces(){
std::vector< Pace >* vPaces = new std::vector< Pace >;
for( int i = 0; i < 7; i++ ){
for( int j = 0; j < 7; j++ ){
// Find a card;
if( _arChess[i][j] != s_Card ) continue;
// Check 4 dirs:
// d_Up, d_Down, d_Left, d_Right
static int xInc[] = { 0, 0, -1, 1 };
static int yInc[] = { -1, 1, 0, 0 };
for( int dir = 0; dir < 4; dir ++ ){
// Must next to another card:
int x = j+ xInc[ dir ];
int y = i+ yInc[ dir ];
if( x < 0 || x >= 7 || y < 0 || y >=7 || _arChess[y][x] != s_Card ) continue;
// And can jump to a empty block:
x += xInc[ dir ];
y += yInc[ dir ];
if( x < 0 || x >= 7 || y < 0 || y >=7 || _arChess[y][x] != s_Empty ) continue;
// Push to vPaces
vPaces->push_back( Pace( i, j, (Pace::Dir) dir ) );
}
}
}
return vPaces;
};
// Apply a pace
void JumpChess::doPace( const Pace& p ){
int x = p._col;
int y = p._line;
int dir = p._dir;
static int xInc[] = { 0, 0, -1, 1 };
static int yInc[] = { -1, 1, 0, 0 };
assert( _arChess[y][x] == s_Card );
_arChess[y][x] = s_Empty;
x += xInc[dir];
y += yInc[dir];
assert( _arChess[y][x] == s_Card );
_arChess[y][x] = s_Empty;
x += xInc[dir];
y += yInc[dir];
assert( _arChess[y][x] == s_Empty );
_arChess[y][x] = s_Card;
};
//------------------------------------------------------
// define chess state shorthand
JumpChess::State O = JumpChess::State::s_Empty; // letter 'o' uppercase ( resembling 0 )
JumpChess::State I = JumpChess::State::s_Card; // letter 'I' ( resembling 1 )
JumpChess::State P = JumpChess::State::s_Prohibit;// letter 'P' ( well, Prohibit )
// init & end states
static JumpChess::State jcsStartInit [] =
{ P, P, I, I, I, P, P,
P, P, I, I, I, P, P,
I, I, I, I, I, I, I,
I, I, I, O, I, I, I,
I, I, I, I, I, I, I,
P, P, I, I, I, P, P,
P, P, I, I, I, P, P,};
static JumpChess::State jcsEndInit[] =
{ P, P, O, O, O, P, P,
P, P, O, O, O, P, P,
O, O, O, O, O, O, O,
O, O, O, I, O, O, O,
O, O, O, O, O, O, O,
P, P, O, O, O, P, P,
P, P, O, O, O, P, P,};
// Current jc & pace
JumpChess jcStack[ 50 ]; // current jumpchess
vector<Pace>* paceStack[ 50 ]; // current aviliable paces
memset( paceStack, 0, sizeof(paceStack) );
int iPaceStack[ 50 ]; // current index of paces
// Push the first jumpchess
int level = 0;
jcStack[0] = jcStart;
jcStack[49] = jcEnd;
paceStack[0] = jcStack[ 0 ].getAviPaces();
iPaceStack[0] = 0;
cout<<"Start:"<<endl;
int topLevel = 0; // For output only
while( level>=0 && ! jcStack[ level ].isEq( jcEnd ) ){
// First get next pacelist & paceId
vector<Pace>& paceVecNow = *(paceStack[ level ]);
int paceIdNow = iPaceStack[ level ]++;
// If no pace left, backdate
if( paceIdNow >= paceVecNow.size() ){
static bottomLevel = 50;
delete paceStack[ level ];
paceStack[ level-- ] = 0;
// Out put data
cout<<"Result: "<<endl;
cout<<jcStack[level]<<endl;
if( level >= 0 ){
for( int i = 0; i <= level; i ++ )
myLog<<jcStack[i]<<endl;
}
cout<<"All steps saved in \"log.log\""<<endl;
//=============================================================
struct Pace{
int _line, _col;
enum Dir{
d_Up, d_Down, d_Left, d_Right,
}_dir;
Pace( int line, int col, Dir dir ):_line(line), _col(col), _dir(dir){};
};
//=============================================================
// A 7x7 JumpChess, with emptyblock, cardblock,
// and some blocks were prohibit to place card.
//
class JumpChess{
public:
enum State{
s_Empty = 0,
s_Card = 1,
s_Prohibit = 10,
};
//=============================================================
// Control func
//------------------------------------------------
// get all aviliable paces in a vector
std::vector< Pace >* JumpChess::getAviPaces(){
std::vector< Pace >* vPaces = new std::vector< Pace >;
for( int i = 0; i < 7; i++ ){
for( int j = 0; j < 7; j++ ){
// Find a card;
if( _arChess[i][j] != s_Card ) continue;
// Check 4 dirs:
// d_Up, d_Down, d_Left, d_Right
static int xInc[] = { 0, 0, -1, 1 };
static int yInc[] = { -1, 1, 0, 0 };
for( int dir = 0; dir < 4; dir ++ ){
// Must next to another card:
int x = j+ xInc[ dir ];
int y = i+ yInc[ dir ];
if( x < 0 || x >= 7 || y < 0 || y >=7 || _arChess[y][x] != s_Card ) continue;
// And can jump to a empty block:
x += xInc[ dir ];
y += yInc[ dir ];
if( x < 0 || x >= 7 || y < 0 || y >=7 || _arChess[y][x] != s_Empty ) continue;
// Push to vPaces
vPaces->push_back( Pace( i, j, (Pace::Dir) dir ) );
}
}
}
return vPaces;
};
// Apply a pace
void JumpChess::doPace( const Pace& p ){
int x = p._col;
int y = p._line;
int dir = p._dir;
static int xInc[] = { 0, 0, -1, 1 };
static int yInc[] = { -1, 1, 0, 0 };
//------------------------------------------------------
// define chess state shorthand
JumpChess::State O = JumpChess::State::s_Empty; // letter 'o' uppercase ( resembling 0 )
JumpChess::State I = JumpChess::State::s_Card; // letter 'I' ( resembling 1 )
JumpChess::State P = JumpChess::State::s_Prohibit;// letter 'P' ( well, Prohibit )
// init & end states
static JumpChess::State jcsStartInit [] =
{ P, P, I, I, I, P, P,
P, P, I, I, I, P, P,
I, I, I, I, I, I, I,
I, I, I, O, I, I, I,
I, I, I, I, I, I, I,
P, P, I, I, I, P, P,
P, P, I, I, I, P, P,};
static JumpChess::State jcsEndInit[] =
{ P, P, O, O, O, P, P,
P, P, O, O, O, P, P,
O, O, O, O, O, O, O,
O, O, O, I, O, O, O,
O, O, O, O, O, O, O,
P, P, O, O, O, P, P,
P, P, O, O, O, P, P,};
// Current jc & pace
JumpChess jcStack[ 50 ]; // current jumpchess
vector<Pace>* paceStack[ 50 ]; // current aviliable paces
int iPaceStack[ 50 ]; // current index of paces
// Push the first jumpchess
int level = 0;
jcStack[0] = jcStart;
jcStack[49] = jcEnd;
paceStack[0] = jcStack[ 0 ].getAviPaces();
iPaceStack[0] = 0;
while( level>=0 && ! jcStack[ level ].isEq( jcEnd ) ){
// First get next pacelist & paceId
vector<Pace>& paceVecNow = *(paceStack[ level ]);
int paceIdNow = iPaceStack[ level ]++;
// If no pace left, backdate
if( paceIdNow >= paceVecNow.size() ){
delete paceStack[ level ];
paceStack[ level-- ] = 0;
continue;
}
// Get next pace & step in
Pace& PaceNow = paceVecNow[ paceIdNow ];
level++;
jcStack[ level ] = jcStack[ level-1 ];
jcStack[ level ].doPace( PaceNow );
// init pace list of current level
paceStack[ level ] = jcStack[ level ].getAviPaces();
iPaceStack[ level ] = 0;
}
// delete paceStack
for( int i = 0; paceStack[i]; i++ ) delete paceStack[i];
// Out put data
cout<<level<<endl;
cout<<jcStack[level]<<endl;
if( level >= 0 ){
for( int i = 0; i <= level; i ++ )
myLog<<jcStack[i]<<endl;
}