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