Hello Everyone

In our last lesson we showed a video demonstrating the chess program in action with a human playing both Black and White. As I commented in the lesson, the code is growing and starting to become unmanageable. The code does work, but it fails in a major way.

The available data strings are created only after the user selects a piece and makes a move.

So why is this important? For White, which is played by a human, the code needs to see all possible moves before the user selects a piece to move to insure the move is legal and valid. For Black, the computer will not only need to see what moves Black has available, but also, Whites moves in order to determine the best possible move and Whites expected response.

The only way for the current code to do this is to rerun the same code for each piece that can move, then again for the selected piece and finally again when the piece moves. Now if you watched the video you saw in real time that this did not take much time to do, but what happens when the computer starts playing for Black and testing each move and Whites response eight moves out. The answer is it takes forever.

Those who have been following my blog know I have stated many times, a programmer must be willing to abandon a direction in code if the code does not meet the overall design requirements and satisfy the needs of the client. In fact, the most request I get from clients is to fix applications created by other developers who failed for deliver what the client really wanted in their custom application.  I fix the applications by starting with the client expectation and work backwards removing code which does not work or delivers the wrong result and replace it with code that adheres to the original design and gives the client their expected result.

In this case, almost all of our code so far will be modified or thrown out. Currently the scripts

  • SelectPiece
  • BlockorCapture
  • ActivePiece
  • VerifyCheck
  • CapturePiece

only work after the user selects a piece to move and makes the move. Collectively these scripts have over 1500 lines of code and will not meet the future needs of our program.

Before we look at our new code, lets set up a scenario in our game. the image below shows our Chess board with Whites Bishop in line to attack Blacks King but the attack is blocked by Blacks Queen Pawn 4 on D7.

Blocking CheckOur new code now allows the program to see every move available for Black and White before Black makes his move. Also, the program knows where each players King is and whether  the opposing player is in position to threaten the others King. The images below show the list available to our program before Black makes a selection.

Avail Moves blackAll available moves for Black

Avail Moves WhiteAll available moves for White

Line of AttackWhites piece which is threatening Black’s King. Two scripts make this happen.

WhitesBoard

BlacksBoard

WhitesBoard

