All files pieceValidation.js

95.27% Statements 121/127
93.75% Branches 90/96
87.5% Functions 14/16
96.03% Lines 121/126

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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356                                10084x 10084x 10084x 10084x 10084x 10084x 10084x                   10084x   915x   411x   3250x   3862x   674x   972x               10079x               10079x           10079x   20061x 20061x 20061x 20061x   20061x 23417x     23417x   23417x 16143x     23417x 8896x   14521x 14521x         10079x 2x     10077x   10077x 5915x             10077x 2054x             10077x 2054x 2054x       10077x 1996x 1996x 1996x 1996x       10077x     10077x                 915x     915x 915x 915x           411x     411x 411x 411x 411x 411x 411x                   3250x     3250x 3250x           3249x     3249x     3249x     3249x     3249x 3249x 3249x   3249x 2893x       2893x         3249x 2981x       2981x         3249x 2779x       2779x         3249x 2867x       2867x         3249x 23040x   20854x 20854x 14585x                 3862x     3862x 3862x 3862x             3861x 3861x 3861x 3861x                     3861x   7722x 7722x 260x         3861x     2557x           2557x 2438x       1304x     375x     375x   375x   750x 750x           5x                           674x     674x 674x 674x 674x 674x 674x           972x     972x 972x 972x 972x 972x          
/**
	The general idea behind PieceValidation is to examine an individual piece
	and determine (with the information available from about that single piece)
	what move options are available for that piece.
 
	The PieceValidation doesn't alter any properties of the piece, the board
	or any squares. Additionally, the PieceValidation is suitable for 1 phase of
	the evaluation of viable move options for a piece... the BoardValidation
	component handles the overall evaluation of what moves are possible for the
	board in its current state.
*/
import { PieceType, SideType } from './piece.js';
import { NeighborType } from './board.js';
 
export class PieceValidation {
	constructor (board) {
		this.allowBackward = false;
		this.allowDiagonal = false;
		this.allowForward = false;
		this.allowHorizontal = false;
		this.board = board;
		this.type = null;
		this.repeat = 0;
	}
 
	applySpecialValidation () {
		// do nothing...
		// overridden in the concrete validation classes
		// where special logic is required
	}
 
	static create (piece, board) {
		switch (piece) {
			case PieceType.Bishop:
				return new BishopValidation(board);
			case PieceType.King:
				return new KingValidation(board);
			case PieceType.Knight:
				return new KnightValidation(board);
			case PieceType.Pawn:
				return new PawnValidation(board);
			case PieceType.Queen:
				return new QueenValidation(board);
			case PieceType.Rook:
				return new RookValidation(board);
			default:
				return null;
		}
	}
 
	start (src, callback) {
		// ensure callback is set
		callback = callback || ((err, destinationSquares) => new Promise((resolve, reject) => {
			if (err) {
				return reject(err);
			}
 
			return resolve(destinationSquares);
		}));
 
		let opt = {
			destSquares : [],
			origin : src,
			piece : src ? src.piece : null
		};
 
		const findMoveOptions = function (b, r, n) {
			let
				block = false,
				capture = false,
				currentSquare = b.getNeighborSquare(opt.origin, n),
				i = 0;
 
			while (currentSquare && i < r) {
				block = currentSquare.piece !== null &&
					(opt.piece.type === PieceType.Pawn ||
						currentSquare.piece.side === opt.piece.side);
				capture = (currentSquare.piece && !block);
 
				if (!block) {
					opt.destSquares.push(currentSquare);
				}
 
				if (capture || block) {
					currentSquare = null;
				} else {
					currentSquare = b.getNeighborSquare(currentSquare, n);
					i++;
				}
			}
		};
 
		if (!opt.piece || opt.piece.type !== this.type) {
			return callback(new Error('piece is invalid'));
		}
 
		Eif (this.board && opt.origin) {
			// forward squares
			if (this.allowForward) {
				findMoveOptions(this.board, this.repeat,
					opt.piece.side === SideType.White ?
							NeighborType.Above :
							NeighborType.Below);
			}
 
			// backward squares
			if (this.allowBackward) {
				findMoveOptions(this.board, this.repeat,
					opt.piece.side === SideType.White ?
							NeighborType.Below :
							NeighborType.Above);
			}
 
			// horizontal squares
			if (this.allowHorizontal) {
				findMoveOptions(this.board, this.repeat, NeighborType.Left);
				findMoveOptions(this.board, this.repeat, NeighborType.Right);
			}
 
			// diagonal squares
			if (this.allowDiagonal) {
				findMoveOptions(this.board, this.repeat, NeighborType.AboveLeft);
				findMoveOptions(this.board, this.repeat, NeighborType.BelowRight);
				findMoveOptions(this.board, this.repeat, NeighborType.BelowLeft);
				findMoveOptions(this.board, this.repeat, NeighborType.AboveRight);
			}
 
			// apply additional validation logic
			this.applySpecialValidation(opt);
 
			// callback
			return callback(null, opt.destSquares);
		}
 
		return callback(new Error('board is invalid'));
	}
}
 
export class BishopValidation extends PieceValidation {
	constructor (board) {
		super(board);
 
		// base validation properties
		this.allowDiagonal = true;
		this.type = PieceType.Bishop;
		this.repeat = 8;
	}
}
 
