Hello Everyone

In our first lesson we looked at our chess piece and position table and the code for each square on our board. As I mentioned the table may change and it has. I will review the new structure and reason for the change below. We also looked at moving the piece image from selected square to target square and stopped there. Today we will look at the new table and the changes to our current code as well as address the code which validates and either allows or denies the user move.

Lets start with our new table.

ChessTableGridBrowseThe first thing you should notice is the table is now laid out like a chess board. Rank represents the row in the table and the row on the board and the letters A thru H are the fields in the table and the columns on the board. This layout allows us to easily select the value in any field simply by addressing the row and column and if we build an array from this table’s value we have a dimensional array which we need in our code and which I will demonstrate later.

Again if you look  at the image of the table above and compare it with the image of the current board below, you will see the piece placement in the table match the placement on the board.

Chess Piece Placement Each time a piece is moved on the board the table is automatically updated which also updates the array. So now lets discuss the code.

As I mentioned in the previous lesson, when the user selects a piece on the board, the program must insure the move he/she makes is valid and legal. For example, the queen in our image can move up and down column A, Right on row 5 and diagonally from left to right both up and down. The available squares are displayed in the next image.

Avail Queen MovesThis is all squares the Queen can move to legally from her current position, but some are blocked and others represent captures. The next image is of the text box below our board which shows only valid moves for the queen eliminating the blocked squares and or capture squares.

Black Queen Legal MovesAs you can see, the computer already knows all squares where the queen can go and actually knows all squares all pieces can go. but that comes later as does the code for capturing an opponent piece.

Selecting a Piece on the board: “WhitesMove”

WhitesMove is a script in the script library which is called whenever a square (button) is selected. Here is the code.

'Date Created: 04-Sep-2014 10:04:59 AM
'Last Updated: 18-Sep-2014 11:09:07 AM
'Created By  : cdc
'Updated By  : cdc
vmove = ""
tmove = ""
cObjNm = Current_object()
if xDo = "Select" then
    cObjNm = Current_object()
    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))
'    if Left(cPieceNm,1) = "B" then
'        text1.text = "White cannot move Black's Piece"
'        t.close()
'        OrigPos = ""
'        tmove = "Invalid"
'        xPiece = ""
'        xMove = ""
'        cPieceNm = ""
'        NewPOS = ""
'        vMove = ""
'        xDo = "Select"
'        end
'    end if
    xPiece = eval("t."+Left(xMove,1)+"img")
    t.close()
    script_play("SelectPiece")
    script_play("BlockorCapture")
    text1.text = alltrim(MoveList)
'    if tMove = "InValid" then
'        OrigPos = ""
'        tmove = ""
'        xPiece = ""
'        xMove = ""
'        cPieceNm = ""
'        NewPOS = ""
'        vMove = ""
'        xDo = "Select"
'    else    
    xDo = "Move"
'    end if
else if xDo = "Move" 
    script_play("ValidateMoveWhite") 
    if tmove = "Valid"
        goto domove
    else
        if OrigPos = xMove then
            text1.text = "White Cancelled Move!"
            OrigPos = ""
            tmove = ""
            xPiece = ""
            xMove = ""
            cPieceNm = ""
            NewPOS = ""
            vMove = ""
            xDo = "Select"
        else
        text1.text = "Not a Valid move!"    
            OrigPos = ""
            tmove = ""
            xPiece = ""
            xMove = ""
            cPieceNm = ""
            NewPOS = ""
            vMove = ""
            xDo = "Select"
        end if
    end if    
else
    OrigPos = ""
    tmove = ""
    xPiece = ""
    xMove = ""
    cPieceNm = ""
    NewPOS = ""
    vMove = ""
    xDo = "Select"
end if
end
domove:
xbasic_wait_for_idle()
cObjNm = Current_object()
vMove = eval(cObjNm+".Object.Name")
rNbr = val(Right(Alltrim(vMove),1))
t = table.open("tblgrid")
t.fetch_goto(rNbr)
t.change_begin()
eval("t."+Left(Alltrim(vMove),1)) = cPieceNm
eval("t."+Left(Alltrim(vMove),1)+"Img") = xPiece
t.change_end(.t.)
rNbr = val(Right(Alltrim(xMove),1))
t.fetch_goto(rNbr)
ui_msg_box("",""+rNbr+" : "+xPiece+" : "+xMove+" : "+vMove)
t.change_begin()
eval("t."+Left(Alltrim(xMove),1)) = "Open"
eval("t."+Left(Alltrim(xMove),1)+"Img") = "Blank Trans"
t.change_end(.t.)
t.close()
xDo = "Select"
xbasic_wait_for_idle()
eval(cObjNm+".default.hbitmap.bmpfile")  = "[PathAlias.ADB_Path]\Engine\Images"+chr(92)+Alltrim(xPiece)+".png"
eval("topparent:"+xMove+".Default.Hbitmap.Bmpfile")  = "[PathAlias.ADB_Path]\Engine\Images\Blank Trans.png"
eval(cObjNm+".Refresh()")
eval("topparent:"+xMove+".Refresh()")
xbasic_wait_for_idle()
text1.text = cPieceNm+" Moved to.. "+vMove    
OrigPos = ""
tmove = ""
xPiece = ""
xMove = ""
cPieceNm = ""
NewPOS = ""
vMove = ""
xDo = "Select"