CKList = ""
AttackSqs = ""
WAttackSqs = ""
SelPiece = ""
CurrPiece = ""
WCanMove = ""
dim list as C
list = <<%a%
A
B
C
D
E
F
G
H
%a%
dim WPiece[8] as P
WPiece.initialize_from_table("tblgrid")
for each xcol in List
    dim i as N
    FOR i = 1 to 8 step 1
    SelPiece = xcol
    if xcol = "A" .and. i = 5 
        SelPiece = "A_"
    end if        
    cPieceNm = eval("WPiece["+i+"]."+Alltrim(xcol))
    if left(eval("WPiece["+i+"]."+Alltrim(xcol)),1) = "W" 
    xMove = SelPiece+alltrim(str(i))
    rNbr = i
    dim Qlist as C = ""
    if word(cPieceNm,1," ",2) = "W Pawn" then
        if Left(Alltrim(xMove),1) = "a" then
            Movelist = IF(rNbr=2,"a3,A4,B3",IF(rNbr=3,"a4,B4",IF(rNbr=4,"a_5,B5",IF(rNbr=5,"a6,B6",\
            IF(rNbr=6,"a7,B7",IF(rNbr=7,"a8,B8",IF(rNbr=8,"Swap","")))))))
        else if Left(Alltrim(xMove),1) = "b" then
            Movelist = IF(rNbr=2,"B3,B4,C3,A3",IF(rNbr=3,"B4,C4,A4",IF(rNbr=4,"B5,C5,A_5",\
            IF(rNbr=5,"B6,C6,A6",IF(rNbr=6,"B7,C7,A7",IF(rNbr=7,"B8,C8,A8",IF(rNbr=8,"Swap","")))))))
        else if Left(Alltrim(xMove),1) = "c" then
            Movelist = IF(rNbr=2,"C3,C4,D3,B3",IF(rNbr=3,"C4,D4,B4",IF(rNbr=4,"C5,D5,B5",IF(rNbr=5,"C6,D6,B6",\
            IF(rNbr=6,"C7,D7,B7",IF(rNbr=7,"C8,D8,B8",IF(rNbr=8,"Swap","")))))))
        else if Left(Alltrim(xMove),1) = "d" then
            Movelist = IF(rNbr=2,"D3,D4,E3,C3",IF(rNbr=3,"D4,E4,C4",IF(rNbr=4,"D5,E5,C5",\
            IF(rNbr=5,"D6,E6,C6",IF(rNbr=6,"D7,E7,C7",IF(rNbr=7,"D8,E8,C8",IF(rNbr=8,"Swap","")))))))
        else if Left(Alltrim(xMove),1) = "e" then
            Movelist = IF(rNbr=2,"E3,E4,F3,D3",IF(rNbr=3,"E4,F4,D4",IF(rNbr=4,"E5,F5,D5",\
            IF(rNbr=5,"E6,F6,D6",IF(rNbr=6,"E7,F7,D7",IF(rNbr=7,"E8,F8,D8",IF(rNbr=8,"Swap","")))))))
        else if Left(Alltrim(xMove),1) = "f" then
            Movelist = IF(rNbr=2,"F3,F4,G3,E3",IF(rNbr=3,"F4,G4,E4",IF(rNbr=4,"F5,G5,E5",\
            IF(rNbr=5,"F6,G6,E6",IF(rNbr=6,"F7,G7,E7",IF(rNbr=7,"F8,G8,E8",IF(rNbr=8,"Swap","")))))))
        else if Left(Alltrim(xMove),1) = "g" then
            Movelist = IF(rNbr=2,"G3,G4,H3,F3",IF(rNbr=3,"G4,H4,F4",IF(rNbr=4,"G5,H5,F5",\
            IF(rNbr=5,"G6,H6,F6",IF(rNbr=6,"G7,H7,F7",IF(rNbr=7,"G8,H8,F8",IF(rNbr=8,"Swap","")))))))
        else if Left(Alltrim(xMove),1) = "h" then
            Movelist = IF(rNbr=2,"H3,H4,G3",IF(rNbr=3,"H4,G4",IF(rNbr=4,"H5,G5",IF(rNbr=5,"H6,G6",\
            IF(rNbr=6,"H7,G7",IF(rNbr=7,"H8,G8",IF(rNbr=8,"Swap","")))))))
        end if    
    else if word(cPieceNm,2," ",1) = "Rook" then
        Rook:
        if Left(Alltrim(xMove),1) = "a" then
            Movelist = IF(rNbr=1,"A2,A3,A4,A_5,A6,A7,A8,B1,C1,D1,E1,F1,G1,H1",\
            IF(rNbr=2,"a1,a3,a4,a_5,a6,a7,a8,b2,c2,d2,e2,f2,g2,h2",IF(rNbr=3,"a2,a1,a4,a_5,a6,a7,a8,b3,c3,d3,e3,f3,g3,h3",\
            IF(rNbr=4,"a3,a2,a1,a_5,a6,a7,a8,b4,c4,d4,e4,f4,g4,h4",IF(rNbr=5,"a4,a3,a2,a1,a6,a7,a8,b5,c5,d5,e5,f5,g5,h5",\
            IF(rNbr=6,"a_5,a4,a3,a2,a1,a7,a8,b6,c6,d6,e6,f6,g6,h6",IF(rNbr=7,"a6,a_5,a4,a3,a2,a1,a8,b7,c7,d7,e7,f7,g7,h7",\
            IF(rNbr=8,"a7,a6,a_5,a4,a3,a2,a1,b8,c8,d8,e8,f8,g8,h8",""))))))))
        else if Left(Alltrim(xMove),1) = "b" then
            Movelist = IF(rNbr=1,"b2,b3,b4,b_5,b6,b7,b8,a1,c1,d1,e1,f1,g1,h1",\
            IF(rNbr=2,"b1,b3,b4,b5,b6,b7,b8,a2,c2,d2,e2,f2,g2,h2",IF(rNbr=3,"b2,b1,b4,b5,b6,b7,b8,a3,c3,d3,e3,f3,g3,h3",\
            IF(rNbr=4,"b3,b2,b1,b5,b6,b7,b8,a4,c4,d4,e4,f4,g4,h4",IF(rNbr=5,"b4,b3,b2,b1,b6,b7,b8,a_5,c5,d5,e5,f5,g5,h5",\
            IF(rNbr=6,"b5,b4,b3,b2,b1,b7,b8,a6,c6,d6,e6,f6,g6,h6",IF(rNbr=7,"b6,b5,b4,b3,b2,b1,b8,a7,c7,d7,e7,f7,g7,h7",\
            IF(rNbr=8,"b7,b6,b5,b4,b3,b2,b1,a8,c8,d8,e8,f8,g8,h8",""))))))))
        else if Left(Alltrim(xMove),1) = "c" then
            Movelist = IF(rNbr=1,"c2,c3,c4,c5,c6,c7,c8,b1,a1,d1,e1,f1,g1,h1",\
            IF(rNbr=2,"c1,c3,c4,c5,c6,c7,c8,b2,a2,d2,e2,f2,g2,h2",IF(rNbr=3,"c2,c1,c4,c5,c6,c7,c8,b3,a3,d3,e3,f3,g3,h3",\
            IF(rNbr=4,"c3,c2,c1,c5,c6,c7,c8,b4,a4,d4,e4,f4,g4,h4",IF(rNbr=5,"c4,c3,c2,c1,c6,c7,c8,b5,a_5,d5,e5,f5,g5,h5",\
            IF(rNbr=6,"c5,c4,c3,c2,c1,c7,c8,b6,a6,d6,e6,f6,g6,h6",IF(rNbr=7,"c6,c5,c4,c3,c2,c1,c8,b7,a7,d7,e7,f7,g7,h7",\
            IF(rNbr=8,"c7,c6,c5,c4,c3,c2,c1,b8,a8,d8,e8,f8,g8,h8",""))))))))
        else if Left(Alltrim(xMove),1) = "d" then
            Movelist = IF(rNbr=1,"d2,d3,d4,d5,d6,d7,d8,c1,b1,a1,e1,f1,g1,h1",\
            IF(rNbr=2,"d1,d3,d4,d5,d6,d7,d8,c2,b2,a2,e2,f2,g2,h2",IF(rNbr=3,"d2,d1,d4,d5,d6,d7,d8,c3,b3,a3,e3,f3,g3,h3",\
            IF(rNbr=4,"d3,d2,d1,d5,d6,d7,d8,c4,b4,a4,e4,f4,g4,h4",IF(rNbr=5,"d4,d3,d2,d1,d6,d7,d8,c5,b5,a_5,e5,f5,g5,h5",\
            IF(rNbr=6,"d5,d4,d3,d2,d1,d7,d8,c6,b6,a6,e6,f6,g6,h6",IF(rNbr=7,"d6,d5,d4,d3,d2,d1,d8,c7,b7,a7,e7,f7,g7,h7",\
            IF(rNbr=8,"d7,d6,d5,d4,d3,d2,d1,c8,b8,a8,e8,f8,g8,h8",""))))))))
        else if Left(Alltrim(xMove),1) = "e" then
            Movelist = IF(rNbr=1,"e2,e3,e4,e5,e6,e7,e8,d1,c1,b1,a1,f1,g1,h1",\
            IF(rNbr=2,"e1,e3,e4,e5,e6,e7,e8,d2,c2,b2,a2,f2,g2,h2",IF(rNbr=3,"e2,e1,e4,e5,e6,e7,e8,d3,c3,b3,a3,f3,g3,h3",\
            IF(rNbr=4,"e3,e2,e1,e5,e6,e7,e8,d4,c4,b4,a4,f4,g4,h4",IF(rNbr=5,"e4,e3,e2,e1,e6,e7,e8,d5,c5,b5,a_5,f5,g5,h5",\
            IF(rNbr=6,"e5,e4,e3,e2,e1,e7,e8,d6,c6,b6,a6,f6,g6,h6",IF(rNbr=7,"e6,e5,e4,e3,e2,e1,e8,d7,c7,b7,a7,f7,g7,h7",\
            IF(rNbr=8,"e7,e6,e5,e4,e3,e2,e1,d8,c8,b8,a8,f8,g8,h8",""))))))))
        else if Left(Alltrim(xMove),1) = "f" then
            Movelist = IF(rNbr=1,"f2,f3,f4,f5,f6,f7,f8,e1,d1,c1,b1,a1,g1,h1",\
            IF(rNbr=2,"f1,f3,f4,f5,f6,f7,f8,e2,d2,c2,b2,a2,g2,h2",IF(rNbr=3,"f2,f1,f4,f5,f6,f7,f8,e3,d3,c3,b3,a3,g3,h3",\
            IF(rNbr=4,"f3,f1,f1,f5,f6,f7,f8,e4,d4,c4,b4,a4,g4,h4",IF(rNbr=5,"f4,f3,f2,f1,f6,f7,f8,e5,d5,c5,b5,a_5,g5,h5",\
            IF(rNbr=6,"f5,f4,f3,f2,f1,f7,f8,e6,d6,c6,b6,a6,g6,h6",IF(rNbr=7,"f6,f5,f4,f3,f2,f1,f8,e7,d7,c7,b7,a7,g7,h7",\
            IF(rNbr=8,"f7,f6,f5,f4,f3,f2,f1,e8,d8,c8,b8,a8,g8,h8",""))))))))
        else if Left(Alltrim(xMove),1) = "g" then
            Movelist = IF(rNbr=1,"g2,g3,g4,g5,g6,g7,g8,f1,e1,d1,c1,b1,a1,h1",\
            IF(rNbr=2,"g1,g3,g4,g5,g6,g7,g8,f2,e2,d2,c2,b2,a2,h2",IF(rNbr=3,"g2,g1,g4,g5,g6,g7,g8,f3,e3,d3,c3,b3,a3,h3",\
            IF(rNbr=4,"g3,g2,g1,g5,g6,g7,g8,f4,e4,d4,c4,b4,a4,h4",IF(rNbr=5,"g4,g3,g2,g1,g6,g7,g8,f5,e5,d5,c5,b5,a_5,h5",\
            IF(rNbr=6,"g5,g4,g3,g2,g1,g7,g8,f6,e6,d6,c6,b6,a6,h6",IF(rNbr=7,"g6,g5,g4,g3,g2,g1,g8,f7,e7,d7,c7,b7,a7,h7",\
            IF(rNbr=8,"g7,g6,g5,g4,g3,g2,g1,f8,e8,d8,c8,b8,a8,h8",""))))))))
        else if Left(Alltrim(xMove),1) = "h" then
            Movelist = IF(rNbr=1,"h2,h3,h4,h5,h6,h7,h8,g1,f1,e1,d1,c1,b1,a1",\
            IF(rNbr=2,"h1,h3,h4,h5,h6,h7,h8,g2,f2,e2,d2,c2,b2,a2",IF(rNbr=3,"h2,h1,h4,h5,h6,h7,h8,g3,f3,e3,d3,c3,b3,a3",\
            IF(rNbr=4,"h3,h2,h1,h5,h6,h7,h8,g4,f4,e4,d4,c4,b4,a4",IF(rNbr=5,"h4,h3,h2,h1,h6,h7,h8,g5,f5,e5,d5,c5,b5,a_5",\
            IF(rNbr=6,"h5,h4,h3,h2,h1,h7,h8,g6,f6,e6,d6,c6,b6,a6",IF(rNbr=7,"h6,h5,h4,h3,h2,h1,h8,g7,f7,e7,d7,c7,b7,a7",\
            IF(rNbr=8,"h7,h6,h5,h4,h3,h2,h1,g8,f8,e8,d8,c8,b8,a8",""))))))))
        end if
        if word(cPieceNm,2," ",1) = "Queen"
            goto Bishop    
        end if
    else if word(cPieceNm,2," ",1) = "Knight" then
        if Left(Alltrim(xMove),1) = "a" then
            Movelist = IF(rNbr=1,"b3,c2",IF(rNbr=2,"b4,c3,c1",IF(rNbr=3,"b1,b5,c2,c4",IF(rNbr=4,"b2,b6,c3,c5",\
            IF(rNbr=5,"b3,b7,c4,c6",IF(rNbr=6,"b4,b8,c5,c7",IF(rNbr=7,"b5,c8,c6",IF(rNbr=8,"b6,c7",""))))))))
        else if Left(Alltrim(xMove),1) = "b" then
            Movelist = IF(rNbr=1,"a3,c3,d2",IF(rNbr=2,"a4,c4,d1,d3",IF(rNbr=3,"a1,a_5,c1,c5,d2,d4",\
            IF(rNbr=4,"a2,a6,c2,c6,d3,d5",IF(rNbr=5,"a3,a7,c3,c7,d4,d6",IF(rNbr=6,"a4,a8,c4,c8,d5,d7",\
            IF(rNbr=7,"a5,c5,d6,d8",IF(rNbr=8,"a6,c6,d7",""))))))))
        else if Left(Alltrim(xMove),1) = "c" then
            Movelist = IF(rNbr=1,"a2,b3,d3,e2",IF(rNbr=2,"a1,a3,b4,d4,e1,e3",IF(rNbr=3,"a2,a4,b1,d1,b5,d5,e2,e4",\
            IF(rNbr=4,"a3,a_5,b2,d2,b6,d6,e3,e5",IF(rNbr=5,"a4,a6,b3,d3,b7,d7,e4,e6",IF(rNbr=6,"a_5,a7,b4,d4,b8,d8,e5,e7",\
            IF(rNbr=7,"a6,a8,b5,d5,e6,e8",IF(rNbr=8,"a7,b6,d6,e7",""))))))))
        else if Left(Alltrim(xMove),1) = "d" then
            Movelist = IF(rNbr=1,"b2,c3,e3,f2",IF(rNbr=2,"b1,b3,c4,e4,f1,f3",IF(rNbr=3,"b2,b4,c1,e1,c5,e5,f2,f4",\
            IF(rNbr=4,"b3,b5,c2,e2,c6,e6,f3,f5",IF(rNbr=5,"b4,b6,c3,e3,c7,e7,f4,f6",IF(rNbr=6,"b5,b7,c4,e4,c8,e8,f5,f7",\
            IF(rNbr=7,"b6,b8,c5,e5,f6,f8",IF(rNbr=8,"b7,c6,e6,f7",""))))))))
        else if Left(Alltrim(xMove),1) = "e" then
            Movelist = IF(rNbr=1,"c2,d3,f3,g2",IF(rNbr=2,"c1,c3,d4,f4,g1,g3",IF(rNbr=3,"c2,c4,d1,f1,d5,f5,g2,g4",\
            IF(rNbr=4,"c3,c5,d2,f2,d6,f6,g3,g5",IF(rNbr=5,"c4,c6,d3,f3,d7,f7,g4,g6",IF(rNbr=6,"c5,c7,d4,f4,d8,f8,g5,g7",\
            IF(rNbr=7,"c6,c8,d5,f5,g6,g8",IF(rNbr=8,"c7,d6,f6,g7",""))))))))
        else if Left(Alltrim(xMove),1) = "f" then
            Movelist = IF(rNbr=1,"d2,e3,g3,h2",IF(rNbr=2,"d1,d3,e4,g4,h1,h3",IF(rNbr=3,"d2,d4,e1,g1,e5,g5,h2,h4",\
            IF(rNbr=4,"d3,d5,e2,g2,e6,g6,h3,h5",IF(rNbr=5,"d4,d6,e3,g3,e7,g7,h4,h6",IF(rNbr=6,"d5,d7,e4,g4,e8,g8,h5,h7",\
            IF(rNbr=7,"d6,d8,e5,g5,h6,h8",IF(rNbr=8,"d7,e6,g6,h7",""))))))))
        else if Left(Alltrim(xMove),1) = "g" then
            Movelist = IF(rNbr=1,"e2,f3,h3",IF(rNbr=2,"e1,e3,f4,h4",IF(rNbr=3,"e2,e4,f1,h1,f5,h5",\
            IF(rNbr=4,"e3,e5,f2,h2,f6,h6",IF(rNbr=5,"e4,e6,f3,h3,f7,h7",IF(rNbr=6,"e5,e7,f4,h4,f8,h8",\
            IF(rNbr=7,"e6,e8,f5,h5",IF(rNbr=8,"e7,f6,h6",""))))))))
        else if Left(Alltrim(xMove),1) = "h" then
            Movelist = IF(rNbr=1,"f2,g3",IF(rNbr=2,"f1,f3,g4",IF(rNbr=3,"f2,f4,g1,g5",IF(rNbr=4,"f3,f5,g2,g6",\
            IF(rNbr=5,"f4,f6,g3,g7",IF(rNbr=6,"f5,f7,g4,g8",IF(rNbr=7,"f6,f8,g5",IF(rNbr=8,"f7,g6",""))))))))
        end if        
    else if word(cPieceNm,2," ",1) = "Bishop" then
        Bishop:
        if word(cPieceNm,2," ",1) = "Queen"
            Qlist = MoveList
        end if    
        if Left(Alltrim(xMove),1) = "a" then
            Movelist = IF(rNbr=1,"B2,C3,D4,E5,F6,G7,H8",IF(rNbr=2,"B1,B3,C4,D5,E6,F7,G8",IF(rNbr=3,"B2,C1,B4,C5,D6,E7,F8",\
            IF(rNbr=4,"B3,C2,D1,B5,C6,D7,E8",IF(rNbr=5,"B4,C3,D2,E1,B6,C7,D8",IF(rNbr=6,"B5,C4,D3,E2,F1,B7,C8",\
            IF(rNbr=7,"B6,C5,D4,E3,F2,G1,B8",IF(rNbr=8,"B7,C6,D5,E4,F3,G2,H1",""))))))))
        else if Left(Alltrim(xMove),1) = "b" then
            Movelist = IF(rNbr=1,"C2,D3,E4,F5,G6,H7,A2",IF(rNbr=2,"C1,C3,D4,E5,F6,G7,H8,A3,A1",IF(rNbr=3,"C2,D1,C4,D5,E6,F7,G8,A4,A2",\
            IF(rNbr=4,"C3,D2,E1,C5,D6,E7,F8,A_5,A3",IF(rNbr=5,"C4,D3,E2,F1,C6,D7,E8,A4,A6",IF(rNbr=6,"C5,D4,E3,F2,G1,C7,D8,A_5,A7",\
            IF(rNbr=7,"C6,D5,E4,F3,G2,H1,A6,A8",IF(rNbr=8,"C7,D6,E5,F4,G3,H2,,A7",""))))))))
        else if Left(Alltrim(xMove),1) = "c" then
            Movelist = IF(rNbr=1,"D2,E3,F4,G5,H6,B2,A3",IF(rNbr=2,"D1,D3,E4,F5,G6,H7,B1,B3,A4",IF(rNbr=3,"D2,E1,D4,E5,F6,G7,H8,B2,A1,B4,A_5",\
            IF(rNbr=4,"D3,E2,F1,D5,E6,F7,G8,B3,A2,B5,A6",IF(rNbr=5,"D4,E3,F2,G1,D6,E7,F8,B4,A3,B6,A7",\
            IF(rNbr=6,"D5,E4,F3,G2,H1,D7,E8,B5,A4,B7,A8",IF(rNbr=7,"D6,E5,F4,G3,H2,D8,B6,A5,B8",\
            IF(rNbr=8,"D7,E6,F5,G4,H3,B7,A6",""))))))))
        else if Left(Alltrim(xMove),1) = "d" then
            Movelist = IF(rNbr=1,"E2,F3,G4,H5,C2,B3,A4",IF(rNbr=2,"E1,E3,F4,G5,H6,C1,C3,B4,A_5",IF(rNbr=3,"E2,F1,E4,F5,G6,H7,C2,B1,C4,B5,A6",\
            IF(rNbr=4,"E3,F2,G1,E5,F6,G7,H8,C3,B2,A1,C5,B6,A7",IF(rNbr=5,"E4,F3,G2,H1,E6,F7,G8,C4,B3,A2,C6,B7,A8",\
            IF(rNbr=6,"E5,F4,G3,H2,E7,F8,C5,B4,A3,C7,B8",IF(rNbr=7,"E6,F5,G4,H3,E8,C6,B5,A4,C8",IF(rNbr=8,"E7,F6,G5,H4,C7,B6,A_5",""))))))))
        else if Left(Alltrim(xMove),1) = "e" then
            Movelist = IF(rNbr=1,"F2,G3,H4,D2,C3,B4,A_5",IF(rNbr=2,"F1,F3,G4,H5,D1,D3,C4,B5,A6",IF(rNbr=3,"F2,G1,F4,G5,H6,D2,C1,D4,C5,B6,A7",\
            IF(rNbr=4,"F3,G2,H1,F5,G6,H7,D3,C2,B1,D5,C6,B7,A8",IF(rNbr=5,"F4,G3,H2,F6,G7,H8,D4,C3,B2,A1,D6,C7,B8",\
            IF(rNbr=6,"F5,G4,H3,F7,G8,D5,C4,B3,A2,D7,C8",IF(rNbr=7,"F6,G5,H4,F8,D6,C5,B4,A3,D8",IF(rNbr=8,"F7,G6,H5,D7,C6,B5,A4",""))))))))
        else if Left(Alltrim(xMove),1) = "f" then
            Movelist = IF(rNbr=1,"G2,H3,E2,D3,C4,B5,A6",IF(rNbr=2,"G1,G3,H4,E1,E3,D4,C5,B6,A7",IF(rNbr=3,"G2,H1,G4,H5,E2,D1,E4,D5,C6,B7,A8",\
            IF(rNbr=4,"G3,H2,G5,H6,E3,D2,C1,E5,D6,C7,B8",IF(rNbr=5,"G4,H3,G6,H7,E4,D3,C2,B1,E6,D7,C8",\
            IF(rNbr=6,"G5,H4,G7,H8,E5,D4,C3,B2,A1,E7,D8",IF(rNbr=7,"G6,H5,G8,E6,D5,C4,B3,A2,E8",IF(rNbr=8,"G7,H6,E7,D6,C5,B4,A3",""))))))))
        else if Left(Alltrim(xMove),1) = "g" then
            Movelist = IF(rNbr=1,"H2,F2,E3,D4,C5,B6,A7",IF(rNbr=2,"H1,H3,F1,F3,E4,D5,C6,B7,A8",IF(rNbr=3,"H2,H4,F2,E1,F4,E5,D6,C7,B8",\
            IF(rNbr=4,"H3,H5,F3,E2,D1,F5,E6,D7,C8",IF(rNbr=5,"H4,H6,F4,E3,D2,C1,F6,E7,D8",IF(rNbr=6,"H5,H7,F5,E4,D3,C2,B1,F7,E8",\
            IF(rNbr=7,"H6,H8,F6,E5,D4,C3,B2,A1,F8",IF(rNbr=8,"H7,F7,E6,D5,C4,B3,A2",""))))))))
        else if Left(Alltrim(xMove),1) = "h" then
            Movelist = IF(rNbr=1,"G2,F3,E4,D5,C6,B7,A8",IF(rNbr=2,"G1,G3,F4,E5,D6,C7,B8",IF(rNbr=3,"G2,E1,G4,F5,E6,D7,C8",\
            IF(rNbr=4,"G3,F2,E1,G5,F6,E7,D8",IF(rNbr=5,"G4,F3,E2,D1,G6,F7,E8",IF(rNbr=6,"G5,F4,E3,D2,C1,G7,F8",\
            IF(rNbr=7,"G6,F5,E4,D3,C2,B1,G8",IF(rNbr=8,"G7,F6,E5,D4,C3,B2,A1",""))))))))
        end if        
        if word(cPieceNm,2," ",1) = "Queen"
            Movelist = Movelist +","+QList
        end if    
    else if word(cPieceNm,2," ",1) = "Queen" then
        goto Rook    
    else if word(cPieceNm,2," ",1) = "King" then
        if Left(Alltrim(xMove),1) = "a" then
            Movelist = IF(rNbr=1,"a2,b1,b2",IF(rNbr=2,"A1,a3,b2,b1,B3",IF(rNbr=3,"A2,a4,b3,b2,B4",\
            IF(rNbr=4,"A3,a5,b4,b3,B5",IF(rNbr=5,"A4,a6,b5,b4,B6",IF(rNbr=6,"A_5,a7,b6,b5,B7",\
            IF(rNbr=7,"A6,a8,b7,b6,B8",IF(rNbr=8,"A7,b8,b7",""))))))))
        else if Left(Alltrim(xMove),1) = "b" then
            Movelist = IF(rNbr=1,"B2,A1,C1,C2,A2",IF(rNbr=2,"B1,B3,A2,C2,C1,C3,A1,A3",IF(rNbr=3,"B2,B4,A3,C3,C2,C4,A2,A4",\
            IF(rNbr=4,"B3,B5,A4,C4,C3,C5,A3,A_5",IF(rNbr=5,"B4,B6,A_5,C5,C4,C6,A4,A6",IF(rNbr=6,"B5,B7,A6,C6,C5,C7,A_5,A7",\
            IF(rNbr=7,"B6,B8,A7,C7,C6,C8,A6,A8",IF(rNbr=8,"B7,A8,C8,C7,A7",""))))))))
        else if Left(Alltrim(xMove),1) = "c" then
            Movelist = IF(rNbr=1,"C2,B1,D1,D2,B2",IF(rNbr=2,"C1,C3,B2,D2,D1,D3,B1,B3",IF(rNbr=3,"C2,C4,B3,D3,D2,D4,B2,B4",\
            IF(rNbr=4,"C3,C5,B4,D4,D3,D5,B3,B5",IF(rNbr=5,"C4,C6,B5,D5,D4,D6,B4,B6",IF(rNbr=6,"C5,C7,B6,D6,D5,D7,B5,B7",\
            IF(rNbr=7,"C6,C8,B7,D7,D6,D8,B6,B8",IF(rNbr=8,"C7,B8,D8,D7,B7",""))))))))
        else if Left(Alltrim(xMove),1) = "d" then
            Movelist = IF(rNbr=1,"D2,C1,E1,E2,C2",IF(rNbr=2,"D1,D3,C2,E2,E1,E3,C1,C3",IF(rNbr=3,"D2,D4,C3,E3,E2,E4,C2,C4",\
            IF(rNbr=4,"D3,D5,C4,E4,E3,E5,C3,C5",IF(rNbr=5,"D4,D6,C5,E5,E4,E6,C4,C6",IF(rNbr=6,"D5,D7,C6,E6,E5,E7,C5,C7",\
            IF(rNbr=7,"D6,D8,C7,E7,E6,E8,C6,C8",IF(rNbr=8,"D7,C8,E8,E7,C7",""))))))))
        else if Left(Alltrim(xMove),1) = "e" then
            Movelist = IF(rNbr=1,"E2,D1,F1,F2,D2",IF(rNbr=2,"E1,E3,D2,F2,F1,F3,D1,D3",IF(rNbr=3,"E2,E4,D3,F3,F2,F4,D2,D4",\
            IF(rNbr=4,"E3,E5,D4,F4,F3,F5,D3,D5",IF(rNbr=5,"E4,E6,D5,F5,F4,F6,D4,D6",IF(rNbr=6,"E5,E7,D6,F6,F5,F7,D5,D7",\
            IF(rNbr=7,"E6,E8,D7,F7,F6,F8,D6,D8",IF(rNbr=8,"E7,D8,F8,F7,D7",""))))))))
        else if Left(Alltrim(xMove),1) = "f" then
            Movelist = IF(rNbr=1,"F2,E1,G1,G2,E2",IF(rNbr=2,"F1,F3,E2,G2,G1,G3,E1,E3",IF(rNbr=3,"F2,F4,E3,G3,G2,G4,E2,E4",\
            IF(rNbr=4,"F3,F5,E4,G4,G3,G5,E3,E5",IF(rNbr=5,"F4,F6,E5,G5,G4,G6,E4,E6",IF(rNbr=6,"F5,F7,E6,G6,G5,G7,E5,E7",\
            IF(rNbr=7,"F6,F8,E7,G7,G6,G8,E6,E8",IF(rNbr=8,"F7,E8,G8,G7,E7",""))))))))
        else if Left(Alltrim(xMove),1) = "g" then
            Movelist = IF(rNbr=1,"G2,F1,H1,H2,F2",IF(rNbr=2,"G1,G3,F2,H2,H1,H3,F1,F3",IF(rNbr=3,"G2,G4,F3,H3,H2,H4,F2,F4",\
            IF(rNbr=4,"G3,G5,F4,H4,H3,H5,F3,F5",IF(rNbr=5,"G4,G6,F5,H5,H4,H6,F4,F6",IF(rNbr=6,"G5,G7,F6,H6,H5,H7,F5,F7",\
            IF(rNbr=7,"G6,G8,F7,H7,H6,H8,F6,F8",IF(rNbr=8,"G7,F8,H8,H7,F7",""))))))))
        else if Left(Alltrim(xMove),1) = "h" then
            Movelist = IF(rNbr=1,"H2,G1,G2",IF(rNbr=2,"H1,H3,G2,G1,G3",IF(rNbr=3,"H2,H4,G3,G2,G4",\
            IF(rNbr=4,"H3,H5,G4,G3,G5",IF(rNbr=5,"H4,H6,G5,G4,G6",IF(rNbr=6,"H5,H7,G6,G5,G7",IF(rNbr=7,"H6,H8,G7,G6,G8",\
            IF(rNbr=8,"H7,G8,G7",""))))))))
        end if        
    
    end if    
