-
Notifications
You must be signed in to change notification settings - Fork 0
/
Searchengine.cpp
93 lines (83 loc) · 2.17 KB
/
Searchengine.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
#include "Searchengine.h"
#include "string.h"
#include "algorithm"
Searchengine::Searchengine()
{
}
Searchengine::~Searchengine()
{
}
Generator *mg;
BYTE Searchengine:: MakeMove(CHESSMOVE *move)
{
BYTE n_side;
n_side = CurPosition[move->To.x][move->To.y];
CurPosition[move->To.x][move->To.y] = CurPosition[move->From.x][move->From.y];
CurPosition[move->From.x][move->From.y] = 0;
player=3-player;
return n_side;
}
void Searchengine::UnMakeMove(CHESSMOVE *move, BYTE nChessID)//恢复棋盘
{
CurPosition[move->From.x][move->From.y] = CurPosition[move->To.x][move->To.y];
CurPosition[move->To.x][move->To.y] = nChessID;
player=3-player;
}
CHESSMOVE Searchengine:: SearchGoodMove(BYTE position[6][6],int nply,int side)
{
init();
memcpy(CurPosition,position,sizeof(CurPosition));
bestmove = nullmove;
player=side;Maxdepth=nply;
ms(-20000, 20000, nply);
return bestmove;
}
double Searchengine::ms(double alpha, double beta, int depth)
{
double score;
double best = -20000;
Generator As;
Evaluation Ev;
if (depth == 0)
{
best = -Ev.Evaluate(CurPosition,3-player);
return best;
}
int num = As.CreatePossibleMove(CurPosition,depth,player);
if (num == 0)
{
return -10000-depth;
}
/*
if(depth==Maxdepth)
{
for(int i=0;i<num;i++)
{
int type = MakeMove(CurPosition,&As.m_nMoveList[depth][i]);
As.m_nMoveList[depth][i].Score=Ev.Evaluate(CurPosition,3-player);
UnMakeMove(CurPosition,&As.m_nMoveList[depth][i],type);
}
}
std::sort( As.m_nMoveList[depth], As.m_nMoveList[depth]+num);
*/
for(int i=0;i<num;i++)
{
int type = MakeMove(&As.m_nMoveList[depth][i]);
score=-ms(-beta,-alpha,depth-1);
UnMakeMove(&As.m_nMoveList[depth][i],type);
if(score>best)
{
best=score;
if(depth==Maxdepth)
{
bestmove=As.m_nMoveList[depth][i];
bestmove.Score=best;
}
if(score>alpha)
alpha=score;
if(score>beta)
break;
}
}
return best;
}