-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliding-move.ts
90 lines (78 loc) · 2.05 KB
/
sliding-move.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
85
86
87
88
89
90
import { Move } from '../base/moving-strategy';
import { Board } from '../board';
export enum SlideDirection {
TOP = 'TOP',
TOP_RIGHT = 'TOP_RIGHT',
RIGHT = 'RIGHT',
BOTTOM_RIGHT = 'BOTTOM_RIGHT',
BOTTOM = 'BOTTOM',
BOTTOM_LEFT = 'BOTTOM_LEFT',
LEFT = 'LEFT',
TOP_LEFT = 'TOP_LEFT',
}
export class SlidingMove extends Move {
constructor(protected direction: SlideDirection) {
super();
}
computeMoves(board: Board, position: number): number[] {
const moves = [];
let newPosition = position;
while (newPosition >= 0 && newPosition <= 63) {
moves.push(newPosition);
const nextPosition = this.calculateStepDiff() + newPosition;
// Handle board edges
if (this.isEdgeCase(newPosition, nextPosition)) {
break;
}
newPosition = nextPosition;
const pieceAtStep = board.getByIndex(newPosition);
if (pieceAtStep != null) {
const currentPiece = board.getByIndex(position)!;
if (pieceAtStep.color != currentPiece.color) {
moves.push(newPosition);
}
break;
}
}
return moves;
}
private calculateStepDiff() {
switch (this.direction) {
case SlideDirection.TOP:
return -8;
case SlideDirection.TOP_RIGHT:
return -7;
case SlideDirection.RIGHT:
return 1;
case SlideDirection.BOTTOM_RIGHT:
return 9;
case SlideDirection.BOTTOM:
return 8;
case SlideDirection.BOTTOM_LEFT:
return 7;
case SlideDirection.LEFT:
return -1;
case SlideDirection.TOP_LEFT:
return -9;
default:
throw new Error('Invalid slide direction');
}
}
private isEdgeCase(currentPosition: number, nextPosition: number): boolean {
const currentRow = Math.floor(currentPosition / 8);
const nextRow = Math.floor(nextPosition / 8);
switch (this.direction) {
case SlideDirection.LEFT:
case SlideDirection.RIGHT:
return currentRow !== nextRow;
case SlideDirection.TOP_LEFT:
case SlideDirection.BOTTOM_LEFT:
return nextPosition % 8 === 7;
case SlideDirection.TOP_RIGHT:
case SlideDirection.BOTTOM_RIGHT:
return nextPosition % 8 === 0;
default:
return false;
}
}
}