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 board with all player pieces |
||
17 | * @param {object} - Preset gameboard Object |
||
0 ignored issues
–
show
Documentation
introduced
by
![]() |
|||
18 | */ |
||
19 | init(preset = null, name1 = "player1", name2 = "player2") { |
||
20 | 2 | this.p1 = new Player("white", name1); |
|
21 | 2 | this.p2 = new Player("black", name2); |
|
22 | 2 | this.board = new Board(); |
|
23 | 2 | this.gameBoard = this.board.board; |
|
24 | 2 | this.turn = this.p1.color; |
|
25 | 2 | this.lastMove = null; |
|
26 | 2 | this.NrOfmoves = 0; |
|
27 | |||
28 | 2 | if (typeof preset !== "null") { |
|
29 | 2 | this.bameBoard = preset; |
|
30 | } |
||
31 | //Pawns |
||
32 | 2 | for (let i = 1; i <= 8; i++) { |
|
33 | 16 | this.gameBoard["B"][i].piece = this.p1.pawns[i - 1]; |
|
34 | } |
||
35 | |||
36 | 2 | for (let i = 1; i <= 8; i++) { |
|
37 | 16 | this.gameBoard["G"][i].piece = this.p2.pawns[i - 1]; |
|
38 | } |
||
39 | |||
40 | 2 | for (let i = 1; i <= 8; i++) { |
|
41 | 16 | this.gameBoard["C"][i].piece = {color: "", symbol: "E"}; |
|
42 | 16 | this.gameBoard["D"][i].piece = {color: "", symbol: "E"}; |
|
43 | 16 | this.gameBoard["E"][i].piece = {color: "", symbol: "E"}; |
|
44 | 16 | this.gameBoard["F"][i].piece = {color: "", symbol: "E"}; |
|
45 | } |
||
46 | |||
47 | // Rooks |
||
48 | 2 | this.gameBoard["A"][1].piece = this.p1.rooks[0]; |
|
49 | 2 | this.gameBoard["A"][8].piece = this.p1.rooks[1]; |
|
50 | 2 | this.gameBoard["H"][1].piece = this.p2.rooks[0]; |
|
51 | 2 | this.gameBoard["H"][8].piece = this.p2.rooks[1]; |
|
52 | |||
53 | // Knights |
||
54 | 2 | this.gameBoard["A"][2].piece = this.p1.knights[0]; |
|
55 | 2 | this.gameBoard["A"][7].piece = this.p1.knights[1]; |
|
56 | 2 | this.gameBoard["H"][2].piece = this.p2.knights[0]; |
|
57 | 2 | this.gameBoard["H"][7].piece = this.p2.knights[1]; |
|
58 | |||
59 | // Bishops |
||
60 | 2 | this.gameBoard["A"][3].piece = this.p1.bishops[0]; |
|
61 | 2 | this.gameBoard["A"][6].piece = this.p1.bishops[1]; |
|
62 | 2 | this.gameBoard["H"][3].piece = this.p2.bishops[0]; |
|
63 | 2 | this.gameBoard["H"][6].piece = this.p2.bishops[1]; |
|
64 | |||
65 | // King Queen |
||
66 | 2 | this.gameBoard["A"][4].piece = this.p1.king; |
|
67 | 2 | this.gameBoard["A"][5].piece = this.p1.queen; |
|
68 | 2 | this.gameBoard["H"][4].piece = this.p2.king; |
|
69 | 2 | this.gameBoard["H"][5].piece = 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 | let square = this.board.getSquare(x, y); |
||
79 | let nSquare = this.board.getSquare(nx, ny); |
||
80 | let take; |
||
81 | |||
82 | 4 | if (nSquare.piece.symbol !== "E" && nSquare.piece.color !== square.piece.color) { |
|
83 | console.log("Take") |
||
0 ignored issues
–
show
|
|||
84 | take=true |
||
85 | } |
||
86 | |||
87 | 2 | if (square.piece.symbol === "E") { |
|
88 | console.log("no piece to move"); |
||
89 | return false |
||
90 | } |
||
91 | |||
92 | 2 | if (!square.piece.legalMove(x, y, nx, ny, take)) { |
|
0 ignored issues
–
show
|
|||
93 | console.log("piece cant move like that"); |
||
94 | return false; |
||
95 | } |
||
96 | |||
97 | 2 | if (square.piece.color !== this.turn) { |
|
98 | console.log("Wrong player") |
||
99 | return false |
||
100 | } |
||
101 | |||
102 | 2 | if (!this.board.checkDestination(this.turn, nx, ny)) { |
|
103 | console.log("destination is not valid") |
||
104 | return false |
||
105 | } |
||
106 | |||
107 | |||
108 | 2 | if (square.piece.symbol === "B") { |
|
109 | 2 | if (!this.board.checkDiagonal(x, y, nx, ny)) { |
|
110 | console.log("diagonal blocked"); |
||
111 | return false |
||
112 | } |
||
113 | 2 | } else if (square.piece.symbol === "R") { |
|
114 | 2 | if (!this.board.checkRow(x, y, nx, ny)) { |
|
115 | console.log("Row blocked"); |
||
116 | return false |
||
117 | } |
||
118 | 2 | } else if (square.piece.symbol === "Q") { |
|
119 | let direction; |
||
120 | 4 | direction = (x === nx || y == ny) ? "row" : "diagonal"; |
|
121 | 4 | if (direction === "row" && !this.board.checkRow(x, y, nx, ny)) { |
|
122 | console.log("Row blocked"); |
||
123 | return false |
||
124 | } |
||
125 | 4 | if (direction === "diagonal" && !this.board.checkDiagonal(x, y, nx, ny)) { |
|
126 | console.log("diagonal blocked"); |
||
127 | return false |
||
128 | } |
||
129 | } |
||
130 | |||
131 | this.board.move(x, y, nx, ny); |
||
132 | this.lastMove = [x, y, nx, ny]; |
||
133 | this.gameBoard[nx][ny].piece.moved += 1; |
||
134 | this.nextPlayer(); |
||
135 | this.NrOfmoves += 1; |
||
0 ignored issues
–
show
|
|||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Set next player |
||
140 | * @return {null} |
||
141 | */ |
||
142 | nextPlayer() { |
||
143 | 2 | this.turn = this.turn === "black" ? "white" : "black"; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Show the status of the game |
||
148 | * @return {object} |
||
149 | */ |
||
150 | status() { |
||
151 | return { |
||
152 | "Player to act": this.turn, |
||
153 | "White name": this.p1.name, |
||
154 | "Black name": this.p2.name, |
||
155 | "Last move": this.lastMove, |
||
156 | "Board": this.board.getBoardArray() |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | module.exports = new Game(); |
||
162 |