-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path标头.h
635 lines (548 loc) · 10.9 KB
/
标头.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#pragma once
#include<iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include "dataBase.h"
#define OK 999
#define NO 888 //平局也算作失败
#define SIZE 3
#define WINLEN 3
#define Null 2 //棋盘该点未落子
#define x 1
#define o 0
using namespace std;
class CDL {
public://结构体
typedef struct OpenTree {
int index;//索引码
int deep;//深度
double winProb;//胜率
int childNum;//孩子数量
int tag;//该层属于x还是属于o
int dataBaseIndex;
//结构体指针
OpenTree* child;//孩子
OpenTree* father;//父亲
OpenTree* brother;//兄弟
}OT;
public://结构体变量
OT* oth;//open头
double a;//学习率
private:
int** broad;
void init();
private://成员函数
void initHead();
OT* insOTnode(int index, double winProb, OT* father);//插入open树节点
void structNode(OT* father);
private://工具函数
int transToIndex(int** maxtri);//将一个棋盘矩阵转换为index
void transToMaxtri(int** maxtri, int index);//将一个index转换为棋盘矩阵
int toPower(int number, int Binary);//获得幂数 powr:幂
int toNum(int power, int Binary);//获得数
int ifWin(int I, int J);//判断输赢
void showBroad(int I, int J);//显示输出
void SumNode(OT* node);//统计总计节点数
int sumNode;
string transToStr(double num,int ifIndex);
double transToDouble(string str);
public:
CDL();
public:
OT* nowNode;//当前所处棋盘状态节点
void fallSon(int* I, int* J);//落子,交出决策
void upDate(int I, int J, int round);
public:
void backWard(int winORdraw);//一方胜利反向迭代
//void structDataBase(OT* node);//构建数据库节点
//void updateDataBase();//更新数据库节点信息
};
void CDL::initHead()
{
oth = (OT*)malloc(sizeof(OT));
oth->deep = 0;
oth->winProb = -1;
oth->child = NULL;
oth->brother = NULL;
oth->father = NULL;
oth->childNum = 0;
oth->tag = -1;
oth->dataBaseIndex= 0;
init();//初始化棋盘
oth->index = transToIndex(broad);//将初始状态放入open树中
}
/*
函数:插入一个节点到Open树中
传参:节点所需参数
*/
CDL::OT* CDL::insOTnode(int index, double winProb, OT* father)
{
OT* newNode = (OT*)malloc(sizeof(OT));
newNode->index = index;
newNode->deep = father->deep + 1;
newNode->winProb = winProb;
newNode->father = father;
newNode->child = NULL;
newNode->childNum = 0;
newNode->dataBaseIndex = father->dataBaseIndex * 10 + father->childNum+1;
if (father->tag == -1)
newNode->tag = 1;
else if (father->tag == 0)
newNode->tag = 1;
else
newNode->tag = 0;
//如果父节点没有孩子节点,则该新节点为孩子节点;若有,则为孩子节点的兄弟
if (father->child == NULL)
{
father->child = newNode;
newNode->brother = NULL;
}
else
{
newNode->brother = father->child->brother;
father->child->brother = newNode;
}
father->childNum++;
return newNode;
}
int ddd = 0;
/*
参数:递归构造open树节点
传参:当前待拓展节点指针
*/
void CDL::structNode(OT* father)
{
//获取当前落子I J,判断输赢
if (father!= oth)
{
int I, J;
int deffIndex = father->father->index - father->index;//index差值
//得出这个差值第几位
int power = toPower(deffIndex, 3);
I = power / SIZE;
J = power % SIZE;
if (ifWin(I, J) == OK)
return;
}
//遍历棋盘,找到可桌子点
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
{
//恢复棋盘至父节点棋盘样子
transToMaxtri(broad, father->index);
//如果该点可以着子
if (broad[i][j] == Null)
{
//判断着x或者o,并着子
if (father->deep % 2 == 0)
broad[i][j] = x;
else
broad[i][j] = o;
int index = transToIndex(broad);
//递归
OT* nowNode = insOTnode(index, 50.01, father);
structNode(nowNode);
}
}
}
void CDL::SumNode(OT* node)
{
sumNode += node->childNum;
OT* ptr = node->child;
while (ptr != NULL)
{
SumNode(ptr);
ptr = ptr->brother;
}
}
CDL::CDL()
{
srand((int)time(0));
//初始化数据库
/*initDataBase();
string qury = "use xo";
MySqlQury(qury);
qury = " CREATE TABLE tb_base(dbIndex INT, dbWP DOUBLE)ENGINE = InnoDB DEFAULT CHARSET = utf8; ";
MySqlQury(qury);*/
initHead();
structNode(oth);
nowNode = oth;//初始化当前节点为根
a = 0.1;
cout << "初始化完毕!" << endl;
}
/*
已查
函数:根据当前状态节点选择决策节点
返回值: i和j,回合round
*/
void CDL::fallSon(int* I, int* J)
{
//求出概率总和
double sum_double = 0;
OT* ptr = nowNode->child;
for (int i = 0; i <nowNode->childNum; i++)
{
sum_double += ptr->winProb;
ptr = ptr->brother;
}
double test = sum_double;
double num=0;
if (sum_double >=1)
{
//生成随机数
int sum_int = (int)sum_double;
if (sum_int != 0)
sum_int = rand() % sum_int;//整数部分
sum_double = (int)rand() % 100;//小数部分
sum_double = sum_double / 100;
num = (double)sum_int + sum_double;
}
else
{
int dec = sum_double * 100;
if (dec != 0)
{
dec = rand() % dec;
sum_double = dec;
sum_double = sum_double / 100;
}
else
sum_double = 0;
}
//找到属于哪个区间
ptr = nowNode->child;
for (int j = 0; j < nowNode->childNum; j++)
{
if (num <= ptr->winProb)
{
//找出i,和j
int deffIndex = nowNode->index - ptr->index;//index差值
//得出这个差值第几位
int power = toPower(deffIndex, 3);
*I = power / SIZE;
*J = power % SIZE;
//更新当前状态
nowNode = ptr;
return;
}
num = num - ptr->winProb;
ptr = ptr->brother;
}
cout << "fallSon函数错误!" ;
}
void CDL::backWard(int winORdraw)
{
if (winORdraw == OK)
{
nowNode->winProb += a * (100 - nowNode->winProb);
nowNode->father->winProb += a * (0 - nowNode->father->winProb);
}//给定初始化奖励(惩罚)
else
{
//先手平局给30,后手平局给50
if (nowNode->tag == x)
{
nowNode->winProb += a * (30 - nowNode->winProb);
nowNode->father->winProb += a * (50 - nowNode->father->winProb);
}
else
{
nowNode->winProb += a * (50 - nowNode->winProb);
nowNode->father->winProb += a * (30 - nowNode->father->winProb);
}
}
OT* ptr = nowNode;
while (ptr->deep > 2)
{
//隔层传递
ptr->father->father->winProb += a * (ptr->winProb - ptr->father->father->winProb);
ptr = ptr->father->father;
}
ptr = nowNode->father;
while (ptr->deep > 2)
{
//隔层传递
ptr->father->father->winProb += a * (ptr->winProb - ptr->father->father->winProb);
ptr = ptr->father->father;
}
//初始化当前所处节点状态
nowNode = oth;//初始化当前节点为根
}
/*
已查
函数:更新当前所处棋盘状态节点
参数:i和j,谁的回合round
*/
void CDL::upDate(int I, int J, int round)
{
int newIndex;
if (round == 1)
newIndex = nowNode->index + (x - Null) * toNum(I * SIZE + J, 3);
else
newIndex = nowNode->index + (o - Null) * toNum(I * SIZE + J, 3);
//开始寻找该index的child
OT* ptr = nowNode->child;
while (ptr)
{
//如果找到该节点,则更新当前节点状态为该节点
if (ptr->index == newIndex)
{
nowNode = ptr;
cout << "1" << endl;
return;
}
ptr = ptr->brother;
}
if (ptr == NULL)
{
cout << "upDate函数出错。" << endl;
while (1);
}
}
//初始化棋盘
void CDL::init()
{
broad = (int**)malloc(sizeof(int*) * SIZE);
for (int i = 0; i < SIZE; i++)
broad[i] = (int*)malloc(sizeof(int) * SIZE);
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
broad[i][j] = Null;
}
/*
*已查
函数:将一个棋盘矩阵转换为index
传参:棋盘矩阵
返回:index值
*/
int CDL::transToIndex(int** maxtri)
{
int index = 0;
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
{
index += maxtri[i][j] * toNum(i * SIZE + j, 3);
}
return index;
}
/*
函数:将一个index转换为棋盘矩阵
传参:1.棋盘矩阵容器 2.index值
*/
void CDL::transToMaxtri(int** maxtri, int index)
{
for (int i = SIZE - 1; i > -1; i--)
for (int j = SIZE - 1; j > -1; j--)
{
int t = toNum(i * SIZE + j, 3);
maxtri[i][j] = index / t;
index = index - t * maxtri[i][j];
}
}
/*
*已查
函数:将一个数按照进制获得幂
传参:1.数 2.进制
返回:幂
*/
int CDL::toPower(int number, int Binary)
{
int t = 1;
const int maxPower = 10;//最大幂
for (int i = 0; i < maxPower; i++)
{
if (number / t < Binary)
return i;
t = t * Binary;
}
return NO;
}
/*
*已查
函数:
传参:1.幂数 2.进制
返回:数
*/
int CDL::toNum(int power, int Binary)
{
int t = 1;
for (int i = 0; i < power; i++)
{
t = t * Binary;
}
return t;
}
/*
函数:判断是否赢棋
传参:当前落子点
返回:WIN或者FAIL
*/
int CDL::ifWin(int I, int J)
{
int nCh = broad[I][J];
int i, j, nowLen; //nowLen 当前已连结长度
//上
nowLen = 0;
i = I - (WINLEN - 1);
j = J;
while (1)
{
if (nowLen == WINLEN)
return OK;
if (i > (I + (WINLEN - 1)))
break;
if (i<0 || i>(SIZE - 1) || broad[i][j] != nCh)
{
nowLen = 0;
i++;
continue; //如果当前i或者j超出边界或者中断,则跳过
}
if (broad[i][j] == nCh)
nowLen++;
i++;
}
//左
nowLen = 0;
i = I;
j = J - (WINLEN - 1);
while (1)
{
if (nowLen == WINLEN)
return OK;
if (j > (J + (WINLEN - 1)))
break;
if (j<0 || j>(SIZE - 1) || broad[i][j] != nCh)
{
nowLen = 0;
j++;
continue; //如果当前i或者j超出边界或者中断,则跳过
}
if (broad[i][j] == nCh)
nowLen++;
j++;
}
//左上
nowLen = 0;
i = I - (WINLEN - 1);
j = J - (WINLEN - 1);
while (1)
{
if (nowLen == WINLEN)
return OK;
if (i > (I + (WINLEN - 1)) || j > (J + (WINLEN - 1)))
break;
if (j<0 || j>(SIZE - 1) || i<0 || i>(SIZE - 1) || broad[i][j] != nCh)
{
nowLen = 0;
i++;
j++;
continue; //如果当前i或者j超出边界或者中断,则跳过
}
if (broad[i][j] == nCh)
nowLen++;
i++;
j++;
}
//左下
nowLen = 0;
i = I + (WINLEN - 1);
j = J - (WINLEN - 1);
while (1)
{
if (nowLen == WINLEN)
return OK;
if (i < (I - (WINLEN - 1)) || j >(J + (WINLEN - 1)))
break;
if (j<0 || j>(SIZE - 1) || i < 0 || i >(SIZE - 1) || broad[i][j] != nCh)
{
nowLen = 0;
i--;
j++;
continue; //如果当前i或者j超出边界或者中断,则跳过
}
if (broad[i][j] == nCh)
nowLen++;
i--;
j++;
}
return NO;
}
void CDL::showBroad(int I, int J)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (broad[i][j] == 1)
{
if (i == I && j == J)
cout << 'X' << " ";
else
cout << 'x' << " ";
}
else if (broad[i][j] == 0)
{
if (i == I && j == J)
cout << 'O' << " ";
else
cout << 'o' << " ";
}
else
{
cout << " " << " ";
}
}
cout << endl;
}
}
string CDL::transToStr(double num,int ifIndex)
{
string str;
int num_int = num;
while (1)
{
str += num_int - (num_int / 10 * 10) + 48;
num_int = num_int / 10;
if (num_int == 0)
break;
}
char tC;
for (int i = 0; i < str.size() / 2; i++)
{
tC = str[i];
str[i] = str[str.size() - i - 1];
str[str.size() - i - 1] = tC;
}
if (!ifIndex)
{
str += ".";
num = num - (int)num;
//保留两位
num_int = num * 100;
str += num_int / 10 + 48;
str += num_int % 10 + 48;
}
return str;
}
double CDL::transToDouble(string str)
{
int num_int = 0;
for (int i = 0; str[i] != '.'; i++)
{
num_int *= 10;
num_int += str[i] - 48;
}
double num_double;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == '.')
{
num_double = (str[i + 1] - 48) * 10;
num_double += str[i + 2] - 48;
num_double /= 100;
num_double += num_int;
return num_double;
}
}
}