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
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Init board with all player pieces |
30
|
|
|
* @param {object} - Preset gameboard Object |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
init(preset = null) { |
33
|
|
|
|
34
|
2 |
|
if (typeof preset !== "null") { |
35
|
2 |
|
this.bameBoard = preset; |
36
|
|
|
} |
37
|
|
|
//Pawns |
38
|
2 |
|
for (let i = 1; i <= 8; i++) { |
39
|
16 |
|
this.gameBoard["B"][i] = this.p1.pawns[i - 1]; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
for (let i = 1; i <= 8; i++) { |
43
|
16 |
|
this.gameBoard["G"][i] = this.p2.pawns[i - 1]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Rooks |
47
|
2 |
|
this.gameBoard["A"][1] = this.p1.rooks[0]; |
48
|
2 |
|
this.gameBoard["A"][8] = this.p1.rooks[1]; |
49
|
2 |
|
this.gameBoard["H"][1] = this.p2.rooks[0]; |
50
|
2 |
|
this.gameBoard["H"][8] = this.p2.rooks[1]; |
51
|
|
|
|
52
|
|
|
// Knights |
53
|
2 |
|
this.gameBoard["A"][2] = this.p1.knights[0]; |
54
|
2 |
|
this.gameBoard["A"][7] = this.p1.knights[1]; |
55
|
2 |
|
this.gameBoard["H"][2] = this.p2.knights[0]; |
56
|
2 |
|
this.gameBoard["H"][7] = this.p2.knights[1]; |
57
|
|
|
|
58
|
|
|
// Bishops |
59
|
2 |
|
this.gameBoard["A"][3] = this.p1.bishops[0]; |
60
|
2 |
|
this.gameBoard["A"][6] = this.p1.bishops[1]; |
61
|
2 |
|
this.gameBoard["H"][3] = this.p2.bishops[0]; |
62
|
2 |
|
this.gameBoard["H"][6] = this.p2.bishops[1]; |
63
|
|
|
|
64
|
|
|
// King Queen |
65
|
2 |
|
this.gameBoard["A"][4] = this.p1.king; |
66
|
2 |
|
this.gameBoard["A"][5] = this.p1.queen; |
67
|
2 |
|
this.gameBoard["H"][4] = this.p2.king; |
68
|
2 |
|
this.gameBoard["H"][5] = this.p2.queen; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Move a boardpiece and call helpfunctions to check legality of the move |
74
|
|
|
* @return {bool}, True if move executed correct otherwise False |
75
|
|
|
*/ |
76
|
|
|
movePiece(x, y, nx, ny) { |
77
|
|
|
const piece = this.board.getSquare(x, y); |
78
|
|
|
|
79
|
2 |
|
if (piece.color !== this.turn) { |
80
|
|
|
return false |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
this.board.move(x, y, nx, ny) |
84
|
|
|
this.lastMove = [x, y, nx, ny] |
85
|
|
|
this.nextPlayer(); |
86
|
|
|
console.log(this.status()); |
|
|
|
|
87
|
|
|
return true |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set next player |
92
|
|
|
* @return {null} |
93
|
|
|
*/ |
94
|
|
|
nextPlayer() { |
95
|
2 |
|
this.turn = this.turn === "black" ? "white" : "black"; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Show the status of the game |
100
|
|
|
* @return {object} |
101
|
|
|
*/ |
102
|
|
|
status() { |
103
|
|
|
return { |
104
|
|
|
"Player to act": this.turn, |
105
|
|
|
"White name": this.p1.name, |
106
|
|
|
"Black name": this.p2.name, |
107
|
|
|
"last move": this.lastMove |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
module.exports = new Game(); |
113
|
|
|
|