Whtmlets/js2020/Jakhangirov/index.html

Материал из Department of Theoretical and Applied Mechanics
Версия от 14:33, 2 июня 2020; Totamonik (обсуждение | вклад) (Код программы Cell на языке JavaScript)

Перейти к: навигация, поиск

Описание

Генератор лабиринта на JavaScript

Исполнитель: Джахангиров Мансур

Группа 3630103/90001 Кафедра Теоретической механики

Визуализация

Код программы scratch на языке JavaScript:
 1 let cols, rows;
 2 let w = 20;
 3 let grid = [];
 4 let current;
 5 let stack = [];
 6 
 7 function setup() {
 8   createCanvas(600, 600);
 9   cols = floor(width / w);
10   rows = floor(height / w);
11 
12   for (let j = 0; j < rows; j++) {
13     for (let i = 0; i < cols; i++) {
14       var cell = new Cell(i, j);
15       grid.push(cell);
16     }
17   }
18 
19   current = grid[0];
20 }
21 
22 function draw() {
23   background(51);
24   for (let i = 0; i < grid.length; i++) {
25     grid[i].show();
26   }
27 
28   current.visited = true;
29   current.highlight();
30   // STEP 1
31   let next = current.checkNeighbors();
32   if (next) {
33     next.visited = true;
34 
35     // STEP 2
36     stack.push(current);
37 
38     // STEP 3
39     removeWalls(current, next);
40 
41     // STEP 4
42     current = next;
43   } else if (stack.length > 0) {
44     current = stack.pop();
45   }
46 }
47 
48 function index(i, j) {
49   if (i < 0 || j < 0 || i > cols - 1 || j > rows - 1) {
50     return -1;
51   }
52   return i + j * cols;
53 }
54 
55 function removeWalls(a, b) {
56   let x = a.i - b.i;
57   if (x === 1) {
58     a.walls[3] = false;
59     b.walls[1] = false;
60   } else if (x === -1) {
61     a.walls[1] = false;
62     b.walls[3] = false;
63   }
64   let y = a.j - b.j;
65   if (y === 1) {
66     a.walls[0] = false;
67     b.walls[2] = false;
68   } else if (y === -1) {
69     a.walls[2] = false;
70     b.walls[0] = false;
71   }
72 }
Код программы Cell на языке JavaScript:
 1 function Cell(i, j) {
 2   this.i = i;
 3   this.j = j;
 4   this.walls = [true, true, true, true];
 5   this.visited = false;
 6 
 7   this.checkNeighbors = function() {
 8     let neighbors = [];
 9 
10     let top = grid[index(i, j - 1)];
11     let right = grid[index(i + 1, j)];
12     let bottom = grid[index(i, j + 1)];
13     let left = grid[index(i - 1, j)];
14 
15     if (top && !top.visited) {
16       neighbors.push(top);
17     }
18     if (right && !right.visited) {
19       neighbors.push(right);
20     }
21     if (bottom && !bottom.visited) {
22       neighbors.push(bottom);
23     }
24     if (left && !left.visited) {
25       neighbors.push(left);
26     }
27 
28     if (neighbors.length > 0) {
29       let r = floor(random(0, neighbors.length));
30       return neighbors[r];
31     } else {
32       return undefined;
33     }
34   };
35   this.highlight = function() {
36     let x = this.i * w;
37     let y = this.j * w;
38     noStroke();
39     fill(0, 0, 255, 100);
40     rect(x, y, w, w);
41   };
42 
43   this.show = function() {
44     let x = this.i * w;
45     let y = this.j * w;
46     stroke(255);
47     if (this.walls[0]) {
48       line(x, y, x + w, y);
49     }
50     if (this.walls[1]) {
51       line(x + w, y, x + w, y + w);
52     }
53     if (this.walls[2]) {
54       line(x + w, y + w, x, y + w);
55     }
56     if (this.walls[3]) {
57       line(x, y + w, x, y);
58     }
59 
60     if (this.visited) {
61       noStroke();
62       fill(255, 0, 255, 100);
63       rect(x, y, w, w);
64     }
65   };
66 }