-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuto.cpp
89 lines (74 loc) · 2.18 KB
/
Auto.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
#include "Auto.h"
int Auto::search(const Board &board, int depth) {
depth--;
Board boardCopy = board;
boardCopy.addRandomTile();
if (boardCopy.isGameLose()) {
return -100000000;
} else if (boardCopy.isGameWin()) {
return 0x7fffffff;
} else if (depth == 0) {
return boardCopy.evaluate();
} else {
int evalUp = 1;
int evalDown = 0;
int evalLeft = 3;
int evalRight = 2;
Board boardUp = boardCopy;
Board boardDown = boardCopy;
Board boardLeft = boardCopy;
Board boardRight = boardCopy;
if (boardUp.mergeUpHelper()) {
evalUp = search(boardUp, depth);
}
if (boardDown.mergeDownHelper()) {
evalDown = search(boardDown, depth);
}
if (boardLeft.mergeLeftHelper()) {
evalLeft = search(boardLeft, depth);
}
if (boardRight.mergeRightHelper()) {
evalRight = search(boardRight, depth);
}
if (evalUp >= evalDown && evalUp >= evalLeft && evalUp >= evalRight) {
return evalUp;
} else if (evalDown >= evalLeft && evalDown >= evalRight) {
return evalDown;
} else if (evalLeft >= evalRight) {
return evalLeft;
} else {
return evalRight;
}
}
}
direction Auto::getNextStep(const Board &board) {
int evalUp = 0;
int evalDown = 1;
int evalLeft = 2;
int evalRight = 3;
Board boardUp = board;
Board boardDown = board;
Board boardLeft = board;
Board boardRight = board;
if (boardUp.mergeUpHelper()) {
evalUp = search(boardUp, 9);
}
if (boardDown.mergeDownHelper()) {
evalDown = search(boardDown, 9);
}
if (boardLeft.mergeLeftHelper()) {
evalLeft = search(boardLeft, 9);
}
if (boardRight.mergeRightHelper()) {
evalRight = search(boardRight, 9);
}
if (evalUp >= evalDown && evalUp >= evalLeft && evalUp >= evalRight) {
return UP;
} else if (evalDown >= evalLeft && evalDown >= evalRight) {
return DOWN;
} else if (evalLeft >= evalRight) {
return LEFT;
} else {
return RIGHT;
}
}