Merge branch 'master' of s444428/AL-2020 into master

This commit is contained in:
Wojciech Łukasik 2020-04-28 17:51:12 +00:00 committed by Gogs
commit 35518f0f86
5 changed files with 235 additions and 40 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

View File

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

View File

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

View File

@ -16,22 +16,7 @@ class Agent{
params = {positionX, positionY, turn};
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(){
if(this.turn == 'Up'){
@ -64,7 +49,6 @@ class Agent{
}
showAgent(){
console.log(this.turn)
if(this.turn === "Up"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Up.png')";
}
@ -84,6 +68,21 @@ class Agent{
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;
}
}
}
@ -94,6 +93,11 @@ class Field{
this.isShelf = isShelf;
this.isOccupiedByAgent = isOccupiedByAgent;
this.costOfTravel = costOfTravel;
this.neighbors = [];
this.gScore = 0;
this.heuristic = 0;
this.f = 0;
this.previous = undefined;
}
getParams(){
@ -123,7 +127,13 @@ class Field{
setCostOfTravel(costOfTravel){
this.costOfTravel = costOfTravel;
}
}
setNeighbors(field){
if(field.isShelf == false){
this.neighbors.push(field)
}
}
}
class Shelf{
@ -180,7 +190,8 @@ class Candy{
}
}
//funckcje
//funkcje
function createBoard(rangeX, rangeY){
const board = [];
@ -328,21 +339,186 @@ function showBoard(board){
if(board[y][x].getIsShelf())
document.getElementById("row-" + y).innerHTML += "<div class='regal2' id=" + x + "-" + y + "> </div>"
else
document.getElementById("row-" + y).innerHTML += "<div class='field' id=" + x + "-" + y + "> </div>"
document.getElementById("row-" + y).innerHTML += "<div class='field' id=" + x + "-" + y + ">"+ board[y][x].costOfTravel + "</div>"
}
}
}
function getDistance(field, goalField){
// var a = field.xField - goalField.xField;
// var b = field.yField - goalField.yField;
// var c = Math.sqrt(a*a + b*b);
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){
}
let board = createBoard(10,10);
board = createShelfOnBoard(board);
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 start(){
showBoard(board);
agent.showAgent();
showBoard(board);
let path = aStar(board[0][0], board[7][7]);
//console.log(path);
}

View File

@ -83,3 +83,21 @@ body {
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%;
}