All files game.js

81.35% Statements 48/59
80% Branches 20/25
93.33% Functions 14/15
81.35% Lines 48/59

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                            458x   458x 458x                 458x         98x   5x     5x 5x           8x 8x     8x     8x 7x             98x   98x 98x 98x         98x 98x     98x 458x 458x 74x       98x   98x 8x 8x   5x       98x       1490x             458x 458x   458x 29312x 13012x                   458x 458x                                                                       458x 458x 458x 458x 458x 458x 458x 458x 458x 458x 458x          
/**
	Games contain the history of a board and the board itself.
 
	At time of writing this, the game is also intended to store some
	degree of information regarding the opponents and keys that
	could be used for storage, etc.
*/
import base64 from 'crypto-js/enc-base64.js'
import { Board } from './board.js';
import { EventEmitter } from 'events';
import md5 from 'crypto-js/md5.js';
import { SideType } from './piece.js';
 
function addToHistory (game) {
	return (ev) => {
		let
			hashCode = game.getHashCode(),
			move = new Move(
				ev.prevSquare,
				ev.postSquare,
				ev.capturedPiece,
				ev.algebraic,
				ev.castle,
				ev.enPassant,
				hashCode);
 
		game.moveHistory.push(move);
	};
}
 
function denotePromotionInHistory (game) {
	return () => {
		let
			latest = game.moveHistory[
			game.moveHistory.length - 1];
 
		Eif (latest) {
			latest.promotion = true;
		}
	};
}
 
function removeFromHistory (game) {
	return () => {
		game.moveHistory.pop();
 
		// find the previous move piece
		let m = game.moveHistory[game.moveHistory.length - 1];
 
		// update last moved piece
		if (m) {
			game.board.lastMovedPiece = m.piece;
		}
	};
}
 
export class Game extends EventEmitter {
	constructor (board) {
		super();
 
		this.board = board;
		this.captureHistory = [];
		this.moveHistory = [];
	}
 
	static create () {
		let
			board = Board.create(),
			game = new Game(board);
 
		// handle move and promotion events correctly
		board.on('move', (ev) => {
			addToHistory(game)(ev);
			if (ev && ev.capturedPiece) {
				game.captureHistory.push(ev.capturedPiece);
			}
		});
 
		board.on('promote', denotePromotionInHistory(game));
		
		board.on('undo', (ev) => {
			removeFromHistory(game)(ev);
			if (ev && ev.capturedPiece && game.captureHistory.length > 0) {
				// last move was a capture, remove it from capture history
				game.captureHistory.pop();
			}
		});
 
		return game;
	}
 
	getCurrentSide () {
		return this.moveHistory.length % 2 === 0 ?
			SideType.White :
			SideType.Black;
	}
 
	getHashCode () {
		let 
			i = 0,
			sum = '';
 
		for (i = 0; i < this.board.squares.length; i++) {
			if (this.board.squares[i].piece !== null) {
				sum += [
					this.board.squares[i].file,
					this.board.squares[i].rank,
					(this.board.squares[i].piece.side === SideType.White ? 'w' : 'b'),
					this.board.squares[i].piece.notation,
					(i < (this.board.squares.length - 1) ? '-' : '')].join('');
			}
		}
 
		// generate hash code for board
		let digest = md5(sum);
		return base64.stringify(digest);
	}
 
	static load (moveHistory) {
		let
			board = Board.create(),
			game = new Game(board),
			i = 0;
 
		// handle move and promotion events correctly
		board.on('move', (ev) => {
			addToHistory(game)(ev);
			if (ev && ev.capturedPiece) {
				game.captureHistory.push(ev.capturedPiece);
			}
		});
 
		board.on('promote', denotePromotionInHistory(game));
 
		// apply move history
		for (i = 0; i < moveHistory.length; i++) {
			board.move(
				board.getSquare(
					moveHistory[i].prevFile,
					moveHistory[i].prevRank),
				board.getSquare(
					moveHistory[i].postFile,
					moveHistory[i].postRank));
		}
 
		return game;
	}
}
 
export class Move {
	constructor (originSquare, targetSquare, capturedPiece, notation, castle, enPassant, hash) {
		this.algebraic = notation;
		this.capturedPiece = capturedPiece;
		this.castle = castle;
		this.enPassant = enPassant;
		this.hashCode = hash;
		this.piece = targetSquare.piece;
		this.promotion = false;
		this.postFile = targetSquare.file;
		this.postRank = targetSquare.rank;
		this.prevFile = originSquare.file;
		this.prevRank = originSquare.rank;
	}
}
 
export default { Game, Move };