| @@ 1-127 (lines=127) @@ | ||
| 1 | const Player = require("./Player"); |
|
| 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 |
|
| 32 | */ |
|
| 33 | init(preset = null) { |
|
| 34 | ||
| 35 | 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 | if (piece.color !== this.turn) { |
|
| 81 | console.log("Wrong player") |
|
| 82 | return false |
|
| 83 | } |
|
| 84 | ||
| 85 | 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()); |
|
| 103 | } |
|
| 104 | ||
| 105 | /** |
|
| 106 | * Set next player |
|
| 107 | * @return {null} |
|
| 108 | */ |
|
| 109 | nextPlayer() { |
|
| 110 | 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 | ||
| @@ 1-127 (lines=127) @@ | ||
| 1 | const Player = require("./Player"); |
|
| 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 |
|
| 32 | */ |
|
| 33 | init(preset = null) { |
|
| 34 | ||
| 35 | 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 | if (piece.color !== this.turn) { |
|
| 81 | console.log("Wrong player") |
|
| 82 | return false |
|
| 83 | } |
|
| 84 | ||
| 85 | 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()); |
|
| 103 | } |
|
| 104 | ||
| 105 | /** |
|
| 106 | * Set next player |
|
| 107 | * @return {null} |
|
| 108 | */ |
|
| 109 | nextPlayer() { |
|
| 110 | 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 | ||