-
Notifications
You must be signed in to change notification settings - Fork 50
/
Player.js
101 lines (84 loc) · 3.65 KB
/
Player.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
class Player {
constructor() {
this.fitness = 0;
this.vision = []; //the input array fed into the neuralNet
this.decision = []; //the out put of the NN
this.unadjustedFitness;
this.lifespan = 0; //how long the player lived for this.fitness
this.bestScore = 0; //stores the this.score achieved used for replay
this.dead = false;
this.score = 0;
this.gen = 0;
this.genomeInputs = 5;
this.genomeOutputs = 2;
this.brain = new Genome(this.genomeInputs, this.genomeOutputs);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
show() {
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
move() {
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
update() {
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
look() {
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//gets the output of the this.brain then converts them to actions
think() {
var max = 0;
var maxIndex = 0;
//get the output of the neural network
this.decision = this.brain.feedForward(this.vision);
for (var i = 0; i < this.decision.length; i++) {
if (this.decision[i] > max) {
max = this.decision[i];
maxIndex = i;
}
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a clone of this player with the same brian
clone() {
var clone = new Player();
clone.brain = this.brain.clone();
clone.fitness = this.fitness;
clone.brain.generateNetwork();
clone.gen = this.gen;
clone.bestScore = this.score;
return clone;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//since there is some randomness in games sometimes when we want to replay the game we need to remove that randomness
//this fuction does that
cloneForReplay() {
var clone = new Player();
clone.brain = this.brain.clone();
clone.fitness = this.fitness;
clone.brain.generateNetwork();
clone.gen = this.gen;
clone.bestScore = this.score;
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
return clone;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//fot Genetic algorithm
calculateFitness() {
this.fitness = random(10);
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
crossover(parent2) {
var child = new Player();
child.brain = this.brain.crossover(parent2.brain);
child.brain.generateNetwork();
return child;
}
}