Checking for Move Validity

Paths

In chess a piece in front of another piece is like a brick wall. The only piece that can somehow break this barrier is a knight, while the rest must succumb to the limitations of a blocked path. Therefore, the board must be further refined to check for a blocked path when neccessary. Remember, the board doesn't move the pieces, you do. All the board does is provide an environment where the pieces can be moved and maintained. In other words, the board checks paths when called on to do so.

To check a path the function is given a start square. It determines whether the piece is moving horizontally, vertically, or diagnolly by using a special function that can find the difference between the start square and the end square.

findTheDiff(int start, int end)
{
    int x = start - end;
    if(x<0)
         x*=1
     return x;
}

In this way we find the difference in rows and the difference in columns. If they are equal, then the piece is moving diagnolly. If the rows are changing, then were moving horizontally. If the columns are changing, then we're moving vertically. Next, set up counters to check the path of the piece from it's start until its end. If a piece is found, it's all over. Otherwise, the path ain't blocked. Remember that the start square and end square don't have to be empty. This algorithm for checking paths on the board comes in handy when searching for checks since a checking piece must have a clear path.

A path algorithm...
public void checkPath(Move m){
    //get the info from the move
    sRank = m.getsRank();
    sFile = m.getsFile();
    eRank = m.geteRank();
    eFile = m.geteFile();
    //find dX and dY
    dX = findTheDiff(sFile, eFile);
    dY = findTheDiff(sRank, eRank);
    //checking along ranks
    if(dX == 0){
        int iStep = sRank;
        int i = 1;
        //if the start is greater than the end
        if(sRank>eRank)
            i*=-1;
        //run loop until we've reached the end
        while(not (iStep == (eRank - i)){
            if(board[sFile][iStep + i].getColor() not equal COLORLESS)
                return true; //the path is blocked
            iStep += i;
        }
    }

    //checking alongs files
    if(dY==0){
    int jStep = sRank;
    int j= 1;
    if(sFile>eFile)
        j*=-1;
    while(not (sFile == (eFile - j)){
        if(board[jStep + j][sRank].getColor == COLORLESS)
            return true;
        jStep += j;
    }
    //checking along diagnols
    if(dY==dX){
    int iStep = sRank;
    int jStep = sFile;
    i = 1;
    j = 1;
    if(sRank > eRank)
        i*=-1;
    if(sFile > eFile)
        j*=-1;
    while(not (iStep == (eRank - i)) && not (jStep == (eFile - j)))
        if(board[jStep + j][iStep + i].getColor == COLORLESS)
            return true;
        iStep += i;
        jStep += j;
    }
    //if nothing caught, the path is not blocked
    return false;
}

Move Validity

Overall move validity consist of two things. One, the piece must have a clear path and two, the piece must be in its definition. So, the function for checking for move validity consist of only these two things and it returns the result of its search, which is either true or false, to an interface. This is really a simple function that uses the properties of a piece to determine what type it is dealing with.

start