API Docs for:
Show:

File: js/classes/pieces/Knight.js

/**
module contains classes of pieces
@module Pieces
**/ 

/**
* A Knight class, represents a Knight piece
* @class Knight
* @constructor
* @param {} color 	knight's color
* @param {} type 	type of the piece, e.g. "Knight" 
* @param {} X 		x coordinate on the board (0-7), 0 represents 'a' file, 7 represents 'h' file 
* @param {} Y 		y coordinate on the board (0-7), 0 represents 8-th rank, 7 represnts 1-st rank
* @param {} id 		id of the piece
*/
function Knight(color, type, X, Y, id) {
    /**
 	* Knight's color
 	* @property color
 	* @type String
 	*/
    this.color  = color;
	
	/**
 	* Knight's type
 	* @property type
 	* @type String
 	*/
    this.type   = type;
	
	/**
 	* x coordinate
 	* @property X
 	* @type Number
 	*/
    this.X      = X;
	
	/**
 	* y coordinate
 	* @property Y
 	* @type Number
 	*/
    this.Y      = Y;
	
	/**
 	* Knight's ID
 	* @property ID
 	* @type Number
 	*/
    this.ID     = id;
	
	/**
 	* notation symbol
 	* @property notationName
 	* @type String
 	*/
	this.notationName 	= "N";
	
	/**
     * get relative path to the piece image
     * @method getImageSource
     * @return {String} path to the piece image
     */
    this.getImageSource = function (){
        
        if(this.color == "white") {
            return "images/pieces/wknight.png";
        } else {
            return "images/pieces/bknight.png";
        }
    }
    
	/**
     * get piece legal moves
     * @method getLegalMoves
     * @return {Array} an array of (x,y) coordinates represents a legal moves for a piece
     */
    this.getLegalMoves = function() {
        legalMoves = [];
        
        legalMoves.push({x:this.X + 2, y:this.Y + 1});
        legalMoves.push({x:this.X + 2, y:this.Y - 1});
        legalMoves.push({x:this.X - 2, y:this.Y + 1});
        legalMoves.push({x:this.X - 2, y:this.Y - 1});
        legalMoves.push({x:this.X + 1, y:this.Y + 2});
        legalMoves.push({x:this.X - 1, y:this.Y + 2});
        legalMoves.push({x:this.X + 1, y:this.Y - 2});
        legalMoves.push({x:this.X - 1, y:this.Y - 2});
        
        for(var i = 0; i < legalMoves.length; i++) {
            if(legalMoves[i].x > 7 || legalMoves[i].y > 7 || legalMoves[i].x < 0 || legalMoves[i].y < 0) {
                legalMoves[i] = null;
            }
        }
        
        legalMoves = legalMoves.clean(null);

        parentLoop:
        for(var j = 0; j < legalMoves.length; j++) {
            for(var i = 0; i < board.pieces.length; i++) {
                var p = board.pieces[i];
                var m = legalMoves[j];
                
                if(legalMoves[j] == null) continue parentLoop;
                
                if(p.X == m.x && p.Y == m.y && p.color == this.color) {
                    legalMoves[j] = null;
                }
            }
        }
        
		legalMoves = legalMoves.clean(null);
        return preventCheck(this, legalMoves);
    }
    
	/**
     * get piece covered fields
     * @method getCoveredFields
     * @return {Array} an array of (x,y) coordinates represents a set of fields, covered by a piece 
     */
    this.getCoveredFields = function() {
        
        coveredFields = [
            {x:this.X + 2, y:this.Y + 1},
            {x:this.X + 2, y:this.Y - 1},
            {x:this.X - 2, y:this.Y + 1},
            {x:this.X - 2, y:this.Y - 1},
            {x:this.X + 1, y:this.Y + 2},
            {x:this.X - 1, y:this.Y + 2},
            {x:this.X + 1, y:this.Y - 2},
            {x:this.X - 1, y:this.Y - 2}
        ];
        
        for(var i = 0; i < coveredFields.length; i++) {
            if(coveredFields[i].x > 7 || coveredFields[i].y > 7 || coveredFields[i].x < 0 || coveredFields[i].y < 0) {
                coveredFields[i] = null;
            }
        }
        
        return coveredFields.clean(null);
    }
	
	/**
	 * moves piece to a new field
	 * @method move
	 * @param {Number} x x coordinate of destination
	 * @param {Number} y y coordinate of destination
	 * @return {Object} object represents a summary of the move 
	 */
	this.move = function(x, y) {
        if(!confirmMove(x,y,this)) return;
        return finishMove(x,y,this);
	}
	
	/**
	 * get notation symbol of the piece
	 * @method getNotationSymbol
	 * @return {String} notation symbol
	 */
	this.getNotationSymbol = function() {
		if(this.color == "white") {
			return this.notationName.toUpperCase();
		} else {
			return this.notationName.toLowerCase();
		}
	}
}