Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 924x 924x 924x 924x 7x 7x 7x 5x 7x 1x 7x 4x 7x 1x 1x 1x 1x 924x 43x 43x 43x 43x 43x 924x 1x 1x 924x 2x 2x 1x 1x 1x 1x 924x 51x 51x 51x 51x 51x 924x 2x 2x 2x 2x 2x 924x 3x 3x 3x 3x 29x 29x 39x 924x 29x 29x 29x 29x 29x 29x 29x 924x 10x 10x 10x 10x 924x 924x 924x 105x 105x 105x 105x 1294x 1294x 105x 924x 10x 10x 10x 80x 640x 640x 640x 640x 10x 924x 41x 924x 51x 51x 51x 51x 51x 51x 51x 670x 670x 670x 51x 924x 51x 186x 924x 102x 102x 102x 102x 102x 102x 102x 924x 43x 924x 1x 1x 1x 924x 924x 924x 924x 924x 924x 924x 924x 924x 924x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x | import { PieceColorEnum } from "../piece/pieceColorEnum.js"; import { getPiece, piecesBuilder } from "../piece/piecesBuilder.js"; function createBoard() { let pieces = {}; function getPieces() { return pieces; } function setPieces(piecesParam) { pieces = piecesParam; } function tryMove(movementOrigin, movementDestination, playerColor) { if (!pieces[movementOrigin].isOfColor(playerColor)) { return "Invalid move: Attempting to move a wrong color piece."; } if (!pieces[movementOrigin].isPossibleMove(movementDestination, pieces)) { return getInvalidMovementError(pieces[movementOrigin].getFullName()); } let stateBeforeMoving = createMemento(); move(movementOrigin, movementDestination); if (isColorOnCheck(playerColor)) { setMemento(stateBeforeMoving); return "Invalid move: cannot end turn on check"; } } function move(movementOrigin, movementDestination) { pieces[movementDestination] = pieces[movementOrigin]; pieces[movementDestination].setPosition(movementDestination); createEmptyTile(movementOrigin); } function isStalemate(turnColor) { return getValidMovementNotCausingCheck(turnColor) === undefined; } function isOnCheckMate(playerColor) { if (isColorOnCheck(playerColor)) { console.log("possible checkmate"); console.log("is checkmate: " + isColorOnCheckMate(playerColor)); if (isColorOnCheckMate(playerColor)) return true; } return false; } function isColorOnCheck(color) { let attackpos = getAllAttackPositionsByColor(color.getOppositeColor()); let kingpos = getKingPositionByColor(color); return attackpos.has(kingpos); } function isColorOnCheckMate(color) { if (!isColorOnCheck(color)) return false; return getValidMovementNotCausingCheck(color) == undefined; } function getValidMovementNotCausingCheck(color) { const previousState = createMemento(); for (let coord of getAllCoordinatesByColor(color)) { for (let mov of movementsFromTheCoordinate(pieces[coord].getPosition())) { move(pieces[coord].getPosition(), mov); if (!isColorOnCheck(color)) { setMemento(previousState); return { origin: coord, destination: mov }; } setMemento(previousState); } } return undefined; } function movementsFromTheCoordinate(origin) { pieces[origin].updateCurrentPosition(origin, pieces); return pieces[origin].getPossibleMovements(); } function getBoardPieceNames() { let result = {}; for (let key in pieces) result[key] = pieces[key].getAbbreviation(); return result; } function getAllSquaresOfBlackPieces() { return getAllCoordinatesByColor(PieceColorEnum.Black); } function getAllEmptySquares() { return getAllCoordinatesByColor(PieceColorEnum.Empty); } function getAllCoordinatesByColor(color) { const coloredSquares = []; for (let coordinate in pieces) if (pieces[coordinate].isOfColor(color)) coloredSquares.push(coordinate); return coloredSquares; } function createMemento() { let boardString = ""; const boardNames = getBoardPieceNames(); for (let i=8; i>0; i--) for (let letter = 0; letter < "abcdefgh".length; letter++) { let currentID = "abcdefgh"[letter] + i.toString(); boardString += boardNames[currentID] + "-"; } return boardString; } function setMemento(memento) { setPieces(piecesBuilder(memento.split("-")).buildFromLayout()); } function getAllAttackPositionsByColor(color) { const coordinatesUnderAttack = new Set(); if (color.isEmpty()) return coordinatesUnderAttack; let colorPieces = getAllPiecesByColor(color); for (let colorPiece of colorPieces) { colorPiece.getAttackPositions(pieces) .forEach(coordinate => coordinatesUnderAttack.add(coordinate)); } return coordinatesUnderAttack; } function getKingPositionByColor(color) { return getAllPiecesByColor(color) .find((piece) => piece.getAbbreviation() === color.getAbbreviation() + 'K') .getPosition(); } function getAllPiecesByColor(color) { const coloredPieces = []; let allColorCoordinates = getAllCoordinatesByColor(color); for (let coloredCoordinate of allColorCoordinates) coloredPieces.push(pieces[coloredCoordinate]); return coloredPieces; } function createEmptyTile(coordinate) { pieces[coordinate] = getPiece('_', coordinate); } function getInvalidMovementError(pieceFullName) { return "Invalid " + pieceFullName + " movement"; } return { tryMove, isColorOnCheck, getValidMovementNotCausingCheck, movementsFromTheCoordinate, getBoardPieceNames, getAllSquaresOfBlackPieces, getAllEmptySquares, getAllCoordinatesByColor, createMemento, setMemento, getPieces, setPieces, isOnCheckMate, isStalemate }; } export { createBoard }; |