-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.pde
78 lines (70 loc) · 1.58 KB
/
Cell.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Blueprint of a node
class Cell {
// Node coordinates and size
protected float x, y, dimension;
// Color of the node
protected int i, j, f, g, h;
protected boolean wall;
protected Cell cameFrom;
protected ArrayList<Cell>neighbors = new ArrayList<Cell>();
Cell(float x, float y, float dimension, int i, int j) {
this.x = x;
this.y = y;
this.i = i;
this.j = j;
this.dimension = dimension;
this.f = 0;
this.g = 0;
this.h = 0;
this.cameFrom = null;
this.wall = false;
if(random(1) < 0.5) {
this.wall = true;
}
}
void display(int r, int g, int b) {
fill(r, g, b);
if(this.wall) {
fill(0, 0, 0);
}
rectMode(CENTER);
noStroke();
square(this.x, this.y, this.dimension);
}
void addNeighbors(Cell[][] grid) {
int a = this.i;
int b = this.j;
// Bottom neighbor
if(a < rows - 1) {
neighbors.add(grid[a+1][b]);
}
// Above neighbor
if(a > 0) {
neighbors.add(grid[a-1][b]);
}
// Right neighbor
if(b < columns - 1) {
neighbors.add(grid[a][b+1]);
}
// Left neighbor
if(b > 0) {
neighbors.add(grid[a][b-1]);
}
// Left-top neighbor
if(a > 0 && b > 0) {
neighbors.add(grid[a-1][b-1]);
}
// Right-top neighbor
if(a > 0 && b < columns - 1) {
neighbors.add(grid[a-1][b+1]);
}
// Left-bottom neighbor
if(a < rows - 1 && b > 0) {
neighbors.add(grid[a+1][b-1]);
}
// Right-bottom neighbor
if(a < rows - 1 && b < columns - 1) {
neighbors.add(grid[a+1][b+1]);
}
}
}