-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
310 lines (266 loc) · 7.37 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const NUMBER_OF_ROWS = 5;
const NUMBER_OF_COLUMNS = 5;
const DIRECTION_SEQUENCE = ["Right", "Down", "Left", "Up"];
/**
* @typedef {Object} Dimension
* @property {number} width - Object's width
* @property {number} height - Object's height
*/
/**
* @typedef {Object} CreateBoardReturn
* @property {Element | null} instance - Then board instance
* @property {function():void} init - Creating board and grid
* @property {function():Dimension} getGridSize - Get the dimension of the board
*/
/**
* Create a new board.
*
* @param {Object} options
* @param {string} options.selector
* @returns {CreateBoardReturn}
*/
const createBoard = ({ selector }) => {
const board = document.querySelector(selector);
const numberOfGrids = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
const init = () => {
if (!board) {
console.warn("Board is not found");
return;
}
for (let i = 0; i < numberOfGrids; i++) {
const gridElement = document.createElement("div");
gridElement.id = `br-board-grid-${i + 1}`;
gridElement.classList.add("br-board-grid");
board.appendChild(gridElement);
}
};
const getGridSize = () => {
if (!board) {
console.warn("Board is not found");
return {
width: 0,
height: 0,
};
}
return {
width: board.clientWidth / NUMBER_OF_ROWS,
height: board.clientHeight / NUMBER_OF_COLUMNS,
};
};
return {
instance: board,
init,
getGridSize,
};
};
/**
* @typedef {Object} RobotPosition
* @property {number} column - The robot's current column
* @property {number} row - The robot's current row
*/
/**
* @typedef {Object} CreateRobotReturn
* @property {Element | null} instance - Then robot instance
* @property {function(number=):void} move - Move the robot forwards
* @property {function():void} rotate - Change the direction of the robot clockwise
* @property {function():Dimension} getDirection - Get the cardinal direction the robot is facing
* @property {function():void} updateRobotPositions - Get the cardinal direction the robot is facing
*/
/**
* Create a new robot.
*
* @param {Object} options
* @param {string} options.robotSelector - The CSS selector string for the robot.
* @returns {CreateRobotReturn}
*/
const createRobot = ({ robotSelector }) => {
const robot = document.querySelector(robotSelector);
let currentDirection = DIRECTION_SEQUENCE[0];
// Robot starts at the top left corner of the board.
// This translates into a position of [0, 0] with a zero-based index.
let currentRow = 0;
let currentColumn = 0;
/**
* Check if the robot can move to the next grid within the board.
*
* @param {Object} options
* @param {number} options.nextColumn
* @param {number} options.nextRow
* @returns boolean
*/
const canMove = ({ nextColumn, nextRow }) => {
return (
0 <= nextColumn &&
nextColumn < NUMBER_OF_COLUMNS &&
0 <= nextRow &&
nextRow < NUMBER_OF_ROWS
);
};
/**
* Move the robot.
*
* @param {number} [distance=0] - How far the robot should move.
*/
const move = (distance = 0) => {
if (!distance) {
console.warn("Unknown distance: " + distance);
return;
}
if (!robot) {
console.warn("Robot is not found");
return;
}
switch (currentDirection) {
case "Right": {
if (
canMove({
nextColumn: currentColumn + 1,
nextRow: currentRow,
})
) {
currentColumn += 1;
}
break;
}
case "Down": {
if (
canMove({
nextColumn: currentColumn,
nextRow: currentRow + 1,
})
) {
currentRow += 1;
}
break;
}
case "Left": {
if (
canMove({
nextColumn: currentColumn - 1,
nextRow: currentRow,
})
) {
currentColumn -= 1;
}
break;
}
case "Up": {
if (
canMove({
nextColumn: currentColumn,
nextRow: currentRow - 1,
})
) {
currentRow -= 1;
}
break;
}
default: {
// do nothing
}
}
updateRobotPositions(distance);
};
/**
* Rotate the robot clockwise.
*/
const rotate = () => {
const CSS_VAR_ROBOT_ROTATE_ANGLE = "--br-robot-rotate-angle";
const currentDirectionIndex = DIRECTION_SEQUENCE.findIndex(
(direction) => direction === currentDirection
);
if (currentDirectionIndex >= 0) {
const nextDirectionIndex = (currentDirectionIndex + 1) % 4;
currentDirection = DIRECTION_SEQUENCE[nextDirectionIndex];
} else {
currentDirection = DIRECTION_SEQUENCE[0];
}
const rotateAngleString = window
.getComputedStyle(document.documentElement)
.getPropertyValue(CSS_VAR_ROBOT_ROTATE_ANGLE);
const rotateAngle = !isNaN(parseInt(rotateAngleString))
? (parseInt(rotateAngleString) + 90) % 360
: 90;
document.documentElement.style.setProperty(
CSS_VAR_ROBOT_ROTATE_ANGLE,
`${rotateAngle}deg`
);
};
/**
* The current direction the robot is facing.
* @returns {string} One of the cardinal direction: `Right`, `Down`, `Left`, `Up`.
*/
const getDirection = () => {
return currentDirection;
};
/**
* Update the current position (top and left) of the robot.
*
* @param {number} distance
*/
const updateRobotPositions = (distance = 0) => {
robot.style.left = `${currentColumn * distance}px`;
robot.style.top = `${currentRow * distance}px`;
};
return {
instance: robot,
move,
rotate,
getDirection,
updateRobotPositions,
};
};
document.addEventListener("DOMContentLoaded", () => {
const board = createBoard({ selector: ".br-board-container > div.br-board" });
if (!board.instance) {
console.warn("Board is not found");
return;
}
board.init();
const robot = createRobot({
robotSelector: ".br-board-container > .br-robot",
});
// add actions to the keyup event for the following keys:
// - the "M" key to "move forwards", and
// - the "R" key to "rotate"
window.addEventListener("keyup", (kbEvent) => {
if (
kbEvent.target instanceof HTMLButtonElement &&
kbEvent.target.classList.contains("br-control")
) {
// do nothing
return;
}
switch (kbEvent.key) {
case "m": {
const { width: moveDistance } = board.getGridSize();
robot?.move(moveDistance);
break;
}
case "r": {
robot?.rotate();
break;
}
default: {
// do nothing
}
}
});
// initialise "move forward" control button & add click action
const moveButton = document.querySelector("button.br-control-move");
moveButton?.addEventListener("click", () => {
const { width: moveDistance } = board.getGridSize();
robot.move(moveDistance);
});
// initialise "rotate" control button & add click action
const rotateButton = document.querySelector("button.br-control-rotate");
rotateButton?.addEventListener("click", () => {
robot.rotate();
});
// Update the robot position on the board.
// NOTE: This is required for the robot is Absolute-positioned on top of the board.
window.addEventListener("resize", () => {
const { width: moveDistance } = board.getGridSize();
robot.updateRobotPositions(moveDistance);
});
});