Passed
Push — master ( add66c...3bf222 )
by Christofer
01:09
created

src/chess/Board.js   A

Size

Lines of Code 185

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 80.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 1
dl 0
loc 185
ccs 42
cts 52
cp 0.8077
rs 10
noi 5

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
            [{"row": key}, {"square": key + 1}, {"square": key + 2}, {"square": key + 3}, {"square": key + 4}, {"square": key + 5}, {"square": key + 6}, {"square": key + 7}, {"square": 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, nxNumber - 1) : row.slice(nxNumber, xNumber - 1);
76
        }
77
78 2
        console.log(list);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
79
        // loop squares and check type
80 2
        for (i = 0; i < list.length; i++) {
81 4
            if (list[i].piece.symbol !== "E") {
82 1
                return false;
83
            }
84
        }
85
86 1
        return true;
87
    }
88
89
    /**
90
     * Check if the is anything blocking the path between 2 squares
91
     *
92
     * @returns {bool} - true if free else false
93
     */
94
    checkDiagonal(x, y, nx, ny) {
95
        let xNumber, nxNumber, steps;
96 2
        steps = Math.abs(y - ny);
97 2
        xNumber = this.rows.indexOf(x)
98 2
        nxNumber = this.rows.indexOf(nx)
99
100 6
        if (xNumber < nxNumber && y < ny || xNumber > nxNumber && y > ny) {
101 2
            y = y > ny ? ny : y;
102 2
            xNumber = xNumber > nxNumber ? nxNumber : xNumber;
103 2
            for (let i = 1; i < steps; i++) {
104 2
                if (this.board[this.rows[xNumber + i]][y + i].piece.symbol !== "E") {
105 1
                    return false
106
                }
107
            }
108
        } else {
109 2
            y = y < ny ? ny : y;
110 2
            xNumber = xNumber > nxNumber ? nxNumber : xNumber;
111
            for (let i = 1; i < steps; i++) {
112 2
                if (this.board[this.rows[xNumber + i]][y - i].piece.symbol !== "E") {
113
                    return false
114
                }
115
            }
116
        }
117 1
        return true
118
    }
119
120
    /**
121
     * get a value for a specific square
122
     *
123
     * @returns {string}
124
     */
125
    getSquare(row, col) {
126 7
        return this.board[row][col];
127
    }
128
129
130
    /**
131
     * change the value of a square
132
     *
133
     * @returns {null}
134
     */
135
    updateSquare(row, col, value) {
136 4
        console.log(value);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
137
138 4
        this.board[row][col].piece = value;
139
    }
140
141
142
    /**
143
     * Move a piece to another square
144
     * @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...
145
     * old xy coordinates and new xy coordinates
146
     *
147
     */
148
    move(x, y, nx, ny) {
149 1
        let square = this.getSquare(x, y);
150
151 1
        this.updateSquare(nx, ny, square.piece);
152 1
        this.updateSquare(x, y, {color: "", symbol: "E"});
153
    }
154
155
156
    /**
157
     * Check that destination is no of the same color as mover
158
     * @return {bool}
159
     */
160
    checkDestination(turn, x, y) {
161
        const square = this.getSquare(x, y);
162
163 2
        if (turn === square.piece.color) {
164
            return false;
165
        }
166
167
        return true;
168
    }
169
170
171
    /**
172
     * Return board as array
173
     * @return {array}
174
     */
175
    getBoardArray() {
176
        let boardArray = [];
177
178
        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...
179
            for (let i = 1; i < this.board[key].length; i++) {
180
                boardArray.push(this.board[key][i]);
181
            }
182
        }
183
184
        return boardArray;
185
    }
186
}
187
188
189
module.exports = Board;
190