Commit 6b53aeed by WangChenxi

DeBug1

parent ebbfb394
...@@ -8,14 +8,14 @@ ...@@ -8,14 +8,14 @@
*/ */
//EnCode for move(action):: //EnCode for move(action)::
/* /*
from_id 64 stone 17
to_id 64
captured 10 captured 10
promoted 8 promoted 8
stone 17 from_id 64
Attention: all this coding part should be >= 0 to_id 64
Attention: all this coding part should be >= 0 for a normal action
*/ */
//action = from_id * 64^4 + to_id * 64^3 + captured * 64^2 + promoted * 64^1 + stone * 64^0 //action = stone * 64^4 + captured * 64^3 + promoted * 64^2 + from_id * 64^1 + to_id * 64^0 //For many games, we use to_id as its action. To keep the same, to_id is the last bit.
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
......
...@@ -97,7 +97,7 @@ ChessCoordId StrToId(const std::string &str) { ...@@ -97,7 +97,7 @@ ChessCoordId StrToId(const std::string &str) {
return CoordToId(x, y); return CoordToId(x, y);
} }
void ActionToId(const int &action, const ChessCoordId &from_id, const ChessCoordId &to_id, ChessStoneColor &captured_stone, ChessStoneColor &promoted_stone, ChessStoneColor &stone){ void ActionToId(const int &action, const ChessStone &stone, const ChessStone &captured_stone, const ChessStone &promoted_stone, const ChessCoordId &from_id, const ChessCoordId &to_id){
int code[5]; int code[5];
int i; int i;
if(action == COORD_RESIGN){ if(action == COORD_RESIGN){
...@@ -114,15 +114,15 @@ void ActionToId(const int &action, const ChessCoordId &from_id, const ChessCoord ...@@ -114,15 +114,15 @@ void ActionToId(const int &action, const ChessCoordId &from_id, const ChessCoord
code[i] = action % 64; code[i] = action % 64;
action = action / 64; action = action / 64;
} }
from_id = code[4]; stone = code[4];
to_id = code[3]; captured_stone = code[3];
captured_stone = code[2]; promoted_stone = code[2];
promoted_stone = code[1]; from_id = code[1];
stone = code[0]; to_id = code[0];
} }
} }
void IdToAction(const ChessCoordId &from_id, const ChessCoordId &to_id, ChessStoneColor &captured_stone, ChessStoneColor &promoted_stone, ChessStoneColor &stone, const int &action){ void IdToAction(const ChessStone &stone, const ChessStone &captured_stone, const ChessStone &promoted_stone, const ChessCoordId &from_id, const ChessCoordId &to_id, const int &action){
int code[5]; int code[5];
int i, j; int i, j;
for(i = 0; i < 5; i++){ for(i = 0; i < 5; i++){
...@@ -138,7 +138,7 @@ void IdToAction(const ChessCoordId &from_id, const ChessCoordId &to_id, ChessSto ...@@ -138,7 +138,7 @@ void IdToAction(const ChessCoordId &from_id, const ChessCoordId &to_id, ChessSto
code[i] *= 64; code[i] *= 64;
} }
} }
action = from_id * code[4] + to_id * code[3] + captured_stone * code[2] + promoted_stone * code[1] + stone * code[0]; action = stone * code[4] + captured_stone * code[3] + promoted_stone * code[2] + from_id * code[1] + to_id * code[0];
} }
} }
......
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
// Return code of functions should be "int" // Return code of functions should be "int"
typedef uint8_t ChessStoneColor; // Stone color typedef uint8_t ChessStoneColor; // Stone color
typedef int16_t ChessCoordId; // Stone IDs or coordinates typedef int16_t ChessCoordId; // Stone IDs or coordinates
typedef int16_t ChessSize; // Counts of visit times, used blocks, .. or other count typedef int32_t ChessSize; // Counts of visit times, used blocks, .. or other count //For our action space, int16 may be not enough
//typedef int16_t ChessSize; // Counts of visit times, used blocks, .. or other count
namespace ChessComm { namespace ChessComm {
...@@ -45,7 +46,7 @@ const ChessStoneColor BLACK_ROOK = 13; ...@@ -45,7 +46,7 @@ const ChessStoneColor BLACK_ROOK = 13;
const ChessStoneColor BLACK_QUEEN = 14; const ChessStoneColor BLACK_QUEEN = 14;
const ChessStoneColor BLACK_KING = 15; const ChessStoneColor BLACK_KING = 15;
const ChessStoneColor COLOR_UNKNOWN = -1; const ChessStoneColor COLOR_UNKNOWN = -1;
const char *const COLOR_STRING[] = { "Empty", "Black", "White", "Wall", "White Pawn", "White Knight", "White Bishop", "White Rook", "White Queen", "White King", "Black Pawn", "Black Knight", "Black Bishop", "Black Rook", "Black Queen", "Black King" }; const char *const COLOR_STRING[] = { "Empty", "White", "Black", "Wall", "White Pawn", "White Knight", "White Bishop", "White Rook", "White Queen", "White King", "Black Pawn", "Black Knight", "Black Bishop", "Black Rook", "Black Queen", "Black King" };
const ChessCoordId N = -8; const ChessCoordId N = -8;
const ChessCoordId S = 8; const ChessCoordId S = 8;
...@@ -106,9 +107,9 @@ extern std::string IdToStr(const ChessCoordId id); ...@@ -106,9 +107,9 @@ extern std::string IdToStr(const ChessCoordId id);
extern ChessCoordId StrToId(const std::string &str); extern ChessCoordId StrToId(const std::string &str);
extern void ActionToId(const int &action, const ChessCoordId &from_id, const ChessCoordId &to_id, ChessStone &captured_stone, ChessStone &promoted_stone, ChessStone &stone); extern void ActionToId(const int &action, const ChessStone &stone, const ChessStone &captured_stone, const ChessStone &promoted_stone, const ChessCoordId &from_id, const ChessCoordId &to_id);
extern void IdToAction(const ChessCoordId &from_id, const ChessCoordId &to_id, ChessStone &captured_stone, ChessStone &promoted_stone, ChessStone &stone, const int &action); extern void IdToAction(const ChessStone &stone, const ChessStone &captured_stone, const ChessStone &promoted_stone, const ChessCoordId &from_id, const ChessCoordId &to_id, const int &action);
extern void CreateGlobalVariables(); extern void CreateGlobalVariables();
......
...@@ -91,7 +91,7 @@ int ChessState::Move(int action) { ...@@ -91,7 +91,7 @@ int ChessState::Move(int action) {
zobrist_hash_value_ ^= g_zobrist_player_hash_weight[Opponent()]; zobrist_hash_value_ ^= g_zobrist_player_hash_weight[Opponent()];
zobrist_hash_value_ ^= g_zobrist_board_hash_weight[Self()][action]; zobrist_hash_value_ ^= g_zobrist_board_hash_weight[Self()][action];
} }
ChessFunction::ActionToId(action, from_id, to_id, captured_stone, promoted_stone, piece); ChessFunction::ActionToId(action, piece, captured_stone, promoted_stone, from_id, to_id);
++move_count_[action]; ++move_count_[action];
switch (captured_stone) switch (captured_stone)
...@@ -291,9 +291,9 @@ bool ChessEnv::IsKingInCheck() const{ ...@@ -291,9 +291,9 @@ bool ChessEnv::IsKingInCheck() const{
} }
} }
return isSquareThreatened(kingid); return IsSquareThreatened(kingid);
} // end of isKingInCheck } // end of IsKingInCheck
void ChessEnv::Find_Legal_Moves() const{ void ChessEnv::Find_Legal_Moves() const{
action_list_.clear(); action_list_.clear();
...@@ -313,29 +313,29 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -313,29 +313,29 @@ void ChessEnv::Find_Legal_Moves() const{
if (i < 2 * ChessComm::BORDER_SIZE) // Check for promotion if (i < 2 * ChessComm::BORDER_SIZE) // Check for promotion
{ {
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::WHITE_QUEEN, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::WHITE_QUEEN, i, j, action);
//CMove move(piece, i, j, EM, WQ); //CMove move(piece, i, j, EM, WQ);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::WHITE_ROOK, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::WHITE_ROOK, i, j, action);
//CMove move(piece, i, j, EM, WR); //CMove move(piece, i, j, EM, WR);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::WHITE_BISHOP, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::WHITE_BISHOP, i, j, action);
//CMove move(piece, i, j, EM, WB); //CMove move(piece, i, j, EM, WB);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::WHITE_KNIGHT, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::WHITE_KNIGHT, i, j, action);
//CMove move(piece, i, j, EM, WN); //CMove move(piece, i, j, EM, WN);
action_list_.push_back(action); action_list_.push_back(action);
} }
} }
else // regular pawn move else // regular pawn move
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, EM); //CMove move(piece, i, j, EM);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -346,7 +346,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -346,7 +346,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (i < 40) // Only from second rank //if (i < 40) // Only from second rank
if(i >= 6 * ChessComm::BORDER_SIZE) if(i >= 6 * ChessComm::BORDER_SIZE)
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, EM); //CMove move(piece, i, j, EM);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -362,29 +362,29 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -362,29 +362,29 @@ void ChessEnv::Find_Legal_Moves() const{
if (i < 2 * ChessComm::BORDER_SIZE) // Check for promotion if (i < 2 * ChessComm::BORDER_SIZE) // Check for promotion
{ {
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::WHITE_QUEEN, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::WHITE_QUEEN, i, j, action);
//CMove move(piece, i, j, m_board[j], WQ); //CMove move(piece, i, j, m_board[j], WQ);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::WHITE_ROOK, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::WHITE_ROOK, i, j, action);
//CMove move(piece, i, j, m_board[j], WR); //CMove move(piece, i, j, m_board[j], WR);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::WHITE_BISHOP, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::WHITE_BISHOP, i, j, action);
//CMove move(piece, i, j, m_board[j], WB); //CMove move(piece, i, j, m_board[j], WB);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::WHITE_KNIGHT, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::WHITE_KNIGHT, i, j, action);
//CMove move(piece, i, j, m_board[j], WN); //CMove move(piece, i, j, m_board[j], WN);
action_list_.push_back(action); action_list_.push_back(action);
} }
} }
else else
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -399,29 +399,29 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -399,29 +399,29 @@ void ChessEnv::Find_Legal_Moves() const{
if(i < 2 * ChessComm::BORDER_SIZE) if(i < 2 * ChessComm::BORDER_SIZE)
{ {
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::WHITE_QUEEN, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::WHITE_QUEEN, i, j, action);
//CMove move(piece, i, j, m_board[j], WQ); //CMove move(piece, i, j, m_board[j], WQ);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::WHITE_ROOK, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::WHITE_ROOK, i, j, action);
//CMove move(piece, i, j, m_board[j], WR); //CMove move(piece, i, j, m_board[j], WR);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::WHITE_BISHOP, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::WHITE_BISHOP, i, j, action);
//CMove move(piece, i, j, m_board[j], WB); //CMove move(piece, i, j, m_board[j], WB);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::WHITE_KNIGHT, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::WHITE_KNIGHT, i, j, action);
//CMove move(piece, i, j, m_board[j], WN); //CMove move(piece, i, j, m_board[j], WN);
action_list_.push_back(action); action_list_.push_back(action);
} }
} }
else else
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -442,7 +442,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -442,7 +442,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] <= 0) //if (m_board[j] <= 0)
if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -470,7 +470,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -470,7 +470,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] <= 0) //if (m_board[j] <= 0)
if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
//if (m_board[j] < 0) //if (m_board[j] < 0)
...@@ -505,7 +505,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -505,7 +505,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] <= 0) //if (m_board[j] <= 0)
if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
//if (m_board[j] < 0) //if (m_board[j] < 0)
...@@ -540,7 +540,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -540,7 +540,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] <= 0) //if (m_board[j] <= 0)
if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
//if (m_board[j] < 0) //if (m_board[j] < 0)
...@@ -571,7 +571,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -571,7 +571,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] <= 0) //if (m_board[j] <= 0)
if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::BLACK || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -608,29 +608,29 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -608,29 +608,29 @@ void ChessEnv::Find_Legal_Moves() const{
if(i >= 6 * ChessComm::BORDER_SIZE) if(i >= 6 * ChessComm::BORDER_SIZE)
{ {
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::BLACK_QUEEN, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::BLACK_QUEEN, i, j, action);
//CMove move(piece, i, j, EM, BQ); //CMove move(piece, i, j, EM, BQ);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::BLACK_ROOK, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::BLACK_ROOK, i, j, action);
//CMove move(piece, i, j, EM, BR); //CMove move(piece, i, j, EM, BR);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::BLACK_BISHOP, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::BLACK_BISHOP, i, j, action);
//CMove move(piece, i, j, EM, BB); //CMove move(piece, i, j, EM, BB);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::BLACK_KNIGHT, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::BLACK_KNIGHT, i, j, action);
//CMove move(piece, i, j, EM, BN); //CMove move(piece, i, j, EM, BN);
action_list_.push_back(action); action_list_.push_back(action);
} }
} }
else // Regular pawn move else // Regular pawn move
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, EM); //CMove move(piece, i, j, EM);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -643,7 +643,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -643,7 +643,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (i > 80) // Only from seventh rank //if (i > 80) // Only from seventh rank
if (i < 2 * ChessComm::BORDER_SIZE) // Only from seventh rank if (i < 2 * ChessComm::BORDER_SIZE) // Only from seventh rank
{ {
ChessFunction::IdToAction(i, j, ChessComm::EMPTY, ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, ChessComm::EMPTY, ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, EM); //CMove move(piece, i, j, EM);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -659,29 +659,29 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -659,29 +659,29 @@ void ChessEnv::Find_Legal_Moves() const{
if (i >= 6 * ChessComm::BORDER_SIZE) // Check for promotion if (i >= 6 * ChessComm::BORDER_SIZE) // Check for promotion
{ {
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::BLACK_QUEEN, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::BLACK_QUEEN, i, j, action);
//CMove move(piece, i, j, m_board[j], BQ); //CMove move(piece, i, j, m_board[j], BQ);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::BLACK_ROOK, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::BLACK_ROOK, i, j, action);
//CMove move(piece, i, j, m_board[j], BR); //CMove move(piece, i, j, m_board[j], BR);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::BLACK_BISHOP, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::BLACK_BISHOP, i, j, action);
//CMove move(piece, i, j, m_board[j], BB); //CMove move(piece, i, j, m_board[j], BB);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::BLACK_KNIGHT, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::BLACK_KNIGHT, i, j, action);
//CMove move(piece, i, j, m_board[j], BN); //CMove move(piece, i, j, m_board[j], BN);
action_list_.push_back(action); action_list_.push_back(action);
} }
} }
else else
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -696,29 +696,29 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -696,29 +696,29 @@ void ChessEnv::Find_Legal_Moves() const{
if(i >= 6 * ChessComm::BORDER_SIZE) if(i >= 6 * ChessComm::BORDER_SIZE)
{ {
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::BLACK_QUEEN, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::BLACK_QUEEN, i, j, action);
//CMove move(piece, i, j, m_board[j], BQ); //CMove move(piece, i, j, m_board[j], BQ);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::BLACK_ROOK, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::BLACK_ROOK, i, j, action);
//CMove move(piece, i, j, m_board[j], BR); //CMove move(piece, i, j, m_board[j], BR);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::BLACK_BISHOP, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::BLACK_BISHOP, i, j, action);
//CMove move(piece, i, j, m_board[j], BB); //CMove move(piece, i, j, m_board[j], BB);
action_list_.push_back(action); action_list_.push_back(action);
} }
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::BLACK_KNIGHT, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::BLACK_KNIGHT, i, j, action);
//CMove move(piece, i, j, m_board[j], BN); //CMove move(piece, i, j, m_board[j], BN);
action_list_.push_back(action); action_list_.push_back(action);
} }
} }
else else
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -739,7 +739,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -739,7 +739,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] >= 0) //if (m_board[j] >= 0)
if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -767,7 +767,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -767,7 +767,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] >= 0) //if (m_board[j] >= 0)
if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
//if (m_board[j] > 0) //if (m_board[j] > 0)
...@@ -802,7 +802,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -802,7 +802,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] >= 0) //if (m_board[j] >= 0)
if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm:EMPTY) if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm:EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
//if (m_board[j] > 0) //if (m_board[j] > 0)
...@@ -837,7 +837,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -837,7 +837,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] >= 0) //if (m_board[j] >= 0)
if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
//if (m_board[j] > 0) //if (m_board[j] > 0)
...@@ -868,7 +868,7 @@ void ChessEnv::Find_Legal_Moves() const{ ...@@ -868,7 +868,7 @@ void ChessEnv::Find_Legal_Moves() const{
//if (m_board[j] >= 0) //if (m_board[j] >= 0)
if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm::EMPTY) if(current_player_ == ChessComm::WHITE || board_state_[j] == ChessComm::EMPTY)
{ {
ChessFunction::IdToAction(i, j, board_state_[j], ChessComm::EMPTY, piece, action); ChessFunction::IdToAction(piece, board_state_[j], ChessComm::EMPTY, i, j, action);
//CMove move(piece, i, j, m_board[j]); //CMove move(piece, i, j, m_board[j]);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -892,10 +892,10 @@ bool ChessEnv::IsMoveValid(int &action) const{ ...@@ -892,10 +892,10 @@ bool ChessEnv::IsMoveValid(int &action) const{
ChessStoneColor captured_stone, promoted_stone, piece; ChessStoneColor captured_stone, promoted_stone, piece;
for (unsigned int i=0; i < action_list_.size(); ++i){ for (unsigned int i=0; i < action_list_.size(); ++i){
if (action_list_[i] == action){ if (action_list_[i] == action){
ChessFunction::ActionToId(action, from_id, to_id, captured_stone, promoted_stone, piece); ChessFunction::ActionToId(action, piece, captured_stone, promoted_stone, from_id, to_id);
piece = board_state_[from_id]; piece = board_state_[from_id];
captured_stone = board_state_[to_id]; captured_stone = board_state_[to_id];
ChessFunction::IdToAction(from_id, to_id, captured_stone, promoted_stone, piece, action); ChessFunction::IdToAction(piece, captured_stone, promoted_stone, from_id, to_id, action);
return true; return true;
} }
} }
...@@ -906,7 +906,7 @@ bool ChessEnv::IsMoveValid(int &action) const{ ...@@ -906,7 +906,7 @@ bool ChessEnv::IsMoveValid(int &action) const{
int ChessEnv::CalcResult() const { int ChessEnv::CalcResult() const {
ChessStoneColor king; ChessStoneColor king;
CSquare kingid = ChessComm::COORD_UNSET; ChessCoordId kingid = ChessComm::COORD_UNSET;
if (current_player_ == ChessComm::WHITE){ if (current_player_ == ChessComm::WHITE){
king = ChessComm::WHITE_KING; king = ChessComm::WHITE_KING;
...@@ -988,7 +988,7 @@ void ChessEnv::GetInputDim(vector<int>& input_dim) { ...@@ -988,7 +988,7 @@ void ChessEnv::GetInputDim(vector<int>& input_dim) {
void ChessEnv::GetLegalAction(vector<int>& action) { void ChessEnv::GetLegalAction(vector<int>& action) {
action.clear(); action.clear();
for (unsigned int i=0; i < action_list_.size(); ++i){ for (unsigned int i = 0; i < action_list_.size(); ++i){
action.push_back(action_list_[i]); action.push_back(action_list_[i]);
} }
} }
...@@ -996,7 +996,7 @@ void ChessEnv::GetLegalAction(vector<int>& action) { ...@@ -996,7 +996,7 @@ void ChessEnv::GetLegalAction(vector<int>& action) {
std::string ChessEnv::action2str(int action){ std::string ChessEnv::action2str(int action){
ChessCoordId from_id, to_id; ChessCoordId from_id, to_id;
ChessStoneColor captured_stone, promoted_stone, piece; ChessStoneColor captured_stone, promoted_stone, piece;
ChessFunction::ActionToId(action, from_id, to_id, captured_stone, promoted_stone, piece); ChessFunction::ActionToId(action, piece, captured_stone, promoted_stone, from_id, to_id);
char buffer[12]; char buffer[12];
buffer[0] = 'f'; buffer[0] = 'f';
buffer[1] = 'r'; buffer[1] = 'r';
......
...@@ -57,7 +57,7 @@ protected: ...@@ -57,7 +57,7 @@ protected:
ChessStoneColor GetWinner() const; ChessStoneColor GetWinner() const;
inline void HandOff() { current_player_ = Opponent(); } inline void HandOff() { current_player_ = Opponent(); }
inline ChessStoneColor Opponent(const ChessStoneColor color = ChessComm::COLOR_UNKNOWN) const { inline ChessStoneColor Opponent(const ChessStoneColor color = ChessComm::COLOR_UNKNOWN) const {
return ChessComm::BLACK + ChessComm::WHITE return ChessComm::WHITE + ChessComm::BLACK
- (ChessComm::COLOR_UNKNOWN != color ? color : current_player_); - (ChessComm::COLOR_UNKNOWN != color ? color : current_player_);
} }
inline ChessStoneColor Self() const { return current_player_; } inline ChessStoneColor Self() const { return current_player_; }
...@@ -67,7 +67,7 @@ protected: ...@@ -67,7 +67,7 @@ protected:
void GetSensibleMove(); void GetSensibleMove();
bool IsSquareThreatened(ChessCoordId& id) const; bool IsSquareThreatened(ChessCoordId& id) const;
bool IsKingInCheck() const bool IsKingInCheck() const;
void Find_Legal_Moves() const; void Find_Legal_Moves() const;
bool IsMoveValid(int& action) const; bool IsMoveValid(int& action) const;
......
No preview for this file type
...@@ -6,14 +6,14 @@ ...@@ -6,14 +6,14 @@
Experiment environment; Experiment environment;
Environment in Agents' mind Environment in Agents' mind
*/ */
//EnCode for move(action):: //EnCode for chinesechess move(action)::
/* /*
stone 19
from_id 90 from_id 90
to_id 90 to_id 90
stone 19 Attention: all this coding part should be >= 0 for a normal action
Attention: all this coding part should be >= 0
*/ */
//action = stone * 90^2 + from_id * 90^1 + to_id * 90^0 //action = stone * 90^2 + from_id * 90^1 + to_id * 90^0 //For many games, we use to_id as its action. To keep the same, to_id is the last bit.
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
......
...@@ -21,20 +21,21 @@ ...@@ -21,20 +21,21 @@
// Return code of functions should be "int" // Return code of functions should be "int"
typedef uint8_t ChineseChessStoneColor; // Stone color typedef uint8_t ChineseChessStoneColor; // Stone color
typedef int16_t ChineseChessCoordId; // Stone IDs or coordinates typedef int16_t ChineseChessCoordId; // Stone IDs or coordinates
typedef int16_t ChineseChessSize; // Counts of visit times, used blocks, .. or other count typedef int32_t ChineseChessSize; // Counts of visit times, used blocks, .. or other count //For our action space, int16 may be not enough
//typedef int16_t ChineseChessSize; // Counts of visit times, used blocks, .. or other count
namespace ChineseChessComm { namespace ChineseChessComm {
const ChineseChessCoordId BORDER_SIZE_LENGTH = 9; // sxk_modify const ChineseChessCoordId BORDER_SIZE_LENGTH = 9; // sxk_modify //y
const ChineseChessCoordId BORDER_SIZE_HEIGHT = 10; const ChineseChessCoordId BORDER_SIZE_HEIGHT = 10; //x
const ChineseChessCoordId CHINESECHESSBOARD_SIZE = BORDER_SIZE_LENGTH * BORDER_SIZE_HEIGHT; const ChineseChessCoordId CHINESECHESSBOARD_SIZE = BORDER_SIZE_LENGTH * BORDER_SIZE_HEIGHT;
const ChineseChessCoordId COORD_UNSET = -2; const ChineseChessCoordId COORD_UNSET = -2;
//const ChineseChessCoordId COORD_PASS = -1; //const ChineseChessCoordId COORD_PASS = -1;
const ChineseChessCoordId COORD_RESIGN = -3; const ChineseChessCoordId COORD_RESIGN = -3;
const ChineseChessStoneColor EMPTY = 0; const ChineseChessStoneColor EMPTY = 0;
const ChineseChessStoneColor RED = 1; const ChineseChessStoneColor RED = 1; //First player
const ChineseChessStoneColor BLACK = 2; const ChineseChessStoneColor BLACK = 2; //Second player
const ChineseChessStoneColor WALL = 3; const ChineseChessStoneColor WALL = 3;
const ChineseChessStoneColor RED_PAWN = 4; const ChineseChessStoneColor RED_PAWN = 4;
const ChineseChessStoneColor RED_CANNON = 5; const ChineseChessStoneColor RED_CANNON = 5;
......
...@@ -22,6 +22,8 @@ ChineseChessEnv::ChineseChessEnv() { ...@@ -22,6 +22,8 @@ ChineseChessEnv::ChineseChessEnv() {
current_player_ = RED; current_player_ = RED;
last_action_ = COORD_UNSET; last_action_ = COORD_UNSET;
is_resign_ = false; is_resign_ = false;
is_redking_incheck_ = false;
is_blackking_incheck_ = false;
state_ = 0; state_ = 0;
board_hash_states_.clear(); board_hash_states_.clear();
zobrist_hash_value_ = 0; zobrist_hash_value_ = 0;
...@@ -106,6 +108,7 @@ int ChineseChessState::Move(int action) { ...@@ -106,6 +108,7 @@ int ChineseChessState::Move(int action) {
void ChineseChessEnv::Find_Legal_Moves() const{ void ChineseChessEnv::Find_Legal_Moves() const{
action_list_.clear(); action_list_.clear();
is_redking_incheck_ = is_blackking_incheck_ = false;
int action; int action;
ChineseChessCoordId i, j, k, l; ChineseChessCoordId i, j, k, l;
ChineseChessCoordId i_x, i_y, j_x, j_y , k_x, k_y, l_x, l_y; ChineseChessCoordId i_x, i_y, j_x, j_y , k_x, k_y, l_x, l_y;
...@@ -157,9 +160,15 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -157,9 +160,15 @@ void ChineseChessEnv::Find_Legal_Moves() const{
} }
} }
if(!is_mystone && !is_king_facetoface){ if(!is_mystone && !is_king_facetoface){
if(piece == ChineseChessComm::RED_KING && current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(piece == ChineseChessComm::BLACK_KING && current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action);
}
}
} }
} }
break; break;
...@@ -187,9 +196,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -187,9 +196,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{
} }
} }
if(!is_chariotpass && !is_mystone){ if(!is_chariotpass && !is_mystone){
if(piece == ChineseChessComm::RED_CHARIOT && current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(piece == ChineseChessComm::BLACK_CHARIOT && current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action);
}
if(piece == ChineseChessComm::RED_CHARIOT && board_state_[j] == ChineseChessComm::BLACK_KING){
is_blackking_incheck_ = true;
}
if(piece == ChineseChessComm::BLACK_CHARIOT && board_state_[j] == ChineseChessComm::RED_KING){
is_redking_incheck_ = true;
}
}
} }
break; break;
...@@ -214,12 +235,23 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -214,12 +235,23 @@ void ChineseChessEnv::Find_Legal_Moves() const{
is_horsepass = true; is_horsepass = true;
} }
} }
if (abs(i_y - j_y) == 2 && abs(i_x - j_x) == 1 || if (abs(i_y - j_y) == 2 && abs(i_x - j_x) == 1 || (abs(i_y - j_y) == 1 && abs(i_x - j_x) == 2)) {
(abs(i_y - j_y) == 1 && abs(i_x - j_x) == 2)) {
if(!is_mystone && !is_horsepass){ if(!is_mystone && !is_horsepass){
if(piece == ChineseChessComm::RED_HORSE && current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(piece == ChineseChessComm::BLACK_HORSE && current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action);
}
if(piece == ChineseChessComm::RED_HORSE && board_state_[j] == ChineseChessComm::BLACK_KING){
is_blackking_incheck_ = true;
}
if(piece == ChineseChessComm::BLACK_HORSE && board_state_[j] == ChineseChessComm::RED_KING){
is_redking_incheck_ = true;
}
}
} }
} }
break; break;
...@@ -240,9 +272,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -240,9 +272,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{
} }
} }
if ((count == 1 && (board_state_[j] != ChineseChessComm::EMPTY && !is_mystone)) || (count == 0 && board_state_[j] == ChineseChessComm::EMPTY)){ if ((count == 1 && (board_state_[j] != ChineseChessComm::EMPTY && !is_mystone)) || (count == 0 && board_state_[j] == ChineseChessComm::EMPTY)){
if(piece == ChineseChessComm::RED_CANNON && current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action);
}
if(piece == ChineseChessComm::BLACK_CANNON && current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(piece == ChineseChessComm::RED_CANNON && board_state_[j] == ChineseChessComm::BLACK_KING){
is_blackking_incheck_ = true;
}
if(piece == ChineseChessComm::BLACK_CANNON && board_state_[j] == ChineseChessComm::RED_KING){
is_redking_incheck_ = true;
}
}
}else{ }else{
for (l_x = i_x + 1; l_x < j_x; l_x++){ for (l_x = i_x + 1; l_x < j_x; l_x++){
l_y = i_y; l_y = i_y;
...@@ -252,9 +296,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -252,9 +296,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{
} }
} }
if ((count == 1 && (board_state_[j] != ChineseChessComm::EMPTY && !is_mystone)) || (count == 0 && board_state_[j] == ChineseChessComm::EMPTY)){ if ((count == 1 && (board_state_[j] != ChineseChessComm::EMPTY && !is_mystone)) || (count == 0 && board_state_[j] == ChineseChessComm::EMPTY)){
if(piece == ChineseChessComm::RED_CANNON && current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(piece == ChineseChessComm::BLACK_CANNON && current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action);
}
if(piece == ChineseChessComm::RED_CANNON && board_state_[j] == ChineseChessComm::BLACK_KING){
is_blackking_incheck_ = true;
}
if(piece == ChineseChessComm::BLACK_CANNON && board_state_[j] == ChineseChessComm::RED_KING){
is_redking_incheck_ = true;
}
}
} }
}else if (abs(i_x - j_x) == 0 && abs(i_y - j_y) != 0){ //横向 }else if (abs(i_x - j_x) == 0 && abs(i_y - j_y) != 0){ //横向
if (i_y - j_y > 0){ if (i_y - j_y > 0){
...@@ -266,9 +322,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -266,9 +322,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{
} }
} }
if ((count == 1 && (board_state_[j] != ChineseChessComm::EMPTY && !is_mystone)) || (count == 0 && board_state_[j] == ChineseChessComm::EMPTY)){ if ((count == 1 && (board_state_[j] != ChineseChessComm::EMPTY && !is_mystone)) || (count == 0 && board_state_[j] == ChineseChessComm::EMPTY)){
if(piece == ChineseChessComm::RED_CANNON && current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(piece == ChineseChessComm::BLACK_CANNON && current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action);
}
if(piece == ChineseChessComm::RED_CANNON && board_state_[j] == ChineseChessComm::BLACK_KING){
is_blackking_incheck_ = true;
}
if(piece == ChineseChessComm::BLACK_CANNON && board_state_[j] == ChineseChessComm::RED_KING){
is_redking_incheck_ = true;
}
}
}else{ }else{
for (l_y = i_y + 1; l_y < j_y; l_y++){ for (l_y = i_y + 1; l_y < j_y; l_y++){
l_x = i_x; l_x = i_x;
...@@ -278,9 +346,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -278,9 +346,21 @@ void ChineseChessEnv::Find_Legal_Moves() const{
} }
} }
if ((count == 1 && (board_state_[j] != ChineseChessComm::EMPTY && !is_mystone)) || (count == 0 && board_state_[j] == ChineseChessComm::EMPTY)){ if ((count == 1 && (board_state_[j] != ChineseChessComm::EMPTY && !is_mystone)) || (count == 0 && board_state_[j] == ChineseChessComm::EMPTY)){
if(piece == ChineseChessComm::RED_CANNON && current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(piece == ChineseChessComm::BLACK_CANNON && current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action);
}
if(piece == ChineseChessComm::RED_CANNON && board_state_[j] == ChineseChessComm::BLACK_KING){
is_blackking_incheck_ = true;
}
if(piece == ChineseChessComm::BLACK_CANNON && board_state_[j] == ChineseChessComm::RED_KING){
is_redking_incheck_ = true;
}
}
} }
} }
} }
...@@ -296,12 +376,12 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -296,12 +376,12 @@ void ChineseChessEnv::Find_Legal_Moves() const{
ChineseChessFunction::CoordToId(l_x, l_y, l); ChineseChessFunction::CoordToId(l_x, l_y, l);
if(!is_mystone){ if(!is_mystone){
if (board_state_[l] == ChineseChessComm::EMPTY){ //中間沒卡到東西 if (board_state_[l] == ChineseChessComm::EMPTY){ //中間沒卡到東西
if (current_player_ == ChineseChessComm::RED){ if (current_player_ == ChineseChessComm::RED && piece == ChineseChessComm::RED_ELEPHANT){
if (abs(j_x - i_x) == 2 && abs(j_y - i_y) == 2 && j_x >= 5 && j_x <= 9){ if (abs(j_x - i_x) == 2 && abs(j_y - i_y) == 2 && j_x >= 5 && j_x <= 9){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
}else{ }if(current_player_ == ChineseChessComm::BLACK && piece == ChineseChessComm::BLACK_PAWN){
if (abs(j_x - i_x) == 2 && abs(j_y - i_y) == 2 && j_x>= 0 && j_x <= 4){ if (abs(j_x - i_x) == 2 && abs(j_y - i_y) == 2 && j_x>= 0 && j_x <= 4){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
...@@ -320,12 +400,13 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -320,12 +400,13 @@ void ChineseChessEnv::Find_Legal_Moves() const{
ChineseChessFunction::IdToCoord(j, j_x, j_y); ChineseChessFunction::IdToCoord(j, j_x, j_y);
if(!is_mystone){ if(!is_mystone){
if (j_y >= 3 && j_y <= 5) { if (j_y >= 3 && j_y <= 5) {
if (current_player_ == ChineseChessComm::RED) { if (current_player_ == ChineseChessComm::RED && piece == ChineseChessComm::RED_ADVISER) {
if (abs(j_x - i_x) == 1 && abs(j_y - i_y) == 1 && j_x >= 7 && j_x <= 9){ if (abs(j_x - i_x) == 1 && abs(j_y - i_y) == 1 && j_x >= 7 && j_x <= 9){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
}else { }
if(current_player_ == ChineseChessComm::BLACK && piece == ChineseChessComm::BLACK_ADVISER) {
if (abs(j_x - i_x) == 1 && abs(j_y - i_y) == 1 && j_x >= 0 && j_x <= 2){ if (abs(j_x - i_x) == 1 && abs(j_y - i_y) == 1 && j_x >= 0 && j_x <= 2){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
...@@ -342,28 +423,40 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -342,28 +423,40 @@ void ChineseChessEnv::Find_Legal_Moves() const{
ChineseChessFunction::IdToCoord(i, i_x, i_y); ChineseChessFunction::IdToCoord(i, i_x, i_y);
ChineseChessFunction::IdToCoord(j, j_x, j_y); ChineseChessFunction::IdToCoord(j, j_x, j_y);
if(!is_mystone){ if(!is_mystone){
if (current_player_ == ChineseChessComm::RED) {//red if (piece == ChineseChessComm::RED_PAWN) {//red
if (i_x <= 4) {//已過河 if (i_x <= 4) {//已過河
if ((((i_x - j_x) == 1 && abs(i_y - j_y) == 0) || if ((((i_x - j_x) == 1 && abs(i_y - j_y) == 0) || (abs(i_x - j_x) == 0 && abs(i_y - j_y) == 1))){
(abs(i_x - j_x) == 0 && abs(i_y - j_y) == 1))){ if(current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(board_state_[j] == ChineseChessComm::BLACK_KING){
is_blackking_incheck_ = true;
}
}
}else{//未過河 }else{//未過河
if (i_x - j_x == 1 && abs(i_y - j_y) == 0) { if (i_x - j_x == 1 && abs(i_y - j_y) == 0) {
if(current_player_ == ChineseChessComm::RED){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
} }
}else {//black }
}
if(piece == ChineseChessComm::BLACK_PAWN){//black
if (i_x >= 5) {//已過河 if (i_x >= 5) {//已過河
if ((i_x - j_x == -1 && abs(i_y - j_y) == 0) || if ((i_x - j_x == -1 && abs(i_y - j_y) == 0) || (abs(i_x - j_x) == 0 && abs(i_y - j_y) == 1)){
(abs(i_x - j_x) == 0 && abs(i_y - j_y) == 1)){ if(current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
if(board_state_[j] == ChineseChessComm::RED_KING){
is_redking_incheck_ = true;
}
}
}else{//未過河 }else{//未過河
if (i_x - j_x == -1 && abs(i_y - j_y) == 0) { if (i_x - j_x == -1 && abs(i_y - j_y) == 0) {
if(current_player_ == ChineseChessComm::BLACK){
ChineseChessFunction::IdToAction(piece, i, j, action); ChineseChessFunction::IdToAction(piece, i, j, action);
action_list_.push_back(action); action_list_.push_back(action);
} }
...@@ -371,6 +464,7 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -371,6 +464,7 @@ void ChineseChessEnv::Find_Legal_Moves() const{
} }
} }
} }
}
break; break;
default : // Invalid, wrong color, or empty default : // Invalid, wrong color, or empty
...@@ -382,6 +476,16 @@ void ChineseChessEnv::Find_Legal_Moves() const{ ...@@ -382,6 +476,16 @@ void ChineseChessEnv::Find_Legal_Moves() const{
} }
bool ChineseChessEnv::IsKingInCheck() const{
if(is_redking_incheck_ && current_player_ == ChineseChessComm::RED){
return true;
}
if(is_blackking_incheck_ && current_player_ == ChineseChessComm::BLACK){
return true;
}
return false;
} // end of IsKingInCheck
bool ChineseChessEnv::IsMoveValid(int &action) const{ bool ChineseChessEnv::IsMoveValid(int &action) const{
ChineseChessCoordId from_id, to_id; ChineseChessCoordId from_id, to_id;
ChineseChessStoneColor piece; ChineseChessStoneColor piece;
...@@ -397,7 +501,7 @@ bool ChineseChessEnv::IsMoveValid(int &action) const{ ...@@ -397,7 +501,7 @@ bool ChineseChessEnv::IsMoveValid(int &action) const{
int ChineseChessEnv::CalcResult() const { int ChineseChessEnv::CalcResult() const {
ChineseChessStoneColor king; ChineseChessStoneColor king;
CSquare kingid = ChineseChessComm::COORD_UNSET; ChineseChessCoordId kingid = ChineseChessComm::COORD_UNSET;
if (current_player_ == ChineseChessComm::RED){ if (current_player_ == ChineseChessComm::RED){
king = ChineseChessComm::RED_KING; king = ChineseChessComm::RED_KING;
...@@ -479,7 +583,7 @@ void ChineseChessEnv::GetInputDim(vector<int>& input_dim) { ...@@ -479,7 +583,7 @@ void ChineseChessEnv::GetInputDim(vector<int>& input_dim) {
void ChineseChessEnv::GetLegalAction(vector<int>& action) { void ChineseChessEnv::GetLegalAction(vector<int>& action) {
action.clear(); action.clear();
for (unsigned int i=0; i < action_list_.size(); ++i){ for (unsigned int i = 0; i < action_list_.size(); ++i){
action.push_back(action_list_[i]); action.push_back(action_list_[i]);
} }
} }
......
...@@ -64,8 +64,7 @@ protected: ...@@ -64,8 +64,7 @@ protected:
//void ChineseChessEnv::TransformCoord(ChineseChessCoordId &x, ChineseChessCoordId &y, int mode, bool reverse = false) //void ChineseChessEnv::TransformCoord(ChineseChessCoordId &x, ChineseChessCoordId &y, int mode, bool reverse = false)
void GetSensibleMove(); void GetSensibleMove();
bool IsSquareThreatened(ChineseChessCoordId& id) const; bool IsKingInCheck() const;
bool IsKingInCheck() const
void Find_Legal_Moves() const; void Find_Legal_Moves() const;
bool IsMoveValid(int& action) const; bool IsMoveValid(int& action) const;
...@@ -78,6 +77,8 @@ protected: ...@@ -78,6 +77,8 @@ protected:
int last_action_; int last_action_;
bool is_resign_; bool is_resign_;
int state_; int state_;
bool is_redking_incheck_;
bool is_blackking_incheck_;
// hash board state // hash board state
std::unordered_set<uint64_t> board_hash_states_; std::unordered_set<uint64_t> board_hash_states_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment