Repository navigation
Expand file tree
/
Copy pathknight.js
More file actions
56 lines (53 loc) · 1.84 KB
/
Copy pathknight.js
File metadata and controls
56 lines (53 loc) · 1.84 KB
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
import { ChessPiece } from "./chess_piece.js";
import * as SQUARE from './square_methods.js';
export class Knight extends ChessPiece {
/**
* Pawn constructor
* @param {string} pieceID
* @param {string} location
* @param {string} image
*/
constructor(pieceID, location, image) {
super(pieceID, location, image);
}
/**
*
* @param {string} target the square we want to move to
* @param {Set<string>} occSet the set of squares which are occupied
* @returns true if we can move to the target
* @returns false otherwise
*/
canMove(target, occSet) {
if (super.canMove(target, occSet)) { //ask the parent class
return this.canMove2(target);//ask the knight's specific rules for movement
}
//else super says no, you cannot move there
return false;
}
/**
* helper function for the canMove function that can be unit tested since it doesn't use the DOM
* @param {string} target the square we want to move to
* @returns true if the knight is allowed to move there
* @returns false, with an error string otherwise
*/
canMove2(target){
const dxAbs = Math.abs(SQUARE.DeltaX(this.location, target));
const dyAbs = Math.abs(SQUARE.DeltaY(this.location, target));
if(dxAbs === 1){
if(dyAbs === 2){
return true;
}
this.setFeedback('For knight, if |DeltaX| === 1, then |DeltaY| must equal 2.');
return false;
}
if(dxAbs === 2){
if(dyAbs === 1){
return true;
}
this.setFeedback('For knight, if |DeltaX| === 2, then |DeltaY| must equal 1.');
return false;
}
this.setFeedback('A knight must move in an "L"-shaped pattern.');
return false;
}
}