Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sudn0
qtcpp_demo
提交
5ade2b7e
qtcpp_demo
项目概览
sudn0
/
qtcpp_demo
与 Fork 源项目一致
Fork自
colorEagleStdio / qtcpp_demo
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
qtcpp_demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5ade2b7e
编写于
6月 03, 2021
作者:
D
dev@dev.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化马的走位
上级
8d6b89cd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
91 addition
and
15 deletion
+91
-15
chesspi/chesspi.h
chesspi/chesspi.h
+6
-5
chesspi/chesspi_ai.cpp
chesspi/chesspi_ai.cpp
+71
-5
chesspi/chesspi_rules.cpp
chesspi/chesspi_rules.cpp
+13
-5
chesspi/main.cpp
chesspi/main.cpp
+1
-0
未找到文件。
chesspi/chesspi.h
浏览文件 @
5ade2b7e
...
@@ -20,13 +20,14 @@ struct chess_node{
...
@@ -20,13 +20,14 @@ struct chess_node{
* 帅士士相相马马车车炮炮兵兵兵兵兵 將仕仕象象馬馬車車砲砲卒卒卒卒卒
* 帅士士相相马马车车炮炮兵兵兵兵兵 將仕仕象象馬馬車車砲砲卒卒卒卒卒
*/
*/
unsigned
int
alive
=
0
;
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
};
//跳转损失
float
jump_cost
[
2
]{
0
,
0
};
//跳转损失
char
side
=
0
;
char
side
=
0
;
char
depth
=
0
;
char
depth
=
0
;
//遍历
char
killed
=
0
;
float
weight
=
1
;
};
};
...
@@ -52,7 +53,7 @@ std::string node2hash(const unsigned char cod[/*32*/], const unsigned int alive)
...
@@ -52,7 +53,7 @@ std::string node2hash(const unsigned char cod[/*32*/], const unsigned int alive)
//AI
//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
);
std
::
vector
<
chess_node
>
build_tree
(
const
chess_node
&
root
,
const
int
side
,
const
std
::
vector
<
chess_node
>
&
history
);
...
...
chesspi/chesspi_ai.cpp
浏览文件 @
5ade2b7e
...
@@ -11,13 +11,70 @@
...
@@ -11,13 +11,70 @@
#include <atomic>
#include <atomic>
#include "chesspi.h"
#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
);
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
;
return
rescost
;
}
}
...
@@ -56,8 +113,17 @@ std::vector<chess_node> build_tree(const chess_node & root, const int side,const
...
@@ -56,8 +113,17 @@ std::vector<chess_node> build_tree(const chess_node & root, const int side,const
expand_node
(
tree
[
i
],
curr_side
);
expand_node
(
tree
[
i
],
curr_side
);
const
size_t
sz
=
next_status
.
size
();
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
)
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
);
std
::
string
ha
=
node2hash
(
next_status
[
j
].
coords
,
next_status
[
j
].
alive
);
bool
needI
=
false
;
bool
needI
=
false
;
#pragma omp critical
#pragma omp critical
...
@@ -104,12 +170,12 @@ size_t judge_tree(std::vector<chess_node> & tree)
...
@@ -104,12 +170,12 @@ size_t judge_tree(std::vector<chess_node> & tree)
{
{
if
(
tree
[
i
].
side
==
0
)
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
;
tree
[
i
].
weight
=
ratio
;
}
}
else
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
;
tree
[
i
].
weight
=
ratio
;
}
}
size_t
parent
=
tree
[
i
].
parent
;
size_t
parent
=
tree
[
i
].
parent
;
...
...
chesspi/chesspi_rules.cpp
浏览文件 @
5ade2b7e
...
@@ -257,13 +257,12 @@ chess_node * node)
...
@@ -257,13 +257,12 @@ chess_node * node)
{
{
assert
(
map_coords
[
new_y
][
new_x
]);
assert
(
map_coords
[
new_y
][
new_x
]);
alive
[
map_coords
[
new_y
][
new_x
]
-
1
]
=
0
;
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
)
if
(
side
==
0
)
{
{
node
->
killed
=
0
;
for
(
int
i
=
0
;
i
<
32
;
++
i
)
for
(
int
i
=
0
;
i
<
32
;
++
i
)
{
{
node
->
coords
[
i
]
&=
0x0f
;
node
->
coords
[
i
]
&=
0x0f
;
...
@@ -273,10 +272,11 @@ chess_node * node)
...
@@ -273,10 +272,11 @@ chess_node * node)
node
->
alive
|=
(
1
<<
i
);
node
->
alive
|=
(
1
<<
i
);
node
->
alive
^=
(
!
alive
[
i
])
?
(
1
<<
i
)
:
0
;
node
->
alive
^=
(
!
alive
[
i
])
?
(
1
<<
i
)
:
0
;
node
->
killed
+=
1
-
alive
[
i
];
}
}
}
}
else
{
else
{
node
->
killed
=
0
;
for
(
int
i
=
0
;
i
<
32
;
++
i
)
for
(
int
i
=
0
;
i
<
32
;
++
i
)
{
{
const
int
od
=
i
%
16
+
(
1
-
i
/
16
)
*
16
;
const
int
od
=
i
%
16
+
(
1
-
i
/
16
)
*
16
;
...
@@ -287,10 +287,15 @@ chess_node * node)
...
@@ -287,10 +287,15 @@ chess_node * node)
node
->
alive
|=
(
1
<<
i
);
node
->
alive
|=
(
1
<<
i
);
node
->
alive
^=
(
!
alive
[
od
])
?
(
1
<<
i
)
:
0
;
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
;
return
true
;
}
}
...
@@ -300,6 +305,7 @@ const int alive[/*32*/],const int side,chess_node * node)
...
@@ -300,6 +305,7 @@ const int alive[/*32*/],const int side,chess_node * node)
//反转
//反转
if
(
side
==
0
)
if
(
side
==
0
)
{
{
node
->
killed
=
0
;
for
(
int
i
=
0
;
i
<
32
;
++
i
)
for
(
int
i
=
0
;
i
<
32
;
++
i
)
{
{
node
->
coords
[
i
]
&=
0x0f
;
node
->
coords
[
i
]
&=
0x0f
;
...
@@ -309,10 +315,11 @@ const int alive[/*32*/],const int side,chess_node * node)
...
@@ -309,10 +315,11 @@ const int alive[/*32*/],const int side,chess_node * node)
node
->
alive
|=
(
1
<<
i
);
node
->
alive
|=
(
1
<<
i
);
node
->
alive
^=
(
!
alive
[
i
])
?
(
1
<<
i
)
:
0
;
node
->
alive
^=
(
!
alive
[
i
])
?
(
1
<<
i
)
:
0
;
node
->
killed
+=
1
-
alive
[
i
];
}
}
}
}
else
{
else
{
node
->
killed
=
0
;
for
(
int
i
=
0
;
i
<
32
;
++
i
)
for
(
int
i
=
0
;
i
<
32
;
++
i
)
{
{
const
int
od
=
i
%
16
+
(
1
-
i
/
16
)
*
16
;
const
int
od
=
i
%
16
+
(
1
-
i
/
16
)
*
16
;
...
@@ -323,6 +330,7 @@ const int alive[/*32*/],const int side,chess_node * node)
...
@@ -323,6 +330,7 @@ const int alive[/*32*/],const int side,chess_node * node)
node
->
alive
|=
(
1
<<
i
);
node
->
alive
|=
(
1
<<
i
);
node
->
alive
^=
(
!
alive
[
od
])
?
(
1
<<
i
)
:
0
;
node
->
alive
^=
(
!
alive
[
od
])
?
(
1
<<
i
)
:
0
;
node
->
killed
+=
alive
[
i
];
}
}
}
}
...
...
chesspi/main.cpp
浏览文件 @
5ade2b7e
...
@@ -16,6 +16,7 @@ int main()
...
@@ -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
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
};
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
;
chess_node
root
;
if
(
!
build_node
(
coordx
,
coordy
,
alive
,
0
,
&
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录