diff --git a/chesspi/chesspi.h b/chesspi/chesspi.h index b891152e5e7778f375825e1d36edfec5270c205d..6a558f1ad11f0fa9837d7eb563cbfc534f681e07 100644 --- a/chesspi/chesspi.h +++ b/chesspi/chesspi.h @@ -20,13 +20,14 @@ struct chess_node{ * 帅士士相相马马车车炮炮兵兵兵兵兵 將仕仕象象馬馬車車砲砲卒卒卒卒卒 */ unsigned int alive = 0; - size_t parent = 0; - size_t leaves = 0; + //遍历 + float weight = 1; + unsigned int parent = 0; + unsigned int leaves = 0; float jump_cost[2]{0,0};//跳转损失 char side = 0; char depth = 0; - //遍历 - float weight = 1; + char killed = 0; }; @@ -52,7 +53,7 @@ std::string node2hash(const unsigned char cod[/*32*/], const unsigned int alive) //AI //------------------------------------------- -unsigned int calc_cost(const int coordx[/*32*/], const int coordy[/*32*/],const int idx); +float calc_cost(const int coordx[/*32*/], const int coordy[/*32*/],const int alive[/*32*/],const int killed,const int idx); std::vector build_tree(const chess_node & root, const int side,const std::vector & history); diff --git a/chesspi/chesspi_ai.cpp b/chesspi/chesspi_ai.cpp index 6df2b1f2bd30a84226c579a0d7113646b808e91c..a4685a1721e1f19e6428d04d009f629493cda91c 100644 --- a/chesspi/chesspi_ai.cpp +++ b/chesspi/chesspi_ai.cpp @@ -11,13 +11,70 @@ #include #include "chesspi.h" -unsigned int calc_cost(const int coordx[/*32*/], const int coordy[/*32*/],const int idx) +static const unsigned int table_cost[16] = {1000000,150,150,150,150,150,150,500,500,150,150,100,100,100,100,100}; +float calc_cost(const int coordx[/*32*/], const int coordy[/*32*/],const int alive[/*32*/],const int killed,const int idx) { - unsigned int cost[16] = {100000,150,150,150,150,100,100,100,100,500,500,10,100,1000,100,10}; + + // * 帅士士相相马马车车炮炮兵兵兵兵兵 將仕仕象象馬馬車車砲砲卒卒卒卒卒 assert(idx >= 16); - unsigned int rescost = cost[idx%16]; + unsigned int rescost = table_cost[idx%16]; + //位置加权 + switch (idx % 16) { + //相士双全时击杀价值高 + case 1: + case 2: + case 3: + case 4: + if (alive[(idx-1)/2*2+1] || alive [(idx-1)/2*2+2]) + rescost *= 2; + break; + //马战线挺进,以及后期击杀价值高 + case 5: + case 6: + if (idx<16 ) + rescost *= 1+(coordy[idx]/3.0); + else + rescost *= 1+((11-coordy[idx])/3.0); + rescost *= 1 + killed / 4.0; + break; + //车战线击杀高 + case 7: + case 8: + if (idx<16 ) + rescost *= 1+(coordy[idx]/3.0); + else + rescost *= 1+((11-coordy[idx])/3.0); + break; + //炮前期击杀高 + case 9: + case 10: + if (idx<16 ) + rescost *= 1+(coordy[idx]/3.0); + else + rescost *= 1+((11-coordy[idx])/3.0); + rescost *= 1 + (32 - killed) / 4.0; + break; + //卒过河击杀高 + case 11: + case 12: + case 13: + case 14: + case 15: + if (idx<16 ) + rescost *= coordy[idx]>5?4:1; + else + rescost *= coordy[idx]<6?4:1; + //当头卒价值高 + if (coordx[idx]==5 && (coordy[idx]==4 || coordy[idx]==7)) + rescost *=10; + break; + default: + break; + } + + return rescost; } @@ -56,8 +113,17 @@ std::vector build_tree(const chess_node & root, const int side,const expand_node(tree[i],curr_side); const size_t sz = next_status.size(); + //有无结束 + bool ppz = false; +// for (size_t j=0;j & tree) { if (tree[i].side==0) { - float ratio = sqrt((tree[i].jump_cost[1]+1) / (tree[i].jump_cost[0]+1)/ (tree[i].jump_cost[0]+1)); + float ratio = sqrt((tree[i].jump_cost[1]+1) / (tree[i].jump_cost[0]+1)/ sqrt(tree[i].jump_cost[0]+1)); tree[i].weight = ratio; } else { - float ratio = sqrt((tree[i].jump_cost[0]+1) / (tree[i].jump_cost[1]+1)/ (tree[i].jump_cost[1]+1)); + float ratio = sqrt((tree[i].jump_cost[0]+1) / (tree[i].jump_cost[1]+1)/ sqrt(tree[i].jump_cost[1]+1)); tree[i].weight = ratio; } size_t parent = tree[i].parent; diff --git a/chesspi/chesspi_rules.cpp b/chesspi/chesspi_rules.cpp index efd2782bcb24576a3afe6fdfd9523d8e39526f9e..d179b9de26e3619574da00d2f5005f5a827399fc 100644 --- a/chesspi/chesspi_rules.cpp +++ b/chesspi/chesspi_rules.cpp @@ -257,13 +257,12 @@ chess_node * node) { assert(map_coords[new_y][new_x]); alive[map_coords[new_y][new_x]-1] = 0; - //计算吃子代价 - node->jump_cost[1-side] = calc_cost(coordx,coordy,map_coords[new_y][new_x]-1); } //反转 if (side==0) { + node->killed = 0; for (int i=0;i<32;++i) { node->coords[i] &=0x0f; @@ -273,10 +272,11 @@ chess_node * node) node->alive |= (1<alive ^= (!alive[i])? (1<killed += 1-alive[i]; } } else { + node->killed = 0; for (int i=0;i<32;++i) { const int od = i%16 + (1-i/16)*16; @@ -287,10 +287,15 @@ chess_node * node) node->alive |= (1<alive ^= (!alive[od])? (1<killed += 1-alive[i]; } } + if (map_coords[new_y][new_x]>16) + { + node->jump_cost[1-side] = calc_cost(coordx,coordy,alive,node->killed,map_coords[new_y][new_x]-1); + } + return true; } @@ -300,6 +305,7 @@ const int alive[/*32*/],const int side,chess_node * node) //反转 if (side==0) { + node->killed =0; for (int i=0;i<32;++i) { node->coords[i] &=0x0f; @@ -309,10 +315,11 @@ const int alive[/*32*/],const int side,chess_node * node) node->alive |= (1<alive ^= (!alive[i])? (1<killed += 1-alive[i]; } } else { + node->killed =0; for (int i=0;i<32;++i) { const int od = i%16 + (1-i/16)*16; @@ -323,6 +330,7 @@ const int alive[/*32*/],const int side,chess_node * node) node->alive |= (1<alive ^= (!alive[od])? (1<killed += alive[i]; } } diff --git a/chesspi/main.cpp b/chesspi/main.cpp index 2f8ee8e7676d6c0f9892d034aededc27ef115d30..d5ab51211ddf0b0f153380e8eb85dc4bed085991 100644 --- a/chesspi/main.cpp +++ b/chesspi/main.cpp @@ -16,6 +16,7 @@ int main() int coordy[32]{1,1,1,1,1,1,1,1,1,3,3,4,4,4,4,4,10,10,10,10,10,10,10,10,10,8,8,7,7,7,7,7}; int alive[32]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; + printf ("Size of Node = %d Bytes.\n",sizeof(chess_node)); chess_node root; if (!build_node(coordx,coordy,alive,0,&root))