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

src/old_chess/pieces/chess/Game.js   A

Size

Lines of Code 127

Duplication

Duplicated Lines 127
Ratio 100 %

Test Coverage

Coverage 8.51%

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A Game.js ➔ ??? 9 9 1

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 View Code Duplication
const Player = require("./Player");
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
const Board = require("./Board");
3
4
5
/**
6
 * The game module
7
 * @module
8
 */
9
"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
        this.p1 = new Player("white", "player1");
20
        this.p2 = new Player("black", "player2");
21
        this.board = new Board();
22
        this.gameBoard = this.board.board;
23
        this.turn = this.p1.color;
24
        this.lastMove = null;
25
        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
            this.bameBoard = preset;
37
        }
38
        //Pawns
39
        for (let i = 1; i <= 8; i++) {
40
            this.gameBoard["B"][i] = this.p1.pawns[i - 1];
41
        }
42
43
        for (let i = 1; i <= 8; i++) {
44
            this.gameBoard["G"][i] = this.p2.pawns[i - 1];
45
        }
46
47
        // Rooks
48
        this.gameBoard["A"][1] = this.p1.rooks[0];
49
        this.gameBoard["A"][8] = this.p1.rooks[1];
50
        this.gameBoard["H"][1] = this.p2.rooks[0];
51
        this.gameBoard["H"][8] = this.p2.rooks[1];
52
53
        // Knights
54
        this.gameBoard["A"][2] = this.p1.knights[0];
55
        this.gameBoard["A"][7] = this.p1.knights[1];
56
        this.gameBoard["H"][2] = this.p2.knights[0];
57
        this.gameBoard["H"][7] = this.p2.knights[1];
58
59
        // Bishops
60
        this.gameBoard["A"][3] = this.p1.bishops[0];
61
        this.gameBoard["A"][6] = this.p1.bishops[1];
62
        this.gameBoard["H"][3] = this.p2.bishops[0];
63
        this.gameBoard["H"][6] = this.p2.bishops[1];
64
65
        // King Queen
66
        this.gameBoard["A"][4] = this.p1.king;
67
        this.gameBoard["A"][5] = this.p1.queen;
68
        this.gameBoard["H"][4] = this.p2.king;
69
        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