forked from rolai/chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoryHeuristic.cpp
executable file
·129 lines (107 loc) · 2.74 KB
/
HistoryHeuristic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// HistoryHeuristic.cpp: implementation of the CHistoryHeuristic class.
//历史启发
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "chess.h"
#include "HistoryHeuristic.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHistoryHeuristic::CHistoryHeuristic()
{
}
CHistoryHeuristic::~CHistoryHeuristic()
{
}
void CHistoryHeuristic::ResetHistoryTable()
{
memset(m_HistoryTable, 10, 8100*4);
}
int CHistoryHeuristic::GetHistoryScore(CHESSMOVE *move)
{
int nFrom, nTo;
nFrom = move->From.y*9+move->From.x;
nTo = move->To.y*9+move->To.x;
return m_HistoryTable[nFrom][nTo];
}
void CHistoryHeuristic::EnterHistoryScore(CHESSMOVE *move,int depth)
{
int nFrom, nTo;
nFrom = move->From.y*9+move->From.x;
nTo = move->To.y*9+move->To.x;
m_HistoryTable[nFrom][nTo] += 2<<depth;
}
//
void CHistoryHeuristic::Merge(CHESSMOVE *source, CHESSMOVE *target, int l,int m, int r)
{//从小到大排序
int i = l;
int j = m + 1;
int k = l;
while((i <= m) && (j <= r))
if (source[i].Score <= source[j].Score)
target[k++] = source[i++];
else
target[k++] = source[j++];
if(i > m)
for (int q = j; q <= r; q++)
target[k++] = source[q];
else
for(int q = i; q <= m; q++)
target[k++] = source[q];
}
void CHistoryHeuristic::Merge_A(CHESSMOVE *source, CHESSMOVE *target, int l,int m, int r)
{//从大到小排序
int i = l;
int j = m + 1;
int k = l;
while((i <= m) && (j <= r))
if (source[i].Score >= source[j].Score)
target[k++] = source[i++];
else
target[k++] = source[j++];
if(i > m)
for (int q = j; q <= r; q++)
target[k++] = source[q];
else
for(int q = i; q <= m; q++)
target[k++] = source[q];
}
void CHistoryHeuristic::MergePass(CHESSMOVE *source, CHESSMOVE *target, const int s, const int n, const BOOL direction)
{
int i = 0;
while(i <= n - 2 * s)
{
//合并大小为s的相邻二段子数组
if (direction)
Merge(source, target, i, i + s - 1, i + 2 * s - 1);
else
Merge_A(source, target, i, i + s - 1, i + 2 * s - 1);
i=i+2*s;
}
if (i + s < n) //剩余的元素个数小於2s
{
if (direction)
Merge(source, target, i, i + s - 1, n - 1);
else
Merge_A(source, target, i, i + s - 1, n - 1);
}
else
for (int j = i; j <= n - 1; j++)
target[j] = source[j];
}
void CHistoryHeuristic::MergeSort(CHESSMOVE *source, int n, BOOL direction)
{
int s = 1;
while(s < n)
{
MergePass(source, m_TargetBuff, s, n, direction);
s += s;
MergePass(m_TargetBuff, source, s, n, direction);
s += s;
}
}