src/chess/pieces/Knight.js   A
last analyzed

Size

Lines of Code 35

Duplication

Duplicated Lines 35
Ratio 100 %

Test Coverage

Coverage 57.14%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 1
dl 35
loc 35
ccs 8
cts 14
cp 0.5714
rs 10
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Knight.js ➔ ??? 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/**
2
 * Knight module
3
 * @module
4
 */
5 View Code Duplication
"use strict";
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
6
7
/**
8
 * Class with Knight logic
9
 */
10
class Knight {
11
    /**
12
     * init
13
     */
14
    constructor(color) {
15 8
        this.symbol = "N";
16 8
        this.moved = 0;
17 8
        this.active = true;
18 8
        this.color = color;
19 8
        this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"];
20 8
        this.img = this.color === "white" ? "w-knight" : "b-knight";
21
    }
22
23
    legalMove(x, y, nx, ny) {
24
        let xNum = this.rows.indexOf(x);
25
        let nxNum = this.rows.indexOf(nx);
26
27
        let xStep = Math.abs(xNum - nxNum);
28
        let yStep = Math.abs(y - ny)
29
30 6
        if (xStep == 2 && yStep == 1 || xStep == 1 && yStep == 2) {
31
            return true;
32
        }
33
34
        return false;
35
    }
36
}
37
38
39
module.exports = Knight;
40