-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgame.js
64 lines (59 loc) · 1.61 KB
/
game.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
/**
input:
- gameBoardWidth: int
- gameBoardHeight: int
- mPosition: ?
- hPosition: ?
output:
function render(input: ...): string[]
[
"XXXXXXXXXXXXX",
"X.......H...X",
"X...........X",
"X.M.........X",
"X...........X",
"XXXXXXXXXXXXX",
]
*/
class Game {
constructor(gameBoardWidth, gameBoardHeight, mPosition, hPosition) {
this.container = [];
this.gameBoardWidth = gameBoardWidth;
this.gameBoardHeight = gameBoardHeight;
this.mPosition = mPosition;
this.hPosition = hPosition;
this.render();
}
render() {
for (let i = 0; i < this.gameBoardHeight; i++) {
let wall = "X";
for (let j = 1; j < this.gameBoardWidth; j++) {
if (i === 0 || i === this.gameBoardHeight - 1) {
wall += "X";
}
else {
if (j === this.gameBoardWidth - 1) {
wall += "X";
}
else {
if (i === this.mPosition.x && j === this.mPosition.y) {
wall += "M";
}
else if (i === this.hPosition.x &&
j === this.hPosition.y) {
wall += "H";
}
else {
wall += ".";
}
}
}
}
this.container.push(wall);
}
}
}
const m = { x: 3, y: 2 };
const h = { x: 1, y: 8 };
const game = new Game(13, 6, m, h);
console.log(game);