Some code has been cut out that is unrelated.
Here's a few functions
So it always starts with isValidMove where to is correct such as to.x = 5, to.y=4
it will then enter isInCheck and to values come out t.x=-1 and to.y=-1 to enter pawnMoveCheck
So... ideas?
Code: Select all
//to will start out as to.x = 5, to.y=4 or some other #'s that are always correct
this.isValidMove = function(board, from, to, check)
{
if(check == true)
{
console.debug("here");
var boom = this.isInCheck(board, from); //COMES OUT OF HERE different
if(boom != false)
{
alert(boom);
}
}
//var pieceMoving = board[from.x][from.y].getPiece();
var pieceMoving = piece;
switch(pieceMoving)
{
case "p": //pawn
return this.pawnMoveCheck(board, from, to); //enters here wrong!!
break;
}
}
//checks if king is in check
this.isInCheck = function(board, spot)
{
//check all opponents pieces to see if attacking king
//search for king
for(y = 0; y < 8; y++)
{
for(x = 0; x < 8; x++)
{
if(board[x][y] != null && board[x][y] != ".")
{
//a opponents piece
if(board[x][y].getColor() != this.getColor())
{
//is a pawn
if(board[x][y].getPiece() != "p" && board[x][y].getPiece() != "l")
{
loc = {x: x, y: y};
if(this.isValidMove(board, loc, king, false)) // RESURSIVE
{
return board[x][y].getPiece();
}
}
}
}
}
}
return "no";
}