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(){ goForward(){
this.hideAgent()
if(this.turn == 'Up'){ if(this.turn == 'Up'){
this.positionY += 1; this.positionY += 1;
} }
@ -81,6 +82,26 @@ class Agent{
else if(this.turn == 'Right'){ else if(this.turn == 'Right'){
this.positionX += 1; 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.isOccupiedByAgent = isOccupiedByAgent;
this.costOfTravel = costOfTravel; this.costOfTravel = costOfTravel;
this.neighbors = []; this.neighbors = [];
this.gScore = 0; this.g = 0;
this.heuristic = 0; this.h = 0;
this.f = 0; this.f = 0;
this.previous = undefined; this.previous = undefined;
} }
@ -376,24 +397,37 @@ function addNeighbors(board){
} }
} }
function colorGreen(field){ // function colorGreen(field){
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "green"; // 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){ const colorYellow = colorFactory('yellow')
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "red"; const colorGreen = colorFactory('green')
} const colorRed = colorFactory('red')
function colorYellow(field){ // function addNeighborsToOpenSet(set, neigbors){
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "yellow"; // 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){ function addToClosedSet(set, field){
set.push(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){ function aStar(startField, goalField){
let closedSet = []; let closedSet = [];
let openSet = []; let openSet = [];
var path; var path;
let animationFrame = 0
addToOpenSet(openSet, startField) // addToOpenSet(openSet, startField)
openSet.push(startField);
colorGreen(startField, animationFrame);
while(openSet.length > 0){ 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){ if(current === goalField){
path = [] path = []
@ -462,36 +499,38 @@ function aStar(startField, goalField){
console.log("A* COMPLETED"); console.log("A* COMPLETED");
for(var i = 0; i < path.length; i++){ for(var i = 0; i < path.length; i++){
colorYellow(path[i]); animationFrame = colorYellow(path[i], animationFrame);
} }
return path return path;
} }
removeFromSet(openSet, current); removeFromSet(openSet, current);
addToClosedSet(closedSet, current); // addToClosedSet(closedSet, current);
closedSet.push(current);
animationFrame = colorRed(current, animationFrame);
var neighbors = current.neighbors; var neighbors = current.neighbors;
for(var i = 0; i < neighbors.length; i++){ for(var i = 0; i < neighbors.length; i++){
var neighbor = neighbors[i]; var neighbor = neighbors[i];
if(!closedSet.includes(neighbor)){ if(!closedSet.includes(neighbor)){
var tempG = current.gScore + neighbor.costOfTravel; var tempG = current.g + neighbor.costOfTravel;
if(openSet.includes(neighbor)){ if(openSet.includes(neighbor)){
if(tempG < neighbor.gScore){ if(tempG < neighbor.g){
neighbor.gScore = tempG; neighbor.g = tempG;
} }
} else { } else {
neighbor.gScore = tempG; neighbor.g = tempG;
addToOpenSet(openSet, neighbor); // addToOpenSet(openSet, neighbor);
openSet.push(neighbor);
animationFrame = colorGreen(neighbor,animationFrame);
} }
neighbor.h = getDistance(neighbor, goalField);
neighbor.f = neighbor.g + neighbor.h;
neighbor.heuristic = getDistance(neighbor, goalField);
neighbor.f = neighbor.gScore + neighbor.heuristic;
neighbor.previous = current; neighbor.previous = current;
} }
} }
} }
@ -499,7 +538,6 @@ function aStar(startField, goalField){
function executePath(agent, path){ function executePath(agent, path){
} }
@ -509,15 +547,22 @@ board = createCostofField(board, 10, 10);
let candy = new Candy('zelek', 'truskawkowy', 'Jumbo', 541); let candy = new Candy('zelek', 'truskawkowy', 'Jumbo', 541);
let shelfs = createShelf(); let shelfs = createShelf();
let agent = new Agent(0,0, 'Right'); let agent = new Agent(0,0, 'Right');
var button = document.getElementById("testButton");
addNeighbors(board); addNeighbors(board);
function delayedFunction(func, index, time) {
setTimeout(() => func(), time * index);
}
function start(){ function start(){
showBoard(board); showBoard(board);
let path = aStar(board[0][0], board[7][7]); // for (let i = 0; i<9; i++) {
//console.log(path); // delayedStep(agent, i);
// }
const path = aStar(board[0][0], board[9][9]);
// agent.showAgent();
console.log(path.reverse())
} }