2
0
forked from s444420/AL-2020
AL-2020/frontend/js/main.js

357 lines
7.9 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;
}
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'){
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 = "";
}
}
class Field{
constructor(x, y, isShelf, isOccupiedByAgent, costOfTravel){
this.xField = x;
this.yField = y;
this.isShelf = isShelf;
this.isOccupiedByAgent = isOccupiedByAgent;
this.costOfTravel = costOfTravel;
}
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 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;
}
}
//funckcje
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
let shelf = new Shelf(2, 1, true, 221, 222, 0);
listOfShelf.push(shelf)
shelf = new Shelf(3, 1, true, 321, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(4, 4, false, 341, 342, 343);
listOfShelf.push(shelf)
shelf = new Shelf(1, 4, true, 0, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(4, 6, true, 361, 362, 0);
listOfShelf.push(shelf)
shelf = new Shelf(1, 6, true, 261, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(3, 9, true, 381, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(2, 9, true, 281, 0, 0);
listOfShelf.push(shelf)
//drugi poziom
shelf = new Shelf(5, 1, true, 521, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(5, 4, true, 531, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(5, 6, true, 571, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(5, 9, true, 581, 0, 0);
listOfShelf.push(shelf)
//trzeci poziom
shelf = new Shelf(7, 1, true, 721, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(8, 1, true, 821, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(6, 4, true, 741, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(9, 4, true, 841, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(6, 6, true, 761, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(9, 6, true, 861, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(7, 9, true, 781, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(8, 9, true, 881, 0, 0);
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 13:49:03 +02:00
function getDistance(x1,y1,x2,y2){
var a = x1 - x2;
var b = y1 - y2;
var c = Math.sqrt(a*a + b*b);
return c
}
let board = createBoard(10,10);
2020-04-28 13:51:55 +02:00
board = createShelfOnBoard(board);
board = createCostofField(board, 10, 10);
let candy = new Candy('zelek', 'truskawkowy', 'Jumbo', 222);
let shelfs = createShelf();
2020-04-28 12:03:40 +02:00
let agent = new Agent(0,0, 'Right');
2020-04-27 20:04:11 +02:00
function start(){
2020-04-28 14:00:39 +02:00
showBoard(board);
console.log(board);
}
2020-04-27 20:04:11 +02:00