zmianyyy #3

Merged
s444428 merged 1 commits from s444428/AL-2020:master into master 2020-04-28 19:51:13 +02:00
4 changed files with 228 additions and 41 deletions
Showing only changes of commit 8a58a491b8 - Show all commits

View File

@ -13,6 +13,7 @@
<div class="board" id="board"> <div class="board" id="board">
</div> </div>
</div> </div>
</body> </body>
</html> </html>

View File

@ -94,22 +94,22 @@
// document.getElementById(agent.getId()).style.backgroundImage = "url('Agent.jpg')"; // document.getElementById(agent.getId()).style.backgroundImage = "url('Agent.jpg')";
// } // }
// //Funkcja zmieniajaca polozenie agenta o 1 pole //Funkcja zmieniajaca polozenie agenta o 1 pole
// function przemieszczenie(side, time, agent){ function przemieszczenie(side, time, agent){
// const lastPosition = agent.getId(); const lastPosition = agent.getId();
// setTimeout(function(){ setTimeout(function(){
// usunAgenta(agent) usunAgenta(agent)
// if(side == "left") if(side == "left")
// agent.left(); agent.left();
// if(side == "right") if(side == "right")
// agent.right(); agent.right();
// if(side == "up") if(side == "up")
// agent.up(); agent.up();
// if(side == "down") if(side == "down")
// agent.down(); agent.down();
// umiescAgenta(agent); umiescAgenta(agent);
// }, time); }, time);
// } }
// //Funkcja ktora ustala droge agenta do przebycia // //Funkcja ktora ustala droge agenta do przebycia
// function droga(agent) { // function droga(agent) {

View File

@ -16,22 +16,7 @@ class Agent{
params = {positionX, positionY, turn}; params = {positionX, positionY, turn};
return params; return params;
} }
goForward(){
if(this.turn == 'Up'){
this.positionY += 1;
}
else if(this.turn == 'Down'){
this.positionY -= 1;
}
else if(this.turn == 'Left'){
this.positionX -= 1;
}
else if(this.turn == 'Right'){
this.positionX += 1;
}
}
turnLeft(){ turnLeft(){
if(this.turn == 'Up'){ if(this.turn == 'Up'){
@ -83,6 +68,21 @@ class Agent{
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = ""; document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "";
} }
goForward(){
if(this.turn == 'Up'){
this.positionY += 1;
}
else if(this.turn == 'Down'){
this.positionY -= 1;
}
else if(this.turn == 'Left'){
this.positionX -= 1;
}
else if(this.turn == 'Right'){
this.positionX += 1;
}
}
} }
@ -93,6 +93,11 @@ class Field{
this.isShelf = isShelf; this.isShelf = isShelf;
this.isOccupiedByAgent = isOccupiedByAgent; this.isOccupiedByAgent = isOccupiedByAgent;
this.costOfTravel = costOfTravel; this.costOfTravel = costOfTravel;
this.neighbors = [];
this.gScore = 0;
this.heuristic = 0;
this.f = 0;
this.previous = undefined;
} }
getParams(){ getParams(){
@ -122,7 +127,13 @@ class Field{
setCostOfTravel(costOfTravel){ setCostOfTravel(costOfTravel){
this.costOfTravel = costOfTravel; this.costOfTravel = costOfTravel;
} }
setNeighbors(field){
if(field.isShelf == false){
this.neighbors.push(field)
}
}
} }
class Shelf{ class Shelf{
@ -179,7 +190,8 @@ class Candy{
} }
} }
//funckcje
//funkcje
function createBoard(rangeX, rangeY){ function createBoard(rangeX, rangeY){
const board = []; const board = [];
@ -332,11 +344,162 @@ function showBoard(board){
} }
} }
function getDistance(x1,y1,x2,y2){ function getDistance(field, goalField){
var a = x1 - x2; // var a = field.xField - goalField.xField;
var b = y1 - y2; // var b = field.yField - goalField.yField;
var c = Math.sqrt(a*a + b*b); // var c = Math.sqrt(a*a + b*b);
return c var d = Math.abs(field.xField - goalField.xField) + Math.abs(field.yField - goalField.yField)
return d
}
function addNeighbors(board){
for(var y = 0; y < 10; y++){
for(var x = 0; x < 10; x++){
var leftNeigbor, rightNeighbor, topNeighbor, bottomNeighbor;
if(x > 0){
leftNeigbor = board[y][x - 1];
board[y][x].setNeighbors(leftNeigbor);
}
if(x < board[y].length - 1){
rightNeighbor = board[y][x + 1];
board[y][x].setNeighbors(rightNeighbor);
}
if(y > 0){
bottomNeighbor = board[y - 1][x];
board[y][x].setNeighbors(bottomNeighbor);
}
if(y < board.length - 1){
topNeighbor = board[y + 1][x];
board[y][x].setNeighbors(topNeighbor);
}
}
}
}
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";
}
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);
colorRed(field);
}
function addToOpenSet(set, field){
set.push(field);
colorGreen(field);
}
function calculateFScore(field, goalField){
let fscore, distanceToGoal, costOfTravel;
distanceToGoal = getDistance(field, goalField);
costOfTravel = field.costOfTravel;
fscore = costOfTravel + distanceToGoal;
return fscore;
}
function findLowestFScore(openSet, goalField){
let bestField = openSet[0];
for(var x = 1; x < openSet.length; x++){
if(calculateFScore(openSet[x], goalField) < calculateFScore(bestField, goalField)){
bestField = openSet[x];
}
}
return bestField;
}
function removeFromSet(openSet, promisingField){
for(var x = openSet.length - 1; x >= 0; x--){
if(openSet[x] == promisingField){
openSet.splice(x,1);
}
}
}
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;
addToOpenSet(openSet, startField)
while(openSet.length > 0){
let current = findLowestFScore(openSet, goalField);
if(current === goalField){
path = []
var temp = current;
path.push(temp);
while(temp.previous){
path.push(temp.previous);
temp = temp.previous
}
console.log("A* COMPLETED");
for(var i = 0; i < path.length; i++){
colorYellow(path[i]);
}
return path
}
removeFromSet(openSet, current);
addToClosedSet(closedSet, current);
var neighbors = current.neighbors;
for(var i = 0; i < neighbors.length; i++){
var neighbor = neighbors[i];
if(!closedSet.includes(neighbor)){
var tempG = current.gScore + neighbor.costOfTravel;
if(openSet.includes(neighbor)){
if(tempG < neighbor.gScore){
neighbor.gScore = tempG;
}
} else {
neighbor.gScore = tempG;
addToOpenSet(openSet, neighbor);
}
neighbor.heuristic = getDistance(neighbor, goalField);
neighbor.f = neighbor.gScore + neighbor.heuristic;
neighbor.previous = current;
}
}
}
}
function executePath(agent, path){
} }
@ -345,12 +508,17 @@ board = createShelfOnBoard(board);
board = createCostofField(board, 10, 10); 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);
function start(){ function start(){
showBoard(board); showBoard(board);
console.log(board);
let path = aStar(board[0][0], board[7][7]);
//console.log(path);
} }

View File

@ -83,3 +83,21 @@ body {
align-items: center; align-items: center;
} }
#buttonContainer{
width: 100%;
height: 150px;
display: flex;
align-items: center;
}
#testButton{
height:100px;
width:200px;
margin: -20px -50px;
position:relative;
top:30px;
left:48%;
}