提交 5ade2b7e 编写于 作者: D dev@dev.com

优化马的走位

上级 8d6b89cd
......@@ -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<chess_node> build_tree(const chess_node & root, const int side,const std::vector<chess_node> & history);
......
......@@ -11,13 +11,70 @@
#include <atomic>
#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<chess_node> 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<sz && !ppz;++j)
// {
// if (next_status[j].jump_cost[0]==table_cost[0] || next_status[j].jump_cost[1]==table_cost[0])
// ppz = true;
// }
for (size_t j=0;j<sz;++j)
{
if (ppz && next_status[j].jump_cost[0]<table_cost[0] && next_status[j].jump_cost[1]<table_cost[0] )
continue;
std::string ha = node2hash(next_status[j].coords,next_status[j].alive);
bool needI = false;
#pragma omp critical
......@@ -104,12 +170,12 @@ size_t judge_tree(std::vector<chess_node> & 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;
......
......@@ -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<<i);
node->alive ^= (!alive[i])? (1<<i):0;
node->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<<i);
node->alive ^= (!alive[od])? (1<<i):0;
node->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<<i);
node->alive ^= (!alive[i])? (1<<i):0;
node->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<<i);
node->alive ^= (!alive[od])? (1<<i):0;
node->killed += alive[i];
}
}
......
......@@ -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))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册