-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
92 lines (78 loc) · 3.33 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
var DELAY = 10;
$(document).ready(function(){
var width = 300;
var height = 400;
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var plotContext = document.getElementById("plot").getContext("2d");
var logsElement = $("p#logs");
var statsElement = $("p#stats");
var pipeCount = 3;
var birdPopulation = new BirdPopulation(width, height, context, 100, 10);
var results = [];
var bestBirds = [];
var pipesPassedResult = [];
function plotResults(results, pipesPassedResult) {
new Chart(plotContext, {
type: 'line',
data: {
labels: results.map((value, index) => index + 1),
datasets: [{
data: results,
label: "Best Bird Points",
borderColor: "#3e95cd",
fill: false
},
{
data: pipesPassedResult,
label: "# of pipes passed",
borderColor: "#FF0000",
fill: false
}]
}
});
}
function simulateGeneration(birdPopulation, shouldDisplay) {
var environment = new Environment(width, height, context, pipeCount, birdPopulation, shouldDisplay);
environment.initialize();
var steps = 10000;
while(environment.nextStep() && --steps > 0) {}
return environment.pipesPassed;
}
function simulationAndDisplayStep(environment, remainingSteps) {
var stepResult = environment.nextStep();
var [survivors, bestBird, populationSize] = environment.birds.getStatistics();
var pipesPassed = environment.pipesPassed;
statsElement.prepend('<p>Step ' + (10000 - remainingSteps) + ' - Best bird : ' + bestBird.points + ', survivors: ' + survivors + ' / ' + populationSize + ', pipes passed: ' + pipesPassed + '</p>');
if (stepResult && remainingSteps > 0) {
//statsElement.prepend('<p>Step ' + (10000 - remainingSteps) + ' - Best bird : ' + bestBird.points + ', survivors: ' + survivors + ' / ' + populationSize + ', pipes passed: ' + pipesPassed + '</p>');
setTimeout(() => simulationAndDisplayStep(environment, remainingSteps - 1));
}
}
function simulateAndDisplayGeneration(birdPopulation) {
var environment = new Environment(width, height, context, pipeCount, birdPopulation, true);
environment.initialize();
var steps = 10000;
setTimeout(() => simulationAndDisplayStep(environment, steps));
}
function simulateGenerations(number) {
for (var index = 1; index <= number; index++) {
var pipesPassed = simulateGeneration(birdPopulation, false);
var [survivors, bestBird, populationSize] = birdPopulation.getStatistics();
logsElement.append('<p>Best bird in generation ' + index + ' : ' + bestBird.points + ', survivors: ' + survivors + ' / ' + populationSize + ', pipes passed: ' + pipesPassed + '</p>');
results.push(birdPopulation.getBestBird().points);
bestBirds.push(birdPopulation.getBestBird());
pipesPassedResult.push(pipesPassed);
birdPopulation.getNextGeneration();
}
//logsElement.append('<p>Best Birds: ' + JSON.stringify(bestBirds) + '</p>');
}
function evaluateBestBirds() {
birdPopulation.population = bestBirds;
birdPopulation.population.forEach(bird => bird.reset());
simulateAndDisplayGeneration(birdPopulation);
}
simulateGenerations(50);
plotResults(results, pipesPassedResult);
evaluateBestBirds();
});