-
Notifications
You must be signed in to change notification settings - Fork 0
/
Square.js
162 lines (133 loc) · 4.26 KB
/
Square.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Object representing a square tile in the game
// (x, y): coordinates of the top left corner
// index: index in the global squares array
// status: current status of the tile (boolean, true for empty, false for occupied)
// isObstacle: boolean value (true if the square is an obstacle)
function Square(x_, y_, index_) {
this.x = x_;
this.y = y_;
this.index = index_;
this.status = true; // begins empty because we generate the solution first
this.isObstacle = false; // likewise
// Draws the square
this.draw = function(c, isCurrent = false) {
fill(c);
if (torusMode) {
circle(this.x + squareSize/2, this.y + squareSize/2, squareSize);
if (isCurrent) {
fill(backgroundColor);
//circle(this.x + squareSize/2, this.y + squareSize/2, squareSize/2);
circle(this.x + squareSize/2, this.y + squareSize/2, squareSize - squareGap*2);
}
} else {
square(this.x, this.y, squareSize, squareGap);
if (isCurrent) {
fill(backgroundColor);
//square(this.x + squareSize/4, this.y + squareSize/4, squareSize/2, squareGap/2);
square(this.x + squareGap, this.y + squareGap, squareSize - squareGap*2, squareGap/2);
}
}
}
// Flips the value of status
this.changeStatus = function() {
this.status = !(this.status);
}
// Returns boolean value, true if the square is on the top border of the game
this.isOnTopBorder = function() {
return (this.index < numSquaresW);
}
// Returns boolean value, true if the square is on the bottom border of the game
this.isOnBottomBorder = function() {
return (this.index >= numSquares - numSquaresW);
}
// Returns boolean value, true if the square is on the right border of the game
this.isOnRightBorder = function() {
return (this.index % numSquaresW === numSquaresW - 1);
}
// Returns boolean value, true if the square is on the right border of the game
this.isOnLeftBorder = function() {
return (this.index % numSquaresW === 0);
}
// Returns the square on top of this one if it exists, null otherwise
this.up = function() {
if (this.isOnTopBorder()) {
// in the top row
if (torusMode) {
return squares[this.index + numSquares - numSquaresW];
} else {
return null;
}
}
return squares[this.index - numSquaresW];
}
// Returns the square down under this one if it exists, null otherwise
this.down = function() {
if (this.isOnBottomBorder()) {
// in the last row
if (torusMode) {
return squares[this.index - numSquares + numSquaresW];
} else {
return null;
}
}
return squares[this.index + numSquaresW];
}
// Returns the square on the right of this one if it exists, null otherwise
this.right = function() {
if (this.isOnRightBorder()) {
// in the last column
if (torusMode) {
return squares[this.index + 1 - numSquaresW];
} else {
return null;
}
}
return squares[this.index + 1];
}
// Returns the square on the left of this one if it exists, null otherwise
this.left = function() {
if (this.isOnLeftBorder()) {
// in the first column
if (torusMode) {
return squares[this.index - 1 + numSquaresW];
} else {
return null;
}
}
return squares[this.index - 1];
}
// Returns the square obtained when moving into the direction given as argument if it exists, null otherwise
this.get = function(direction) {
switch (direction) {
case 'u':
return this.up();
case 'd':
return this.down();
case 'r':
return this.right();
case 'l':
return this.left();
}
}
// Returns whether this square can go in the direction given as argument
this.canGo = function(direction) {
if (direction === "NAD") { // Not A Direction
return false;
}
if (this.get(direction) === null) {
return false;
}
return this.get(direction).status;
}
// Returns an array of characters corresponding to the directions where this square can move
this.possibilities = function() {
// You wonder what I see in your future?
let p = [];
for (let dir of ['u', 'd', 'r', 'l']) {
if (this.canGo(dir)) {
p.push(dir);
}
}
return p;
}
}