export class KingValidation extends PieceValidation {
	constructor (board) {
		super(board);
 
		// base validation properties
		this.allowBackward = true;
		this.allowDiagonal = true;
		this.allowForward = true;
		this.allowHorizontal = true;
		this.type = PieceType.King;
		this.repeat = 1;
	}
 
	applySpecialValidation () {
		// check for castle?
	}
}
 
export class KnightValidation extends PieceValidation {
	constructor (board) {
		super(board);
 
		// base validation properties
		this.type = PieceType.Knight;
		this.repeat = 1;
	}
 
	applySpecialValidation (opt) {
		// add knight move options
		let
			aboveLeft = this.board.getNeighborSquare(
				opt.origin,
				NeighborType.AboveLeft),
			aboveRight = this.board.getNeighborSquare(
				opt.origin,
				NeighborType.AboveRight),
			belowLeft = this.board.getNeighborSquare(
				opt.origin,
				NeighborType.BelowLeft),
			belowRight = this.board.getNeighborSquare(
				opt.origin,
				NeighborType.BelowRight),
			i = 0,
			p = null,
			squares = [];
 
		if (aboveLeft) {
			squares.push(this.board.getNeighborSquare(
				aboveLeft,
				NeighborType.Above));
 
			squares.push(this.board.getNeighborSquare(
				aboveLeft,
				NeighborType.Left));
		}
 
		if (aboveRight) {
			squares.push(this.board.getNeighborSquare(
				aboveRight,
				NeighborType.Above));
 
			squares.push(this.board.getNeighborSquare(
				aboveRight,
				NeighborType.Right));
		}
 
		if (belowLeft) {
			squares.push(this.board.getNeighborSquare(
				belowLeft,
				NeighborType.Below));
 
			squares.push(this.board.getNeighborSquare(
				belowLeft,
				NeighborType.Left));
		}
 
		if (belowRight) {
			squares.push(this.board.getNeighborSquare(
				belowRight,
				NeighborType.Below));
 
			squares.push(this.board.getNeighborSquare(
				belowRight,
				NeighborType.Right));
		}
 
		for (i = 0; i < squares.length; i++) {
			if (squares[i]) {
				// check for enemy piece on square
				p = squares[i] ? squares[i].piece : null;
				if (!p || p.side !== opt.piece.side) {
					opt.destSquares.push(squares[i]);
				}
			}
		}
	}
}
 
export class PawnValidation extends PieceValidation {
	constructor (board) {
		super(board);
 
		// base validation properties
		this.allowForward = true;
		this.type = PieceType.Pawn;
		this.repeat = 1;
	}
 
	/* eslint no-magic-numbers:0 */
	applySpecialValidation (opt) {
		// check for capture
		let
			i = 0,
			p = null,
			sq = null,
			squares = [
				this.board.getNeighborSquare(opt.origin,
					opt.piece.side === SideType.White ?
							NeighborType.AboveLeft :
							NeighborType.BelowLeft),
				this.board.getNeighborSquare(opt.origin,
					opt.piece.side === SideType.White ?
							NeighborType.AboveRight :
							NeighborType.BelowRight)];
 
		// check for capture
		for (i = 0; i < squares.length; i++) {
			// check for enemy piece on square
			p = squares[i] ? squares[i].piece : null;
			if (p && p.side !== opt.piece.side) {
				opt.destSquares.push(squares[i]);
			}
		}
 
		// check for double square first move
		if (opt.piece.moveCount === 0 &&
				opt.destSquares.length && // Fix for issue #15 (originally looked for length of 1)
				opt.destSquares[0].piece === null) { // Fix for issue #1
			sq = this.board.getNeighborSquare(
				opt.destSquares[0],
				opt.piece.side === SideType.White ?
						NeighborType.Above :
						NeighborType.Below);
 
			if (!sq.piece) {
				opt.destSquares.push(sq);
			}
 
		// check for en passant
		} else if (opt.origin.rank ===
				(opt.piece.side === SideType.White ? 5 : 4)) {
			// get squares left & right of pawn
			squares = [
				this.board.getNeighborSquare(opt.origin, NeighborType.Left),
				this.board.getNeighborSquare(opt.origin, NeighborType.Right)];
			i = 0;
 
			for (i = 0; i < squares.length; i++) {
				// check for pawn on square
				p = squares[i] ? squares[i].piece : null;
				if (p &&
						p.type === PieceType.Pawn &&
						p.side !== opt.piece.side &&
						p.moveCount === 1 &&
						this.board.lastMovedPiece === p) {
 
					opt.destSquares.push(
						this.board.getNeighborSquare(
							squares[i],
							p.side === SideType.Black ?
									NeighborType.Above :
									NeighborType.Below));
				}
			}
		}
	}
}
 
export class QueenValidation extends PieceValidation {
	constructor (board) {
		super(board);
 
		// base validation properties
		this.allowBackward = true;
		this.allowDiagonal = true;
		this.allowForward = true;
		this.allowHorizontal = true;
		this.repeat = 8;
		this.type = PieceType.Queen;
	}
}
 
export class RookValidation extends PieceValidation {
	constructor (board) {
		super(board);
 
		// base validation properties
		this.allowBackward = true;
		this.allowForward = true;
		this.allowHorizontal = true;
		this.repeat = 8;
		this.type = PieceType.Rook;
	}
}
 
export default { PieceValidation };