-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.cpp
129 lines (112 loc) · 3.99 KB
/
thread.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
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
#include "thread.h"
Worker::Worker(atr2var* avtemp, int argctemp, std::string argvtemp[], arena* parent, rgraph** rgraphstemp, cgraph* cyclegtemp, window* atr2wtemp) {
av = avtemp;
argc = argctemp;
argv = argvtemp;
atr2a = parent;
rgraphs = rgraphstemp;
cycleg = cyclegtemp;
atr2w = atr2wtemp;
graphix = true;
}
Worker::Worker(atr2var* avtemp, int argctemp, std::string argvtemp[]) {
av = avtemp;
argc = argctemp;
argv = argvtemp;
graphix = false;
}
void Worker::doWork() {
QString result;
atr2 *atr2p;
if (graphix) {
atr2p = new atr2(av, atr2a, rgraphs, cycleg, atr2w);
} else {
atr2p = new atr2(av);
}
atr2p->init(argc, argv);
int i, j, k, l, n, w;
if(av->matches > 0) {
for (i = 0; i < av->matches; i++) {
atr2p->bout();
}
}
if(!av->graphix) {
std::cout << std::endl;
}
if(av->quit) {
exit(0);
}
if(av->matches > 1) {
std::cout << std::endl << std::endl;
//graph_mode(false);
//textcolor(15);
std::cout << "Bout complete! (" << av->matches << " av.matches)" << std::endl;
k = 0;
w = 0;
for (i = 0; i < av->num_robots; i++) {
if (av->robot[i].wins == w) {
k++;
}
if (av->robot[i].wins > w) {
k = 1;
n = i;
w = av->robot[i].wins;
}
}
std::cout << "Robot Wins Matches Kills Deaths Shots" << std::endl;
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
for (i = 0; i < av->num_robots; i++) {
//textcolor(robot_color(i));
std::cout << atr2func::addfront(atr2func::cstr(i+1), 2) << " - " << atr2func::addrear(av->robot[i].fn, 8)
<< atr2func::addfront(atr2func::cstr(av->robot[i].wins), 7) << atr2func::addfront(atr2func::cstr(av->robot[i].trials), 8)
<< atr2func::addfront(atr2func::cstr(av->robot[i].kills), 8) << atr2func::addfront(atr2func::cstr(av->robot[i].deaths), 8)
<< atr2func::addfront(atr2func::cstr(av->robot[i].shots_fired), 9) << std::endl;
}
//textcolor(15);
std::cout << std::endl;
if (k == 1) {
std::cout << "Robot #" << n + 1 << " (" << av->robot[n].fn << ") wins the bout! (score: " << w << "/" << av->matches << ")" << std::endl;
} else {
std::cout << "There is no clear victor!" << std::endl;
}
std::cout << std::endl;
} else if (av->graphix) {
//graph_mode(false);
//show_statistics();
}
if (av->report) {
atr2p->write_report();
}
atr2p->shutdown();
emit resultReady(result);
}
Controller::Controller(atr2var* avtemp, int argctemp, std::string argvtemp[], arena* parent, rgraph** rgraphstemp, cgraph* cyclegtemp, window *atr2wtemp) {
av = avtemp;
argc = argctemp;
argv = argvtemp;
atr2a = parent;
rgraphs = rgraphstemp;
cycleg = cyclegtemp;
atr2w = atr2wtemp;
Worker *worker = new Worker(av, argc, argv, atr2a, rgraphs, cycleg, atr2w);
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::doWork);
//connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
Controller::Controller(atr2var* avtemp, int argctemp, std::string argvtemp[]) {
av = avtemp;
argc = argctemp;
argv = argvtemp;
Worker *worker = new Worker(av, argc, argv);
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::doWork);
//connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
Controller::~Controller() {
workerThread.quit();
workerThread.wait();
}