-
Notifications
You must be signed in to change notification settings - Fork 0
/
Position.ts
36 lines (30 loc) · 835 Bytes
/
Position.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
export class Position {
constructor(public readonly y: number, public readonly x: number) {
}
up(): Position {
return new Position(this.y + 1, this.x);
}
down(): Position {
return new Position(this.y - 1, this.x);
}
left(): Position {
return new Position(this.y, this.x - 1);
}
right(): Position {
return new Position(this.y, this.x + 1);
}
applyDirection(direction: string): Position {
switch (direction) {
case 'U':
return this.up();
case 'D':
return this.down();
case 'L':
return this.left();
case 'R':
return this.right();
default:
throw new Error(`Unknown direction ${direction}`);
}
}
}