Exemple #1
0
   Game.prototype.validateMove = function(pgnMove, team){
      try{
         if(!this.validateTime()){
            return {valid: false, desc: 'Timeout. ' +   + ' wins!'};
         }
         if(team === this.turn ){
            if(pgnMove === 'O-O-O' || pgnMove === 'O-O'){
               var valid = this.canCastle(pgnMove, team);
               return {valid: valid, desc: valid ? 'Nice Castle; man.' : 'Can Not Castle'};
            }
            var spots = pgnMove.split(/[x-]/i);
            var spot_from = Utils.pgnSqrToCoords(spots[0]);
            var spot_to = Utils.pgnSqrToCoords(spots[1]);

            //Validate the move is on the board
            if(Utils.onBoard(spot_from) && Utils.onBoard(spot_to)){
               //Check that there is a piece at the sqr you are moving from
               if(this.board.squares[spot_from.x][spot_from.y].occupied){
                  var piece = this.getPieceFromCoord(spot_from.x, spot_from.y);
                  if(piece.color === this.turn){
                     var moveSquares = this.getMoveSquaresForPiece(piece, team);
                     if(Utils.isSqrInAry(moveSquares, spot_to)){
                        if(!this.moveResultsInCheck(piece, this.board.squares[spot_to.x][spot_to.y], team)){
                           return {valid: true, desc:'Go for it kid'}; 
                        }else{
                           return {valid: false, desc:"Move would result in check for " + Utils.otherTeam(team)};
                        }
                     }else{
                        return {valid: false, desc:"Move wasn't in moveSquares."};
                     }
                  }else{
                     return {valid: false, desc:'The piece moved belongs to the other team.'};
                  }
               }else{
                  return {valid: false, desc:'No piece where you started'};
               }
            }else{
               return {valid: false, desc: 'Move not on board'};
            }
         }else{
            return {valid: false, desc: "It's not " + team  + "'s move."};
         }
      }catch(ex){
         return {valid: false, desc: 'Exception: \n' + ex.message};
      }
   }
Exemple #2
0
 Game.prototype.marchUntilPiece = function(piece, direction, n){
    squares = [];
    var x = piece.xpos;
    var y = piece.ypos;
    var blocked = false;
    if(direction.up_down){
       //March Right
       for(var i = 1; i <= n && Utils.onBoard({x: x + i, y:y}) && !blocked; i++){
          if(this.board.squares[x + i][y].occupied){
             blocked = true;
          }
          //Make sure we don't include pieces with the same color in the results
          if(!this.board.squares[x + i][y].occupied || this.board.squares[x + i][y].occupied && this.board.squares[x + i][y].piece.color !== piece.color){
             squares.push(this.board.squares[x + i][y]);
          }
       }
       //March Left
       blocked = false;
       for(var i = 1; i <= n && Utils.onBoard({x: x - i, y:y}) && !blocked; i++){
          if(this.board.squares[x - i][y].occupied){ 
             blocked = true;
          }
          if(!this.board.squares[x - i][y].occupied || this.board.squares[x - i][y].occupied && this.board.squares[x - i][y].piece.color !== piece.color){
             squares.push(this.board.squares[x - i][y]);
          }
       }
       //March Up
       blocked = false;
       for(var i = 1; i <= n && Utils.onBoard({x: x, y: y + i}) && !blocked; i++){
          if(this.board.squares[x][y + i].occupied){
             blocked = true;
          }
          if(!this.board.squares[x][y + i].occupied || this.board.squares[x][y + i].occupied && this.board.squares[x][y + i].piece.color !== piece.color){
             squares.push(this.board.squares[x][y + i]);
          }
       }
       //March Down
       blocked = false;
       for(var i = 1; i <= n && Utils.onBoard({x: x, y: y - i}) && !blocked; i++){
          if(this.board.squares[x][y - i].occupied){
             blocked = true;
          }
          if(!this.board.squares[x][y - i].occupied || this.board.squares[x][y - i].occupied && this.board.squares[x][y - i].piece.color !== piece.color){
             squares.push(this.board.squares[x][y - i]);
          }
       }
    }
    if(direction.diag){
       //March Up-Right
       blocked = false;
       for(var i = 1; i <= n && Utils.onBoard({x: x + i, y: y + i}) && !blocked; i++){
          if(this.board.squares[x + i][y + i].occupied){
             blocked = true;
          }
          if(!this.board.squares[x + i][y + i].occupied || this.board.squares[x + i][y + i].occupied && this.board.squares[x + i][y + i].piece.color !== piece.color){
             squares.push(this.board.squares[x + i][y + i]);
          }
       }
       //March Up-Left
       blocked = false;
       for(var i = 1; i <= n && Utils.onBoard({x: x - i, y: y + i}) && !blocked; i++){
          if(this.board.squares[x - i][y + i].occupied){
             blocked = true;
          }
          if(!this.board.squares[x - i][y + i].occupied || this.board.squares[x - i][y + i].occupied && this.board.squares[x - i][y + i].piece.color !== piece.color){
             squares.push(this.board.squares[x - i][y + i]);
          }
       }
       //March Down-Left
       blocked = false;
       for(var i = 1; i <= n && Utils.onBoard({x: x - i, y: y - i}) && !blocked; i++){
          if(this.board.squares[x - i][y - i].occupied){
             blocked = true;
          }
          if(!this.board.squares[x - i][y - i].occupied || this.board.squares[x - i][y - i].occupied && this.board.squares[x - i][y - i].piece.color !== piece.color){
             squares.push(this.board.squares[x - i][y - i]);
          }
       }
       //March Down-Right
       blocked = false;
       for(var i = 1; i <= n && Utils.onBoard({x: x + i, y: y - i}) && !blocked; i++){
          if(this.board.squares[x + i][y - i].occupied){
             blocked = true;
          }
          if(!this.board.squares[x + i][y - i].occupied || this.board.squares[x + i][y - i].occupied && this.board.squares[x + i][y - i].piece.color !== piece.color){
             squares.push(this.board.squares[x + i][y - i]);
          }
       }
    }
    return squares;
 }