Please note the rem’d out code restricts black pieces being selected. This gives me access to all pieces and positions on the board.

the first thing we do in our code is determine are we selecting a piece or location? If the answer is piece, the variable

xDo

is set to Select otherwise it is set to Move.Next we use Current_Object to retrieve the name of our square and break it into parts. (remember, each square is named for the column and rank.) Our Black Queen is on square a5. We use the 5 as our record number then open our tblgrid table fetch record 5 then assign the value of the field (piece name) to our variable cPieceNm. We also assign the image name to xPiece at the same time.

Next we call the script SelectPiece.

SelectPiece:

SelectPiece list all available moves for each piece from each square on the board. Here is the code.

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,1," ",2) = "B Pawn" then
    if Left(Alltrim(xMove),1) = "a" then
        MoveList = IF(rNbr=1,"Swap",IF(rNbr=2,"a1,B1",IF(rNbr=3,"a2,B2",IF(rNbr=4,"a3,B3",\
        IF(rNbr=5,"a4,B4",IF(rNbr=6,"a_5,B5",IF(rNbr=7,"a6,A_5,B6","")))))))
    else if Left(Alltrim(xMove),1) = "b" then
        MoveList = IF(rNbr=1,"Swap",IF(rNbr=2,"B1,C1,A1",IF(rNbr=3,"B2,C2,A2",IF(rNbr=4,"B3,C3,A3",\
        IF(rNbr=5,"B4,C4,A4",IF(rNbr=6,"B5,C5,A_5",IF(rNbr=7,"B6,B5,C6,A6","")))))))
    else if Left(Alltrim(xMove),1) = "c" then
        MoveList = IF(rNbr=1,"Swap",IF(rNbr=2,"C1,D1,B1",IF(rNbr=3,"C2,D2,B2",IF(rNbr=4,"C3,D3,B3",\
        IF(rNbr=5,"C4,D4,B4",IF(rNbr=6,"C5,D5,B5",IF(rNbr=7,"C6,C5,D6,B6","")))))))
    else if Left(Alltrim(xMove),1) = "d" then
        MoveList = IF(rNbr=1,"Swap",IF(rNbr=2,"D1,E1,C1",IF(rNbr=3,"D2,E2,C2",IF(rNbr=4,"D3,E3,C3",\
        IF(rNbr=5,"D4,E4,C4",IF(rNbr=6,"D5,E5,C5",IF(rNbr=7,"D6,D5,E6,C6","")))))))
    else if Left(Alltrim(xMove),1) = "e" then
        MoveList = IF(rNbr=1,"Swap",IF(rNbr=2,"E1,F1,D1",IF(rNbr=3,"E2,F2,D2",IF(rNbr=4,"E3,F3,D3",\
        IF(rNbr=5,"E4,F4,D4",IF(rNbr=6,"E5,F5,D5",IF(rNbr=7,"E6,E5,F6,D6","")))))))
    else if Left(Alltrim(xMove),1) = "f" then
        MoveList = IF(rNbr=1,"Swap",IF(rNbr=2,"F1,G1,E1",IF(rNbr=3,"F2,G2,E2",IF(rNbr=4,"F3,G3,E3",\
        IF(rNbr=5,"F4,G4,E4",IF(rNbr=6,"F5,G5,E5",IF(rNbr=7,"F6,F5,G6,E6","")))))))
    else if Left(Alltrim(xMove),1) = "g" then
        MoveList = IF(rNbr=1,"Swap",IF(rNbr=2,"G1,H1,F1",IF(rNbr=3,"G2,H2,F2",IF(rNbr=4,"G3,H3,F3",\
        IF(rNbr=5,"G4,H4,F4",IF(rNbr=6,"G5,H5,F5",IF(rNbr=7,"G6,G5,H6,F6","")))))))
    else if Left(Alltrim(xMove),1) = "h" then
        MoveList = IF(rNbr=1,"Swap",IF(rNbr=2,"H1,G1",IF(rNbr=3,"H2,G2",IF(rNbr=4,"H3,G3",\
        IF(rNbr=5,"H4,G4",IF(rNbr=6,"H5,G5",IF(rNbr=7,"H6,H5,G6","")))))))
    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

