User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 44
User prompt
implement Particle Swarm Optimisation to move the sheep without colliding with each other
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'self.sheepArray.forEach(function (otherSheep) {' Line Number: 38
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 37
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 37
User prompt
implement Particle Swarm Optimisation to move the sheep without colliding with each other
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < sheepArray.length; i++) {' Line Number: 24
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < sheepArray.length; i++) {' Line Number: 24
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < sheepArray.length; i++) {' Line Number: 24
User prompt
implement Particle Swarm Optimisation to move the sheep without colliding with each other
User prompt
implement Particle Swarm Optimisation for sheep movement
User prompt
implement Particle Swarm Optimisation for the sheep.
User prompt
Fix Bug: 'TypeError: sheep.avoidDog is not a function' in or related to this line: 'sheep.avoidDog(shepherdDog);' Line Number: 127
User prompt
Fix Bug: 'TypeError: sheep.avoidDog is not a function' in or related to this line: 'sheep.avoidDog(shepherdDog);' Line Number: 127
User prompt
Fix Bug: 'TypeError: sheep.avoidDog is not a function' in or related to this line: 'sheep.avoidDog(shepherdDog);' Line Number: 127
User prompt
Fix Bug: 'TypeError: sheep.avoidDog is not a function' in or related to this line: 'sheep.avoidDog(shepherdDog);' Line Number: 127
User prompt
Fix Bug: 'TypeError: sheep.avoidDog is not a function' in or related to this line: 'sheep.avoidDog(shepherdDog);' Line Number: 127
User prompt
realise the interaction of the sheep with each other using Particle Swarm Optimisation
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'x')' in or related to this line: 'var currentScore = Math.sqrt(Math.pow(globalBestPosition.x - self.x, 2) + Math.pow(globalBestPosition.y - self.y, 2));' Line Number: 68
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'x')' in or related to this line: 'var currentScore = Math.sqrt(Math.pow(globalBestPosition.x - self.x, 2) + Math.pow(globalBestPosition.y - self.y, 2));' Line Number: 66
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 49
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 45
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 45
User prompt
sheep should interact with each other according to the principles of Particle Swarm Optimisation
User prompt
the sheep must be 100 pixels away from the dog
/**** * Classes ****/ // Sheep class var Sheep = Container.expand(function () { var self = Container.call(this); var sheepGraphics = self.attachAsset('sheep', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 1; self.direction = Math.random() * Math.PI * 2; self.move = function (sheepArray, globalBestPosition) { var pBest = { x: self.x, y: self.y }; var pBestScore = Infinity; var localBest = self.getLocalBest(sheepArray); var r1 = Math.random(); var r2 = Math.random(); var inertiaWeight = 0.5; var cognitiveComponent = 2.0; var socialComponent = 2.0; self.velocity = { x: inertiaWeight * self.velocity.x + cognitiveComponent * r1 * (pBest.x - self.x) + socialComponent * r2 * (localBest.x - self.x), y: inertiaWeight * self.velocity.y + cognitiveComponent * r1 * (pBest.y - self.y) + socialComponent * r2 * (localBest.y - self.y) }; self.x += self.velocity.x; self.y += self.velocity.y; self.updateBest(pBest, pBestScore, globalBestPosition); }; self.getLocalBest = function (sheepArray) { var localBestPosition = { x: self.x, y: self.y }; var bestScore = Infinity; sheepArray.forEach(function (otherSheep) { if (otherSheep !== self) { var distance = Math.sqrt(Math.pow(otherSheep.x - self.x, 2) + Math.pow(otherSheep.y - self.y, 2)); if (distance < bestScore) { bestScore = distance; localBestPosition = { x: otherSheep.x, y: otherSheep.y }; } } }); return localBestPosition; }; self.updateBest = function (pBest, pBestScore, globalBestPosition) { var currentScore = Math.sqrt(Math.pow(globalBestPosition.x - self.x, 2) + Math.pow(globalBestPosition.y - self.y, 2)); if (currentScore < pBestScore) { pBest.x = self.x; pBest.y = self.y; pBestScore = currentScore; } }; self.avoidWalls = function () { if (self.x < 50 || self.x > 1998 || self.y < 50 || self.y > 2682) { self.direction += Math.PI; } }; self.avoidDog = function (dog) { if (self.intersects(dog)) { self.direction += Math.PI / 2; } }; }); // Shepherd dog class var Dog = Container.expand(function () { var self = Container.call(this); var dogGraphics = self.attachAsset('dog', { anchorX: 0.5, anchorY: 0.5 }); self.move = function (pos) { self.x = pos.x; self.y = pos.y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background to represent the sky }); /**** * Game Code ****/ // Shepherd dog asset // Sheep asset // Initialize sheep array var sheepArray = []; // Create 20 sheep and add them to the game and sheepArray for (var i = 0; i < 20; i++) { var sheep = new Sheep(); sheep.x = Math.random() * 2048; sheep.y = Math.random() * 2732; game.addChild(sheep); sheepArray.push(sheep); } // Create shepherd dog and add it to the game var shepherdDog = new Dog(); shepherdDog.x = 1024; // Start in the middle of the game area shepherdDog.y = 1366; game.addChild(shepherdDog); // Event listener for touch move to move the shepherd dog game.on('move', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.move(pos); }); // Main game tick event LK.on('tick', function () { // Move each sheep for (var i = 0; i < sheepArray.length; i++) { var sheep = sheepArray[i]; sheep.move(); sheep.avoidWalls(); sheep.avoidDog(shepherdDog); } }); // Ensure the game is touchscreen compatible game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.move(pos); }); game.on('up', function (obj) { // This can be used for future features where the dog stops moving on touch up });
===================================================================
--- original.js
+++ change.js
@@ -3,45 +3,70 @@
****/
// Sheep class
var Sheep = Container.expand(function () {
var self = Container.call(this);
- self.avoidOtherSheep = function (sheepArray) {
- var separationDistance = 120;
- sheepArray.forEach(function (otherSheep) {
- if (otherSheep !== self && self.intersects(otherSheep)) {
- var dx = self.x - otherSheep.x;
- var dy = self.y - otherSheep.y;
- var distance = Math.sqrt(dx * dx + dy * dy);
- if (distance < separationDistance) {
- var angle = Math.atan2(dy, dx);
- self.direction += angle;
- }
- }
- });
- };
var sheepGraphics = self.attachAsset('sheep', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1;
self.direction = Math.random() * Math.PI * 2;
- self.move = function () {
- self.x += Math.cos(self.direction) * self.speed;
- self.y += Math.sin(self.direction) * self.speed;
+ self.move = function (sheepArray, globalBestPosition) {
+ var pBest = {
+ x: self.x,
+ y: self.y
+ };
+ var pBestScore = Infinity;
+ var localBest = self.getLocalBest(sheepArray);
+ var r1 = Math.random();
+ var r2 = Math.random();
+ var inertiaWeight = 0.5;
+ var cognitiveComponent = 2.0;
+ var socialComponent = 2.0;
+ self.velocity = {
+ x: inertiaWeight * self.velocity.x + cognitiveComponent * r1 * (pBest.x - self.x) + socialComponent * r2 * (localBest.x - self.x),
+ y: inertiaWeight * self.velocity.y + cognitiveComponent * r1 * (pBest.y - self.y) + socialComponent * r2 * (localBest.y - self.y)
+ };
+ self.x += self.velocity.x;
+ self.y += self.velocity.y;
+ self.updateBest(pBest, pBestScore, globalBestPosition);
};
+ self.getLocalBest = function (sheepArray) {
+ var localBestPosition = {
+ x: self.x,
+ y: self.y
+ };
+ var bestScore = Infinity;
+ sheepArray.forEach(function (otherSheep) {
+ if (otherSheep !== self) {
+ var distance = Math.sqrt(Math.pow(otherSheep.x - self.x, 2) + Math.pow(otherSheep.y - self.y, 2));
+ if (distance < bestScore) {
+ bestScore = distance;
+ localBestPosition = {
+ x: otherSheep.x,
+ y: otherSheep.y
+ };
+ }
+ }
+ });
+ return localBestPosition;
+ };
+ self.updateBest = function (pBest, pBestScore, globalBestPosition) {
+ var currentScore = Math.sqrt(Math.pow(globalBestPosition.x - self.x, 2) + Math.pow(globalBestPosition.y - self.y, 2));
+ if (currentScore < pBestScore) {
+ pBest.x = self.x;
+ pBest.y = self.y;
+ pBestScore = currentScore;
+ }
+ };
self.avoidWalls = function () {
if (self.x < 50 || self.x > 1998 || self.y < 50 || self.y > 2682) {
self.direction += Math.PI;
}
};
self.avoidDog = function (dog) {
- var dogAvoidanceDistance = 100;
- var dx = self.x - dog.x;
- var dy = self.y - dog.y;
- var distance = Math.sqrt(dx * dx + dy * dy);
- if (distance < dogAvoidanceDistance) {
- var angle = Math.atan2(dy, dx);
- self.direction = angle + Math.PI;
+ if (self.intersects(dog)) {
+ self.direction += Math.PI / 2;
}
};
});
// Shepherd dog class
@@ -90,15 +115,14 @@
shepherdDog.move(pos);
});
// Main game tick event
LK.on('tick', function () {
- // Move each sheep and avoid other sheep
+ // Move each sheep
for (var i = 0; i < sheepArray.length; i++) {
var sheep = sheepArray[i];
sheep.move();
sheep.avoidWalls();
sheep.avoidDog(shepherdDog);
- sheep.avoidOtherSheep(sheepArray);
}
});
// Ensure the game is touchscreen compatible
game.on('down', function (obj) {
sheep, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
sheep, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Collie shepherd dog, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a white text bubble that says "baa," comic book style.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
double-sided blue arrow in the form of a semicircle. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
computer mouse top view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
angry big bear, lying down and sleeping, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
erase