This commit is contained in:
Wojciech Łukasik 2020-04-28 23:12:57 +02:00
parent 8a58a491b8
commit 52c46084e6

View File

@ -69,6 +69,7 @@ class Agent{
}
goForward(){
this.hideAgent()
if(this.turn == 'Up'){
this.positionY += 1;
}
@ -81,6 +82,26 @@ class Agent{
else if(this.turn == 'Right'){
this.positionX += 1;
}
this.showAgent()
}
goToField(field){
if(field.yField > this.positionY){
//pole jest nad agentem
}
if(field.yField < this.positionY){
//pole jest pod agentem
}
if(field.xField > this.positionX){
//pole jest na prawo
}
if(field.xField < this.positionX){
//pole jest na lewo
}
}
@ -94,8 +115,8 @@ class Field{
this.isOccupiedByAgent = isOccupiedByAgent;
this.costOfTravel = costOfTravel;
this.neighbors = [];
this.gScore = 0;
this.heuristic = 0;
this.g = 0;
this.h = 0;
this.f = 0;
this.previous = undefined;
}
@ -376,24 +397,37 @@ function addNeighbors(board){
}
}
function colorGreen(field){
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "green";
// function colorGreen(field){
// document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "green";
// }
// function colorRed(field){
// document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "red";
// }
// function colorYellow(field){
// document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "yellow";
// }
const colorFactory = color => (field, animationFrame) => {
const delay = animationFrame * 50
console.log(delay)
setTimeout(() => {
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = color;
}, delay);
return ++animationFrame
}
function colorRed(field){
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "red";
}
const colorYellow = colorFactory('yellow')
const colorGreen = colorFactory('green')
const colorRed = colorFactory('red')
function colorYellow(field){
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "yellow";
}
function addNeighborsToOpenSet(set, neigbors){
for(x = 0; x < neigbors.length; x++){
set.push(neigbors[x]);
colorGreen(neigbors[x]);
}
}
// function addNeighborsToOpenSet(set, neigbors){
// for(x = 0; x < neigbors.length; x++){
// set.push(neigbors[x]);
// colorGreen(neigbors[x]);
// }
// }
function addToClosedSet(set, field){
set.push(field);
@ -432,24 +466,27 @@ function removeFromSet(openSet, promisingField){
}
}
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
function aStar(startField, goalField){
let closedSet = [];
let openSet = [];
var path;
let animationFrame = 0
addToOpenSet(openSet, startField)
// addToOpenSet(openSet, startField)
openSet.push(startField);
colorGreen(startField, animationFrame);
while(openSet.length > 0){
let current = findLowestFScore(openSet, goalField);
// let current = findLowestFScore(openSet, goalField);
let winner = 0;
for(let i = 0; i < openSet.length; i++){
if (openSet[i].f < openSet[winner].f){
winner = i
}
}
let current = openSet[winner];
if(current === goalField){
path = []
@ -462,44 +499,45 @@ function aStar(startField, goalField){
console.log("A* COMPLETED");
for(var i = 0; i < path.length; i++){
colorYellow(path[i]);
animationFrame = colorYellow(path[i], animationFrame);
}
return path
return path;
}
removeFromSet(openSet, current);
addToClosedSet(closedSet, current);
// addToClosedSet(closedSet, current);
closedSet.push(current);
animationFrame = colorRed(current, animationFrame);
var neighbors = current.neighbors;
for(var i = 0; i < neighbors.length; i++){
for(var i = 0; i < neighbors.length; i++){
var neighbor = neighbors[i];
if(!closedSet.includes(neighbor)){
var tempG = current.gScore + neighbor.costOfTravel;
var tempG = current.g + neighbor.costOfTravel;
if(openSet.includes(neighbor)){
if(tempG < neighbor.gScore){
neighbor.gScore = tempG;
if(tempG < neighbor.g){
neighbor.g = tempG;
}
} else {
neighbor.gScore = tempG;
addToOpenSet(openSet, neighbor);
neighbor.g = tempG;
// addToOpenSet(openSet, neighbor);
openSet.push(neighbor);
animationFrame = colorGreen(neighbor,animationFrame);
}
neighbor.heuristic = getDistance(neighbor, goalField);
neighbor.f = neighbor.gScore + neighbor.heuristic;
neighbor.previous = current;
neighbor.h = getDistance(neighbor, goalField);
neighbor.f = neighbor.g + neighbor.h;
neighbor.previous = current;
}
}
}
}
function executePath(agent, path){
}
@ -509,15 +547,22 @@ board = createCostofField(board, 10, 10);
let candy = new Candy('zelek', 'truskawkowy', 'Jumbo', 541);
let shelfs = createShelf();
let agent = new Agent(0,0, 'Right');
var button = document.getElementById("testButton");
addNeighbors(board);
function delayedFunction(func, index, time) {
setTimeout(() => func(), time * index);
}
function start(){
showBoard(board);
let path = aStar(board[0][0], board[7][7]);
//console.log(path);
// for (let i = 0; i<9; i++) {
// delayedStep(agent, i);
// }
const path = aStar(board[0][0], board[9][9]);
// agent.showAgent();
console.log(path.reverse())
}