'    '___________Verify BlockCapture
        
    dim Plist as C    
    dim list2 as C
    dim Rank as N
    Dim pos as C
    dim Origsq as N 
    dim prevsq as N = 0
    dim prevpos as C = ""
    Status = "Open"
    PList = ""
    CKList = ""
    if word(cPieceNm,2," ",1) = "Knight"
        Status = "Open"
        list2 = comma_to_crlf(MoveList)
        Origsq = val(Right(Alltrim(xMove),1))
        prevsq = val(Right(Alltrim(xMove),1))+1
        prevpos = left(alltrim(xMove),1)
        for each square in list2
            Rank = val(Right(Alltrim(square),1))
            pos = left(alltrim(square),1)
            IF left(eval("WPiece["+Rank+"]."+pos),1) = "O" .or. left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
                Plist = Plist +","+ square    
                CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                Status = "Open"
            ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                Status = "Blocked"
            END IF
            if containsi(square,Var->BKingPos)
                AttackSqs = CurrPiece
            end if 
        Next
    else
    Status = "Open"    
    list2 = comma_to_crlf(MoveList)
    Origsq = val(Right(Alltrim(xMove),1))
    prevsq = val(Right(Alltrim(xMove),1))+1
    prevpos = left(alltrim(xMove),1)
    for each square in list2
        Rank = val(Right(Alltrim(square),1))
        pos = left(alltrim(square),1)
        if pos = prevpos .and. Rank > OrigSq
            if Status = "Open"
                if left(eval("WPiece["+Rank+"]."+pos),1) = "B"
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Blocked"
                else if left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                    Status = "Blocked"
                else
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Open"
                end if
                if containsi(square,Var->BKingPos)
                    AttackSqs = CurrPiece
                end if 
            else        
                goto done1
            End if
        End if
    Next
    done1:
    Status = "Open"
    for each square in list2
        Rank = val(Right(Alltrim(square),1))
        pos = left(alltrim(square),1)
        if pos > prevpos .and. Rank > OrigSq
            if Status = "Open"
                if left(eval("WPiece["+Rank+"]."+pos),1) = "B"
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Blocked"
                else if left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                    Status = "Blocked"
                else
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Open"
                end if
                if containsi(square,Var->BKingPos)
                    AttackSqs = CurrPiece
                end if 
            else        
                goto done2
            End if
        End if    
    Next
    done2:
    Status = "Open"
    for each square in list2
        Rank = val(Right(Alltrim(square),1))
        pos = left(alltrim(square),1)
        if pos > prevpos .and. Rank = OrigSq
            if Status = "Open"
                if left(eval("WPiece["+Rank+"]."+pos),1) = "B"
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Blocked"
                else if left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                    Status = "Blocked"
                else
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Open"
                end if
                if containsi(square,Var->BKingPos)
                    AttackSqs = CurrPiece
                end if 
            else        
                goto done3
            End if
        End if    
    Next
    done3:
    Status = "Open"
    for each square in list2
        Rank = val(Right(Alltrim(square),1))
        pos = left(alltrim(square),1)
        if pos > prevpos .and. Rank < OrigSq
            if Status = "Open"
                if left(eval("WPiece["+Rank+"]."+pos),1) = "B"
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Blocked"
                else if left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                    Status = "Blocked"
                else
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Open"
                end if
                if containsi(square,Var->BKingPos)
                    AttackSqs = CurrPiece
                end if 
            else        
                goto done4
            End if
        End if    
    Next
    done4:
    Status = "Open"
    for each square in list2
        Rank = val(Right(Alltrim(square),1))
        pos = left(alltrim(square),1)
        if pos = prevpos .and. Rank < OrigSq
            if Status = "Open"
                if left(eval("WPiece["+Rank+"]."+pos),1) = "B"
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Blocked"
                else if left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                    Status = "Blocked"
                else
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Open"
                end if
                if containsi(square,Var->BKingPos)
                    AttackSqs = CurrPiece
                end if 
            else        
                goto done5
            End if
        End if    
    Next
    done5:
    Status = "Open"
    for each square in list2
        Rank = val(Right(Alltrim(square),1))
        pos = left(alltrim(square),1)
        if pos < prevpos .and. Rank < OrigSq
            if Status = "Open"
                if left(eval("WPiece["+Rank+"]."+pos),1) = "B"
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Blocked"
                else if left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                    Status = "Blocked"
                else
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Open"
                end if
                if containsi(square,Var->BKingPos)
                    AttackSqs = CurrPiece
                end if 
            else        
                goto done6
            End if
        End if    
    Next
    done6:
    Status = "Open"
    for each square in list2
        Rank = val(Right(Alltrim(square),1))
        pos = left(alltrim(square),1)
        if pos < prevpos .and.  Rank = OrigSq
            if Status = "Open"
                if left(eval("WPiece["+Rank+"]."+pos),1) = "B"
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Blocked"
                else if left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                    Status = "Blocked"
                else
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Open"
                end if
                if containsi(square,Var->BKingPos)
                    AttackSqs = CurrPiece
                end if 
            else        
                goto done7
            End if
        End if    
    Next
    done7:
    Status = "Open"
    for each square in list2
        Rank = val(Right(Alltrim(square),1))
        pos = left(alltrim(square),1)
        if pos < prevpos .and. Rank > OrigSq
            if Status = "Open"
                if left(eval("WPiece["+Rank+"]."+pos),1) = "B"
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Blocked"
                else if left(eval("WPiece["+Rank+"]."+pos),1) = "W"
                    Status = "Blocked"
                else
                    Plist = Plist +","+ square    
                    CurrPiece = eval("WPiece["+i+"]."+xcol)+" : "+xMove+" : "+ PList
                    Status = "Open"
                end if
                if containsi(square,Var->BKingPos)
                    AttackSqs = CurrPiece
                end if 
            else        
                goto done8
            End if
        end if
    next
    done8:
    end if
    WCanMove = WCanMove + CurrPiece+crlf()
    WCanMove = word_unique(WCanMove, crlf() )
    end if
    Next i
