-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaimultithread.cpp
More file actions
67 lines (55 loc) · 1.81 KB
/
aimultithread.cpp
File metadata and controls
67 lines (55 loc) · 1.81 KB
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
#include "aimultithread.h"
#include "chessboard.h"
AIMultiThread::AIMultiThread(QObject *parent)
: QObject{parent}
{}
AIMultiThread::AIMultiThread(ChessBoard b, Team t) {
this->board = b;
this->team = t;
this->future_checking_timer = new QTimer();
}
AIMultiThread::~AIMultiThread() {
delete future_checking_timer;
}
void AIMultiThread::start() {
this->children = board.gen_filtered_children_boards(team);
num_finished_futures = 0;
for(int i = 0; i < children.size(); i++) {
futures.push_back(QtConcurrent::run([=]{
return children[i].alpha_beta(team_inverse(team));
}));
}
this->connect(this->future_checking_timer, &QTimer::timeout, this, &AIMultiThread::check_futures);
future_checking_timer->start(200);
}
void AIMultiThread::check_futures() {
bool is_finished = true;
int current_finished_futures = futures.size();
for(int i = 0; i < futures.size(); i++) {
if(!futures[i].isFinished()) {
is_finished = false;
current_finished_futures--;
}
}
if(current_finished_futures != num_finished_futures) {
num_finished_futures = current_finished_futures;
emit think_updated(num_finished_futures / (float) futures.size());
}
if(!is_finished) return;
for(int i = 0; i < futures.size(); i++) {
this->scores.push_back(futures[i].result());
}
emit finished();
}
std::optional<ChessBoard> AIMultiThread::get_best() {
if(children.size() != scores.size() || scores.size() == 0) return std::nullopt;
ChessBoard best_board = children[0];
int best_score = scores[0];
for(int i = 0; i < children.size(); i++) {
if(best_score > scores[i]) {
best_board = children[i];
best_score = scores[i];
}
}
return best_board;
}