【发布时间】:2018-08-06 08:48:18
【问题描述】:
我正在为遗传算法编写这段代码,但是当我运行这段代码时,我在绘制函数之后不断收到意外的 EOF 错误。
SyntaxError: 预期 eof 但找到 }
我不太擅长 javascript,所以我不知道我是否犯了愚蠢的错误或什么。在我改变某些东西之前它似乎工作正常?我不知道,但它不再起作用了,也许我只是没有看到它......
var population;
var lifespan = 200;
var lifeP;
var count = 0;
function setup() {
createCanvas(400,300);
rocket = new Rocket();
population = new Population();
lifeP = createP();
}
function draw() {
background(0);
population.run();
lifeP.html(count);
count++;
}
function Population() {
this.rockets = [];
this.popsize = 25;
for(var i = 0; i < this.popsize; i++){
this.rockets[i] = new Rocket();
}
this.run = function() {
for(var i = 0; i < this.popsize; i++){
this.rockets[i].update();
this.rockets[i].show();
}
}
}
function DNA(){
this.genes = [];
for (var i = 0; i < lifespan; i++){
this.genes[i] = p5.Vector.random2D();
this.genes[i].setMag(0.1);
}
}
function Rocket(){
this.pos = createVector(width/2, height);
this.vel = createVector();
this.acc = createVector();
this.dna = new DNA();;
this.applyForce = function(force){
this.acc.add(force);
}
this.update = function(){
this.applyForce(this.dna.genes[count]);
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
this.show = function(){
push();
noStroke();
fill(255, 150);
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
rectMode(CENTER);
rect(0, 0, 25, 5);
pop();
}
}
【问题讨论】:
标签: javascript syntax-error genetic-algorithm eof