Next
WMoveList = ""
for each sq in WCanMove
    WMoveList = WMoveList + word(sq,3,":",1)+crlf()
Next

for each piece in WMoveList
    if *any(piece, UPPER(BKingPos)) = .t. .OR. *any(piece, BKingPos) = .t. then
        MoveList = ""
    end if
Next
if MoveList = ""
    tmove = "InValid"
else
    tmove = "Valid"
end if        
xMove = eval(cObjNm+".Object.Name")
rNbr = Val(Right(Alltrim(xMove),1))
t = table.open("tblgrid")
t.fetch_first()
t.fetch_goto(rNbr)
cPieceNm = eval("t."+Left(xMove,1))
xPiece = eval("t."+Left(xMove,1)+"img")
t.close()
'if len(Alltrim(AttackSqs)) > 1
WAttackSqs = AttackSqs
AttackSqs = ""

BlacksBoard is the same except values are assigned to a different variable.

The way the script works is to build a list of columns A through H and an array of 1 through 8. The script steps through the list and for each value in our array we find the corresponding square in our Table array and check to see if a piece exist on the square. If it does we then check to insure the piece is not blocked in and if not, we list all possible squares the piece can move to from that square. We then check that list to see if the opposing King is in the line of attack for the piece being tested and if so, that list is assigned to AttackSqs. And best of all, I happens before a player selects a single piece to move.

Now we are able to keep the player from moving a piece which would result in the King being put in check. We can determine if any of the players pieces can block a current check or attack the piece which is checking the King.  We will be able to determine if and when a castle can be performed and if en’passant comes into play. When we get to the point where the computer starts playing for Black, this process will allow us to test very rapidly all variations on the board and make the best possibloe move for Black. Finally the code is run when a new game is started and after the player makes a move so the new board positions are available to the program for control.

Well that’s it for today’s session. The next session will look at the new code for controlling and tracking each players move and how we will be assigning values to moves. Hopefully now that I am on the right track the next lesson will be posted sooner.

Thanks again for stopping by and Remember, if you need help with an Alpha Software application or wish to inquire about a custom application for your business go to our website

www.cdc-takecharge.com

and inquire or contact

NLawson@cdc-TakeCharge.com

Have a great day.