Passed
Push — master ( 74547d...add66c )
by Christofer
01:15
created

src/chess/Game.js   A

Size

Lines of Code 127

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 76.6%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
nc 1
dl 0
loc 127
ccs 36
cts 47
cp 0.766
rs 10
noi 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A Game.js ➔ ??? 0 9 1
1 1
const Player = require("./Player");
2 1
const Board = require("./Board");
3
4
5
/**
6
 * The game module
7
 * @module
8
 */
9 1
"use strict";
10
11
/**
12
 * The main module that executes the game
13
 */
14
class Game {
15
    /**
16
     * Init Game with board, peices, and players
17
     */
18
    constructor() {
19 1
        this.p1 = new Player("white", "player1");
20 1
        this.p2 = new Player("black", "player2");
21 1
        this.board = new Board();
22 1
        this.gameBoard = this.board.board;
23 1
        this.turn = this.p1.color;
24 1
        this.lastMove = null;
25 1
        this.NrOfmoves = 0;
26
    }
27
28
29
    /**
30
     * Init board with all player pieces
31
     * @param {object} - Preset gameboard Object
0 ignored issues
show
Documentation introduced by
The parameter - does not exist. Did you maybe forget to remove this comment?
Loading history...
32
     */
33
    init(preset = null) {
34
35 2
        if (typeof preset !== "null") {
36 2
            this.bameBoard = preset;
37
        }
38
        //Pawns
39 2
        for (let i = 1; i <= 8; i++) {
40 16
            this.gameBoard["B"][i] = this.p1.pawns[i - 1];
41
        }
42
43 2
        for (let i = 1; i <= 8; i++) {
44 16
            this.gameBoard["G"][i] = this.p2.pawns[i - 1];
45
        }
46
47
        // Rooks
48 2
        this.gameBoard["A"][1] = this.p1.rooks[0];
49 2
        this.gameBoard["A"][8] = this.p1.rooks[1];
50 2
        this.gameBoard["H"][1] = this.p2.rooks[0];
51 2
        this.gameBoard["H"][8] = this.p2.rooks[1];
52
53
        // Knights
54 2
        this.gameBoard["A"][2] = this.p1.knights[0];
55 2
        this.gameBoard["A"][7] = this.p1.knights[1];
56 2
        this.gameBoard["H"][2] = this.p2.knights[0];
57 2
        this.gameBoard["H"][7] = this.p2.knights[1];
58
59
        // Bishops
60 2
        this.gameBoard["A"][3] = this.p1.bishops[0];
61 2
        this.gameBoard["A"][6] = this.p1.bishops[1];
62 2
        this.gameBoard["H"][3] = this.p2.bishops[0];
63 2
        this.gameBoard["H"][6] = this.p2.bishops[1];
64
65
        // King Queen
66 2
        this.gameBoard["A"][4] = this.p1.king;
67 2
        this.gameBoard["A"][5] = this.p1.queen;
68 2
        this.gameBoard["H"][4] = this.p2.king;
69 2
        this.gameBoard["H"][5] = this.p2.queen;
70
    }
71
72
73
    /**
74
    * Move a boardpiece and call helpfunctions to check legality of the move
75
    * @return {Bool}, False if illegal move
76
    */
77
    movePiece(x, y, nx, ny) {
78
        const piece = this.board.getSquare(x, y);
79
80 2
        if (piece.color !== this.turn) {
81
            console.log("Wrong player")
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...
82
            return false
83
        }
84
85 2
        if (!this.board.checkDestination(this.turn, nx, ny)) {
86
            console.log("destination is not valid")
87
            return false
88
        }
89
90
        // if (piece.symbol === "P" || piece.symbol === "K" || piece.symbol === "N") {
91
        //
92
        // } else if (piece.symbol === "B") {
93
        //     this.board.checkDiagonal(x, y, nx, ny)
94
        // } else if (piece.symbol === "R") {
95
        //     this.board.checkRow(x, y, nx, ny)
96
        // }
97
98
        this.board.move(x, y, nx, ny)
99
        this.lastMove = [x, y, nx, ny]
100
        this.nextPlayer();
101
        this.NrOfmoves += 1;
102
        console.log(this.status());
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
103
    }
104
105
    /**
106
    * Set next player
107
    * @return {null}
108
    */
109
    nextPlayer() {
110 2
        this.turn = this.turn === "black" ? "white" : "black";
111
    }
112
113
    /**
114
    * Show the status of the game
115
    * @return {object}
116
    */
117
    status() {
118
        return {
119
            "Player to act": this.turn,
120
            "White name": this.p1.name,
121
            "Black name": this.p2.name,
122
            "last move": this.lastMove
123
        }
124
    }
125
}
126
127
module.exports = new Game();
128