I know this looks complicated, but it is actually very simple.The initil If else statement is divided into pieces which is then devided into columns A-H and finally into rows 1-8. The result is a string which shows all legal moves for the selected piece on the selected square. Refer to the image above for Queens’s legal moves.

Special Note: the queen moves like both the Rook and the Bishop. Therefore rather than repeating all of the code for both the rook and bishop I simply use some basic branching to built the Queen List of all Rook and Bishop Moves

Once we have our string, we then call the script BlockorCapture.

BlockorCapture shortens our string from all available moves to legal moves only (currently ignores capture.) This is also the first example or our dimentional array.

BlockorCapture:

dim WPiece[8] as P
WPiece.initialize_from_table("tblgrid")
dim list 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 = ""
dim Status as C = "Open"

list = ""
list2 = comma_to_crlf(MoveList)
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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            if Status = "Open" 
            list = list + square+ ","
            Status = "Open"
            end if
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
        END IF
    End if    
Next
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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            if Status = "Open" 
            list = list + square+ ","
            Status = "Open"
            end if
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
        END IF
    End if    
Next
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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            if Status = "Open" 
            list = list + square+ ","
            Status = "Open"
            end if
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
        END IF
    End if    
Next
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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            if Status = "Open" 
            list = list + square+ ","
            Status = "Open"
            end if
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
        END IF
    End if    
Next
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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            if Status = "Open" 
            list = list + square+ ","
            Status = "Open"
            end if
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
        END IF
    End if    
Next
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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            if Status = "Open" 
            list = list + square+ ","
            Status = "Open"
            end if
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
        END IF
    End if    
Next
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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            if Status = "Open" 
            list = list + square+ ","
            Status = "Open"
            end if
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
        END IF
    End if    
Next
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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            if Status = "Open" 
            list = list + square+ ","
            Status = "Open"
            end if
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
        END IF
    end if
next
if List = "" then
    List = cPieceNm + " is Blocked In and cannot move!"
    tmove = "InValid"
    xDo = "Select"
else
    List = cPieceNm + " Has the Following available moves! "+List
    xDo = "Move"
end if
Movelist = List

At the top of our script we create our array WPiece. By using

WPiece.initialize_from_table(“tblgrid”)

I do not have to hard code the array and each time I change the table, I simply re-initalize and the array is current.

Next we set a series of local variables for the purpose of comparrison, then we convert our string to a list using

comma_to_crlf()

Now with the help of the function for each next… we can compare the values in our string with the properties of our array to see where the selected piece can actually move to. To do this, we must start from the current position and fan out in each available direction. For example, our Queen sits on  a_5,

(I seperate the a and 5 with an underscore since a5 is a keyword in Alpha Software. Fialing to do this would cause errors in the program.)

so if the value in the list is a6 then I run through the list looking for all values where column is the same and Rank is higher than the origianl position. I repeat he test for each direction any given piece can move and I end the squares if a block or capture is encountered.

I won’t lie to you, it took several days for me to finally figure this out. I will however need to modify it when I incorporate en-passant and Castle. (That will be another late night!)

Now that I have my revised string I return to the script WhiteMove. If no available moves were found, the program lets the user know the selected piece is blocked and has no moves. This allows them to select a new piece and try again.

When they select a square to move to, WhiteMove is called again only

xDo=”Move”

The first thing the script does is check if the move is valid

ValidateMoveWhite

'Date Created: 05-Sep-2014 12:54:24 PM
'Last Updated: 18-Sep-2014 07:55:52 AM
'Created By  : cdc
'Updated By  : cdc

tmove = "Valid"
OrigPos = eval(cObjNm+".Object.Name")
if OrigPos = xMove then
    tmove = "Not Valid"
end if
NewPos = word(current_object(),3,":",1)
xisin = NewPOS
if *any(MoveList, UPPER(xisin)) = .t. .OR. *any(MoveList, xisin) = .t. then
'    list = comma_to_crlf(MoveList)
'    for each square in list
    tmove = "Valid"
else
    tmove = "Not Valid"
end if    
if tmove = "Not Valid" then
    end 
else    
end if

This script is simple. It test the revised string to see if the new square is in the list and returns Valid if it is and Invalid if not.

Control is returned to WhiteMove which then completes the move and updates the table with the new piece placement. The screen is refershed and the move is done.

Now that was a lot of work just to move one piece on the board; but we also setup the necessary structure for capture, assigning move values, determining dangerious moves and we can now score the strength of White’s and Blacks moves so the computer will be able to play as Black by itself. That’s exciting.!!!!!!!!!!

Well that’s it for today’s session. The next session will look at the capture move and assigning values to moves. I hope all of you are enjoying this lesson as much as I am. If so drop me a line and let me know.

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.