AL-2020/frontend/js/main.js

525 lines
12 KiB
JavaScript
Raw Normal View History

//Klasy
class Agent{
constructor(positionX, positionY, turn){
this.positionX = positionX;
this.positionY = positionY;
this.turn = turn;
}
getParams(){
let params = {};
positionX = this.positionX;
positionY = this.positionY;
turn = this.turn;
params = {positionX, positionY, turn};
return params;
2020-04-28 19:44:49 +02:00
}
turnLeft(){
if(this.turn == 'Up'){
this.turn = 'Left';
}
else if(this.turn == 'Down'){
this.turn = 'Right';
}
else if(this.turn == 'Left'){
this.turn = 'Down';
}
else if(this.turn == 'Right'){
this.turn = 'Up'
}
}
turnRight(){
if(this.turn == 'Up'){
this.turn = 'Right';
}
else if(this.turn == 'Down'){
this.turn = 'Left';
}
else if(this.turn == 'Left'){
this.turn = 'Up';
}
else if(this.turn == 'Right'){
this.turn = 'Down'
}
}
showAgent(){
if(this.turn === "Up"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Up.png')";
}
else if(this.turn === "Down"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Down.png')";
}
else if(this.turn === "Right"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Right.png')";
}
else if(this.turn === "Left"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Left.png')";
}
}
hideAgent(){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "";
}
2020-04-28 19:44:49 +02:00
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;
}
}
}
class Field{
constructor(x, y, isShelf, isOccupiedByAgent, costOfTravel){
this.xField = x;
this.yField = y;
this.isShelf = isShelf;
this.isOccupiedByAgent = isOccupiedByAgent;
this.costOfTravel = costOfTravel;
2020-04-28 19:44:49 +02:00
this.neighbors = [];
this.gScore = 0;
this.heuristic = 0;
this.f = 0;
this.previous = undefined;
}
getParams(){
let params = {};
xField = this.xField;
yField = this.yField;
isShelf = this.isShelf;
isOccupiedByAgent = this.isOccupiedByAgent;
costOfTravel = this.costOfTravel;
params = {xField, yField, isShelf, isOccupiedByAgent, costOfTravel}
return params;
}
2020-04-28 12:03:40 +02:00
setIsShelf(isShelf){
this.isShelf = isShelf;
}
getIsShelf(){
return this.isShelf;
}
setIsOccupiedByAgent(isOccupiedByAgent){
this.isOccupiedByAgent = isOccupiedByAgent;
}
2020-04-28 13:51:55 +02:00
setCostOfTravel(costOfTravel){
this.costOfTravel = costOfTravel;
2020-04-28 19:44:49 +02:00
}
setNeighbors(field){
if(field.isShelf == false){
this.neighbors.push(field)
}
}
2020-04-28 12:03:40 +02:00
}
class Shelf{
2020-04-28 13:51:55 +02:00
constructor(accessY, accessX, havePlace, box1, box2, box3){
this.accessX = accessX;
this.accessY = accessY;
2020-04-28 12:03:40 +02:00
this.havePlace = true;
2020-04-28 13:51:55 +02:00
this.box1 = box1;
this.box2 = box2;
this.box3 = box3;
2020-04-28 12:03:40 +02:00
}
getParams(){
let params = {};
xField = this.xField;
yField = this.yField;
havePlace = this.havePlace;
box1 = this.box1;
box2 = this.box2;
box3 = this.box3;
params = {xField, yField, havePlace, box1, box2, box3}
return params;
}
setbox1(box1){
this.box1 = box1;
}
setbox2(box1){
2020-04-28 13:51:55 +02:00
this.box2 = box2;
2020-04-28 12:03:40 +02:00
}
setbox3(box1){
2020-04-28 13:51:55 +02:00
this.box3 = box3;
2020-04-28 12:03:40 +02:00
}
setHavePlace(havePlace){
this.havePlace = havePlace;
}
}
2020-04-28 13:51:55 +02:00
class Candy{
constructor(type, taste, mark, id){
this.type = type;
this.taste = taste;
this.mark = mark;
this.id = id;
}
getId(){
return this.id;
}
}
2020-04-28 19:44:49 +02:00
//funkcje
function createBoard(rangeX, rangeY){
const board = [];
for(let y = 0; y < rangeY; y++){
const row = [];
for(let x = 0; x < rangeX; x++){
let field = new Field(x, y, false, false, 1);
row.push(field)
}
board.push(row)
}
return board
}
2020-04-28 13:51:55 +02:00
function createShelfOnBoard(board){
2020-04-28 12:03:40 +02:00
//pierwszy poziom
board[2][2].setIsShelf(true);
board[3][2].setIsShelf(true);
board[2][4].setIsShelf(true);
board[3][4].setIsShelf(true);
board[2][6].setIsShelf(true);
board[3][6].setIsShelf(true);
board[2][8].setIsShelf(true);
board[3][8].setIsShelf(true);
//drugi poziom
board[5][2].setIsShelf(true);
board[5][3].setIsShelf(true);
board[5][7].setIsShelf(true);
board[5][8].setIsShelf(true);
//trzeci poziom
board[7][2].setIsShelf(true);
board[8][2].setIsShelf(true);
board[7][4].setIsShelf(true);
board[8][4].setIsShelf(true);
board[7][6].setIsShelf(true);
board[8][6].setIsShelf(true);
board[7][8].setIsShelf(true);
board[8][8].setIsShelf(true);
return board
}
2020-04-28 13:51:55 +02:00
function createCostofField(board, rangeX, rangeY){
let cost;
let number;
for(let y = 0; y < rangeY; y++){
for(let x = 0; x < rangeX; x++){
number = 0;
if(y<9 && board[y+1][x].getIsShelf()){
number = number + 1;
}
if(y>0 && board[y-1][x].getIsShelf()){
number = number + 1;
}
if(x<9 && board[y][x+1].getIsShelf()){
number = number + 1;
}
if(x>0 && board[y][x-1].getIsShelf()){
number = number + 1;
}
cost = number*2;
if(cost==0){
cost = 1;
}
board[y][x].setCostOfTravel(cost);
}
}
return board;
}
function createShelf(){
const listOfShelf = [];
//pierwszy poziom
2020-04-28 14:04:14 +02:00
let shelf = new Shelf(2, 1, true, 211, 212, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(3, 1, true, 311, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(4, 4, false, 441, 442, 443);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
shelf = new Shelf(1, 4, true, 0, 0, 0);
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(4, 6, true, 461, 462, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(1, 6, true, 161, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(3, 9, true, 391, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(2, 9, true, 291, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
//drugi poziom
2020-04-28 14:04:14 +02:00
shelf = new Shelf(5, 1, true, 511, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(5, 4, true, 541, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(5, 6, true, 561, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(5, 9, true, 591, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
//trzeci poziom
2020-04-28 14:04:14 +02:00
shelf = new Shelf(7, 1, true, 711, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(8, 1, true, 811, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(6, 4, true, 641, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(9, 4, true, 941, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(6, 6, true, 661, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(9, 6, true, 961, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(7, 9, true, 791, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
2020-04-28 14:04:14 +02:00
shelf = new Shelf(8, 9, true, 891, 0, 0);
2020-04-28 13:51:55 +02:00
listOfShelf.push(shelf)
return listOfShelf;
}
function showBoard(board){
for(let y = board.length - 1; y >= 0 ; y--){
document.getElementById("board").innerHTML += "<div class='row' id='row-" + y + "'> </div>";
for(let x = 0; x < board[y].length; x++){
2020-04-28 12:03:40 +02:00
if(board[y][x].getIsShelf())
document.getElementById("row-" + y).innerHTML += "<div class='regal2' id=" + x + "-" + y + "> </div>"
else
2020-04-28 14:00:39 +02:00
document.getElementById("row-" + y).innerHTML += "<div class='field' id=" + x + "-" + y + ">"+ board[y][x].costOfTravel + "</div>"
}
}
}
2020-04-28 19:44:49 +02:00
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){
2020-04-28 13:49:03 +02:00
}
let board = createBoard(10,10);
2020-04-28 13:51:55 +02:00
board = createShelfOnBoard(board);
board = createCostofField(board, 10, 10);
2020-04-28 14:04:14 +02:00
let candy = new Candy('zelek', 'truskawkowy', 'Jumbo', 541);
2020-04-28 13:51:55 +02:00
let shelfs = createShelf();
let agent = new Agent(0,0, 'Right');
2020-04-28 19:44:49 +02:00
var button = document.getElementById("testButton");
addNeighbors(board);
2020-04-27 20:04:11 +02:00
function start(){
2020-04-28 14:00:39 +02:00
showBoard(board);
2020-04-28 19:44:49 +02:00
let path = aStar(board[0][0], board[7][7]);
//console.log(path);
}
2020-04-27 20:04:11 +02:00
2020-04-28 19:44:49 +02:00