xxxxxxxxxx
102
// Find my blog at https://codeheir.com/
// I do a lot of p5.js stuff that might interest you!
class Skier {
constructor(points) {
this.x = this.lowestPoint(points);
this.y = points[this.x];
this.points = points;
this.find = false;
this.temp = 1;
this.minTemp = 0.0001;
this.alpha = 0.99;
}
lowestPoint(points) {
let lowest = Infinity;
let x = 0;
for (let i = 0; i < width; i++) {
if (height - points[i] < lowest) {
lowest = height - points[i];
x = i;
}
}
return x;
}
draw() {
fill(10);
noStroke();
ellipse(this.x, this.y-5, 20, 20);
}
update() {
this.score = height - this.y;
if (this.targetX > this.x) {
if (this.targetX == this.x + 1) {
this.x++;
} else {
this.x+=2;
}
} else if (this.targetX < this.x) {
if (this.targetX == this.x - 1) {
this.x-=1;
} else {
this.x-=2;
}
} else {
this.findHeighestPoint();
}
this.y = this.points[this.x];
}
findHeighestPoint() {
if (!this.find) return;
if (this.temp < this.minTemp) return;
this.targetX = this.x;
for (let i = -width; i < width; i++) {
let neighbourX = this.targetX + i;
let newScore = height - this.points[neighbourX];
if (newScore > this.score) {
this.targetX = neighbourX;
} else {
const prob = Math.exp((this.score-newScore)/this.temp);
if (random(1) >= prob) {
this.targetX = neighbourX;
}
}
}
this.temp *= this.alpha;
}
simpleAlgorithm() {
if (this.points[this.x + 1] < this.y) {
this.x++;
}
if (this.points[this.x - 1] < this.y) {
this.x--;
}
}
restart() {
this.x = floor(random(width));
this.y = this.points[this.x];
}
moveLeft() {
this.x -= 5;
this.y = this.points[this.x];
}
moveRight() {
this.x += 5;
this.y = this.points[this.x];
}
}