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

Size

Lines of Code 52

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 61.9%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 1
dl 0
loc 52
ccs 13
cts 21
cp 0.619
rs 10
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Pawn.js ➔ ??? 0 8 2
1
/**
2
 * Pawn module
3
 * @module
4
 */
5
"use strict";
6
7
/**
8
 * Class with Pawn logic
9
 */
10
class Pawn {
11
    /**
12
     * init
13
     */
14
    constructor(color) {
15 32
        this.symbol = "P";
16 32
        this.moved = 0;
17 32
        this.active = true;
18 32
        this.color = color;
19 32
        this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"];
20 32
        this.img = this.color === "white" ? "w-pawn" : "b-pawn";
21
    }
22
23
    legalMove(x, y, nx, ny, take=false) {
24 2
        let direction = this.color === "black" ? "up" : "down";
25
26
        let xNum = this.rows.indexOf(x);
27
        let nxNum = this.rows.indexOf(nx);
28
29
        let xStep = xNum - nxNum;
30
        let yStep = Math.abs(y - ny);
31
32 2
        if (take === true) {
33 8
            if (direction === "up" && xStep === 1 && yStep === 1
34
                || direction === "down" && xStep === -1 && yStep === 1) {
35
                return true;
36
            }
37
        } else {
38 2
            if (this.moved === 0) {
39 8
                if (direction === "up" && xStep === 2 && yStep === 0
40
                    || direction === "down" && xStep === -2 && yStep === 0) {
41
                    return true;
42
                }
43
            }
44
45 8
            if (direction === "up" && xStep === 1 && yStep === 0
46
                || direction === "down" && xStep === -1 && yStep === 0) {
47
                return true;
48
            }
49
        }
50
51
        return false;
52
    }
53
}
54
55
56
module.exports = Pawn;
57