-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cpp
130 lines (99 loc) · 2.86 KB
/
Enemy.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
//
// Created by td on 4/9/18.
//
#include <iostream>
#include "Enemy.h"
float Enemy::getFitness() {
return this->fitness;
}
void Enemy::generateFitness(Player *opponent) {
int fitness = 0;
float startHealth = this->getHealth();
float oppStartHealth = opponent->getHealth();
double dmgMulti=1;
if (this->getType() > opponent->getType()){
dmgMulti+=0.2;
}
else if (this->getType() < opponent->getType()){
dmgMulti-=0.2;
}
this->fitness = (startHealth - (opponent->getDamage()- this->getDefense()*0.2) - (oppStartHealth - this->getDamage()*dmgMulti));
//std::cout << "MY FITNESS IS" << this->fitness << std::endl;
//turn one
}
Enemy *Enemy::crossOver(Enemy *otherParent) {
//return Enemy(0);
std::vector<int>momGenes = this->getChromosome().getGenes();
std::vector<int>dadGenes = otherParent->getChromosome().getGenes();
std::vector<int>newGenes;
Enemy *child;
for (int i = 0; i < this->getChromosome().getGenes().size(); i++){
if (i % 2==0){
newGenes.emplace_back(momGenes[i]);
}
else{
newGenes.emplace_back(dadGenes[i]);
}
}
return new Enemy(new Chromosome(newGenes));
}
Enemy::Enemy(Chromosome *chrom) {
this->chromosome = *chrom;
this->damage = chrom->getGenes()[0];
this->defense = chrom->getGenes()[1];
this->health = chrom->getGenes()[2];
this->type = chrom->getGenes()[3];
// for (auto genes: )
}
int Enemy::getDamage() {
return this->damage;
}
int Enemy::getHealth() {
return this->health;
}
int Enemy::getDefense() {
return this->defense;
}
int Enemy::getType() {
return this->type;
}
Chromosome Enemy::getChromosome() {
return this->chromosome;
}
void Enemy::mutation() {
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_int_distribution<int> distribution(0,12);
int choice = distribution(generator);
std::vector<int> newGenes = this->chromosome.getGenes();
switch(choice){
case 0:
this->damage = this->damage * 1.1;
newGenes[0] = newGenes[0]*1.1;
break;
case 1:
this->defense = this->defense * 1.1;
newGenes[1] = newGenes[1]*1.1;
break;
case 2:
this->health = this->health * 1.1;
newGenes[2] = newGenes[2]*1.1;
break;
case 3:
this->damage = this->damage * 0.9;
newGenes[0] = newGenes[0]*0.9;
break;
case 4:
this->defense = this->defense * 0.9;
newGenes[1] = newGenes[1]*0.9;
break;
case 5:
this->health = this->health * 0.9;
newGenes[2] = newGenes[2]*0.9;
break;
default:;
}
Chromosome *c;
c = new Chromosome(newGenes);
this->chromosome = *c;
}