Passed
Push — master ( bb6f81...12291e )
by Christofer
57s
created

src/chess/Board.js   A

Size

Lines of Code 150

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 97.06%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
nc 1
dl 0
loc 150
rs 10
c 1
b 0
f 0
ccs 33
cts 34
cp 0.9706
noi 7

1 Function

Rating   Name   Duplication   Size   Complexity  
A Board.js ➔ ??? 0 16 1
1
/**
2
 * A Chess board module
3
 * @module
4
 */
5
"use strict";
6
7
/**
8
 * Board class contains methods for setting up a chess board
9
 */
10
class Board {
11
    /**
12
     * Prepare board properties and init empty board
13
     */
14
    constructor() {
15 6
        this.board = {
16
            "A": [],
17
            "B": [],
18
            "C": [],
19
            "D": [],
20
            "E": [],
21
            "F": [],
22
            "G": [],
23
            "H": [],
24
        };
25
26 6
        this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"];
27
28 6
        this.setUpEmptyTable();
29
    }
30
31
32
    /**
33
     * Set up empty board
34
     */
35
    setUpEmptyTable() {
36 6
        for (var key in this.board) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
37 48
            this.board[key] =
38
            [key, key + 1, key + 2, key + 3, key + 4, key + 5, key + 6, key + 7, key + 8];
39
        }
40
    }
41
42
43
    /**
44
     * get the full board
45
     *
46
     * @returns {object} - current board state
47
     */
48
    getBoard() {
49 1
        return this.board;
50
    }
51
52
    /**
53
     * Check if the is anything blocking the path between 2 squares
54
     *
55
     * @returns {mixed} - true if free or square Coo if false
56
     */
57
    checkRow(x, y, nx, ny) {
58 2
        const direction = x == nx ? "horisontal" : "vertical";
59
        let row, list, xNumber, nxNumber, i;
60
61 2
        if (direction === "horisontal") {
62 1
            row = this.board[x];
63
            // Slice array to get squares to check
64 2
            list = y < ny ? row.slice(y + 1, ny) : row.slice(ny + 1, y);
65
        } else {
66 1
            row = [];
67 1
            for (i = 1; i < this.rows.length; i++) {
68 8
                row.push(this.board[this.rows[i]][y]);
69
            }
70
            // get numeric value of x nx
71 1
            xNumber = this.rows.indexOf(x);
72 1
            nxNumber = this.rows.indexOf(nx);
73
74
            // Slice array to get squares to check
75 2
            list = xNumber < nxNumber ? row.slice(xNumber + 1, nxNumber) : row.slice(nxNumber + 1, xNumber);
76
        }
77
78
        // loop squares and check type
79 2
        for (i = 0; i < list.length; i++) {
80 4
            if (typeof list[i] !== "string") {
81 1
                return false;
82
            }
83
        }
84
85 1
        return true;
86
    }
87
88
    /**
89
     * Check if the is anything blocking the path between 2 squares
90
     *
91
     * @returns {mixed} - true if free or square Coo if false
92
     */
93
    checkDiagonal(x, y, nx, ny) {
0 ignored issues
show
Unused Code introduced by
The parameter nx is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter ny is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter x is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter y is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
94
95
    }
96
97
    /**
98
     * get a value for a specific square
99
     *
100
     * @returns {string}
101
     */
102
    getSquare(row, col) {
103 7
        return this.board[row][col];
104
    }
105
106
107
    /**
108
     * change the value of a square
109
     *
110
     * @returns {null}
111
     */
112
    updateSquare(row, col, value) {
113 4
        this.board[row][col] = value;
114
    }
115
116
117
    /**
118
     * Move a piece to another square
119
     * @param {int} - x, y, nx, ny
0 ignored issues
show
Documentation introduced by
The parameter - does not exist. Did you maybe forget to remove this comment?
Loading history...
120
     * old xy coordinates and new xy coordinates
121
     *
122
     */
123
    move(x, y, nx, ny) {
124 1
        const piece = this.getSquare(x, y);
125
126 1
        this.updateSquare(x, y, x + y);
127 1
        this.updateSquare(nx, ny, piece);
128
    }
129
130
131
    /**
132
     * Asciify board
133
     *
134
     * @returns {String}
135
     */
136
    getAsciiBoard() {
137 1
        let ascii = "";
138
139 1
        for (var key in this.board) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
140 8
            for (let i = 0; i < this.board[key].length; i++) {
141 72
                if (typeof this.board[key][i] === "object") {
142
                    ascii += this.board[key][i].symbol + this.board[key][i].color[0] +  " | "
143
                } else {
144 72
                    ascii += this.board[key][i] +  " | ";
145
                }
146
            }
147 8
            ascii += "\n";
148
        }
149 1
        return ascii;
150
    }
151
}
152
153
154
module.exports = Board;
155