-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamejudge.cpp
82 lines (74 loc) · 2.94 KB
/
gamejudge.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
#include "gamejudge.h"
#include <QDebug>
#include <QObject>
#include <QVector>
#include <QMessageBox>
#include <cstdlib>
#include "computerplayer.h"
#include "neuralnetwork.h"
#include "car.h"
GameJudge::GameJudge(const QVector<Car*>& cars)
{
this->cars = cars;
countDeadOfGeneration = 0;
}
void GameJudge::onWallCollision(Car* car)
{
//car->putOnStart();
qDebug() << countDeadOfGeneration << endl;
countDeadOfGeneration++;
if (countDeadOfGeneration >= cars.size()) {
QVector<NeuralNetwork> new_ais;
int distance_sum = 0;
for (auto it : cars) {
distance_sum += it->getDistanceCountPerLife();
}
int l = 0, r = (int)(((float)cars[0]->getDistanceCountPerLife() / distance_sum) * 100);
float cc = 0;
for(int i = 0; i < cars.size(); i++) {
cc += ((float)cars[i]->getDistanceCountPerLife() / distance_sum);
qDebug() <<cc << " " << ((float)cars[i]->getDistanceCountPerLife() / distance_sum) << " " << distance_sum << " " << cars[i]->getDistanceCountPerLife() << " " << l << " " << r;
l = r;
if (i + 1 >= cars.size()) break;
r += (int)(((float)cars[i + 1]->getDistanceCountPerLife() / distance_sum) * 100);
}
for (int i = 0; i < cars.size(); i++) {
int rand_1001 = rand() % 100 + 1, rand_1002 = rand() % 100 + 1;
Car* ind1 = cars[0];
Car* ind2 = cars[0];
int l = 0, r = (int)(((float)cars[0]->getDistanceCountPerLife() / distance_sum) * 100);
for(int i = 0; i < cars.size(); i++) {
// qDebug() << ((float)cars[i]->getDistanceCountPerLife() / distance_sum) << " " << distance_sum << " " << cars[i]->getDistanceCountPerLife() << " " << l << " " << r;
if(l <= rand_1001 && rand_1001 <= r) {
ind1 = cars[i];
qDebug() << "CHOSEN1 " << i;
break;
}
l = r + 1;
if (i + 1 >= cars.size()) break;
r += (int)(((float)cars[i + 1]->getDistanceCountPerLife() / distance_sum) * 100);
if (l > r) l = r;
}
l = 0; r = (int)(((float)cars[0]->getDistanceCountPerLife() / distance_sum) * 100);
for(int i = 0; i < cars.size(); i++) {
if(l <= rand_1002 && rand_1002 <= r) {
ind2 = cars[i];
qDebug() << "CHOSEN2 " << i;
break;
}
l = r + 1;
if (i + 1 >= cars.size()) break;
r += (int)(((float)cars[i + 1]->getDistanceCountPerLife() / distance_sum) * 100);
if (l > r) l = r;
}
new_ais.push_back(ind1->player->ai.merge(ind2->player->ai));
}
for (int i = 0; i < new_ais.size(); i++) {
cars[i]->player->ai = new_ais[i];
}
countDeadOfGeneration = 0;
for (auto it : cars) {
it->putOnStart();
}
}
}