-
Notifications
You must be signed in to change notification settings - Fork 1
/
move.cpp
48 lines (39 loc) · 1.32 KB
/
move.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
//
// Created by user on 29/10/2023.
//
#include "move.h"
#include <iostream>
#define max(a, b) ((a) > (b) ? (a) : (b))
namespace move {
int bestMove(const grid& g, int depth) {
int bestScore = 0, bestDir = -1;
bestMoveHelper(g, depth, bestScore, bestDir, -1);
return bestDir;
}
inline int evaluate(const grid& g) {
return (g.getEmpty() << 16) + g.getScore();
}
namespace {
int bestMoveHelper(const grid& g, int depth, int &bestScore, int &bestDir, int move) {
if (depth == 0 || g.isBlocked()) {
return evaluate(g);
}
if (move == -1) {
// if the move is -1, we are at the root of the tree
// we will have dynamic depth based on the number of empty tiles
// the less empty tiles, the more depth
depth = max(depth * (SIZE - g.getEmpty() / SIZE) / SIZE, 4);
}
for (int dir : DIRECTIONS) {
grid g1(g);
g1.move(dir);
int score = bestMoveHelper(g1, depth - 1, bestScore, bestDir, (move == -1 ? dir : move));
if (score > bestScore) {
bestScore = score;
bestDir = move;
}
}
return bestScore;
}
}
}