Exemple #3
0
   Game.prototype.getMoveSquaresForPiece = function(piece, team){
      var squares = [];
      var x = piece.xpos;
      var y = piece.ypos;
      switch(piece.name){
         case 'P':
            var teamFactor = team === 'black' ? -1 : 1;
         if(Utils.onBoard({x: x, y: y + 1 * teamFactor}) && !this.board.squares[x][y + 1 * teamFactor].occupied){
            squares.push(this.board.squares[x][y + 1 * teamFactor]);
         }

         //check the diagonals for a capture
         if(Utils.onBoard({x: x + 1, y: y + 1 * teamFactor}) && this.board.squares[x + 1][y + 1 * teamFactor].occupied && this.board.squares[x + 1][y + 1 * teamFactor].piece.color !== team){
            squares.push(this.board.squares[x + 1][y + 1 * teamFactor]);
         }

         if(Utils.onBoard({x: x - 1, y: y + 1 * teamFactor}) && this.board.squares[x - 1][y + 1 * teamFactor].occupied && this.board.squares[x - 1][y + 1 * teamFactor].piece.color !== team){
            squares.push(this.board.squares[x - 1][y + 1 * teamFactor]);
         }

         //If the pawn hasn't moved yet, let it move two (and its not blocked!)
         if(piece.initialPos && Utils.onBoard({x:x, y: y + 2 * teamFactor}) && !this.board.squares[x][y + 2 * teamFactor].occupied && !this.board.squares[x][y + 1 * teamFactor].occupied){
            squares.push(this.board.squares[x][y + 2 * teamFactor]);
         }
         break;
         case 'R':
            squares = this.marchUntilPiece(piece, {up_down: true, diag: false}, 8);
         break;
         case 'B':
            squares = this.marchUntilPiece(piece, {up_down: false, diag: true}, 8);
         break;
         case 'N':
            if(Utils.onBoard({x: x + 2, y: y + 1}) && !(this.board.squares[x + 2][y + 1].piece && this.board.squares[x + 2][y + 1].piece.color === team))
               squares.push(this.board.squares[x + 2][y + 1]);
            if(Utils.onBoard({x: x + 2, y: y - 1}) && !(this.board.squares[x + 2][y - 1].piece && this.board.squares[x + 2][y - 1].piece.color === team))
               squares.push(this.board.squares[x + 2][y - 1]);
            if(Utils.onBoard({x: x - 2, y: y + 1}) && !(this.board.squares[x - 2][y + 1].piece && this.board.squares[x - 2][y + 1].piece.color === team))
               squares.push(this.board.squares[x - 2][y + 1]);
            if(Utils.onBoard({x: x - 2, y: y - 1}) && !(this.board.squares[x - 2][y - 1].piece && this.board.squares[x - 2][y - 1].piece.color === team))
               squares.push(this.board.squares[x - 2][y - 1]);
            if(Utils.onBoard({x: x + 1, y: y + 2}) && !(this.board.squares[x + 1][y + 2].piece && this.board.squares[x + 1][y + 2].piece.color === team))
               squares.push(this.board.squares[x + 1][y + 2]);
            if(Utils.onBoard({x: x + 1, y: y - 2}) && !(this.board.squares[x + 1][y - 2].piece && this.board.squares[x + 1][y - 2].piece.color === team))
               squares.push(this.board.squares[x + 1][y - 2]);
            if(Utils.onBoard({x: x - 1, y: y + 2}) && !(this.board.squares[x - 1][y + 2].piece && this.board.squares[x - 1][y + 2].piece.color === team))
               squares.push(this.board.squares[x - 1][y + 2]);
            if(Utils.onBoard({x: x - 1, y: y - 2}) && !(this.board.squares[x - 1][y - 2].piece && this.board.squares[x - 1][y - 2].piece.color === team))
               squares.push(this.board.squares[x - 1][y - 2]);
         break;
         case 'K':
            squares = this.marchUntilPiece(piece, {up_down: true, diag: true}, 1);
         break;
         case 'Q':
            squares = this.marchUntilPiece(piece, {up_down: true, diag: true}, 8);
         break;

      }
      return squares;
   }