-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
136 lines (106 loc) · 3.45 KB
/
game.js
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
130
131
132
133
134
135
136
var lifespan = 300;
//--------------------------------
// Game Object
//--------------------------------
function Game(width, height) {
this.width = width;
this.height = height;
this.numGeneration = 0;
this.maxFitness = 0;
this.wallLength = 200;
this.amebas = [];
this.popsize = 50;
this.time = 0;
this.food = new Food();
this.predators = []
this.n_predator = 1;
this.matingPool = [];
this.init = function () {
// Create initial Amebas
for (var i = 0; i < this.popsize; i++) {
this.amebas[i] = new Ameba();
}
// Create Predators
for (var i = 0; i < this.n_predator; i++) {
this.predators[i] = new Predator(createVector(width / 2, random(100)),
15,
this.wallLength);
}
}
this.isOutOfBound = function (pos) {
return (pos.x <= 0 || pos.y <= 0 ||
pos.x >= this.width || pos.y >= this.height)
}
this.isEatten = function (pos) {
for (var i = 0; i < this.n_predator; i++) {
if (pos.x >= this.predators[i].pos.x &&
pos.y >= this.predators[i].pos.y &&
pos.x <= this.predators[i].pos.x + this.predators[i].width &&
pos.y <= this.predators[i].pos.y + this.predators[i].height) {
return true;
}
}
return false;
}
this.resetGame = function () {
this.time = 0;
this.numGeneration++;
}
this.run = function () {
if (this.time >= lifespan) {
this.evaluate();
this.resetGame();
}
this.food.update();
this.food.show();
for (var i = 0; i < this.n_predator; i++) {
this.predators[i].update();
this.predators[i].show();
}
for (var i = 0; i < this.amebas.length; i++) {
this.amebas[i].update(this.time);
this.amebas[i].calcFitness(this.food.pos);
if (this.isOutOfBound(this.amebas[i].pos)) {
this.amebas[i].die();
}
if (this.isEatten(this.amebas[i].pos)) {
this.amebas[i].getEatten();
}
this.amebas[i].show();
}
this.time++;
}
this.evaluate = function () {
console.log('evaluating...');
// Find max
var maxFitness = -100;
for (var i = 0; i < this.amebas.length; i++) {
if (maxFitness < this.amebas[i].fitness) {
maxFitness = this.amebas[i].fitness;
}
}
this.maxFitness = maxFitness;
// normalize fitness
for (var i = 0; i < this.amebas.length; i++) {
this.amebas[i].fitness /= maxFitness;
}
// put amebas to the pool
this.matingPool = [];
for (var i = 0; i < this.amebas.length; i++) {
var n = this.amebas[i].fitness * 100;
for (var j = 0; j < n; j++) {
this.matingPool.push(this.amebas[i]);
}
}
// selection
var newGeneration = [];
for (var i = 0; i < this.amebas.length; i++) {
var parentA_DNA = random(this.matingPool).dna;
var parentB_DNA = random(this.matingPool).dna;
var childDNA = parentA_DNA.crossover(parentB_DNA);
childDNA.mutation();
newGeneration[i] = new Ameba(childDNA);
}
this.amebas = newGeneration;
}
}