Hello Everyone

Today we will be looking at the process for capturing an opponents piece and the process for checking the opponents King. Also, we will need to readdress the Setup code to reflect the changes in our table grid. First, I now have a short video showing the game so far. Now it is not complete, but is far enough along for you to see both black and white controlled by a human, (me) and the game conforms to the rules of Chess. Sit back relax and enjoy this short video.

Well I hope you liked it and as you can see there are lots of changes so lets begin.

OnInit Event of form

'Date Created: 02-Sep-2014 12:37:37 PM
'Last Updated: 24-Sep-2014 10:56:51 AM
'Created By  : cdc
'Updated By  : cdc
dim SHARED PlayerTurn as C
dim SHARED cObjNm as C
Dim SHARED cPieceNm as C
dim SHARED tmove as C
dim SHARED Capture as C
dim SHARED NewPOS as C
dim SHARED moveList as C
dim shared vMove as C
dim SHARED OrigPos as C
dim SHARED Status as C
dim SHARED WKingPos as C 
dim SHARED BKingPos as C 
dim SHARED WCheckB as L 
dim SHARED BCheckW as L
dim SHARED CKList as C


xDo = "Select"

The only changes here are the inclusion of five more variables

  • PlayerTurn
  • WKingPos
  • BKingPos
  • WCheckB
  • BCheckW

You will see them in use in the code which follows.

New Game Button

'Date Created: 02-Sep-2014 11:07:07 AM
'Last Updated: 24-Sep-2014 12:51:25 PM
'Created By  : cdc
'Updated By  : cdc

'Zap (remove all records from) a table.
dim tablename as c 
tablename = "piecemoves"
dim prompt_for_confirmation  as l 
prompt_for_confirmation = .f.
'check if the table exists
dim table_filename as c 
table_filename = table.filename_get(tablename)
if file.exists(table_filename) = .f. then 
    ui_msg_box("Error","Table not found: '" + tablename + "'.",UI_STOP_SYMBOL)
else
    'check if table is in use
    if table.in_use(table_filename) = .t. then 
        ui_msg_box("Error","Table '"+tablename+"' cannot be Zapped (i.e. emptied) because it is in use.",UI_STOP_SYMBOL)
    else
        dim tbl as p 
        dim flag_ok_to_zap as l 
        flag_ok_to_zap = .t.
        if prompt_for_confirmation = .t. then 
            dim prompt_result as n 
            prompt_result = ui_msg_box("Warning","Are you sure that you want to Zap '"+tablename+"'?",ui_yes_no_cancel+ui_question_symbol)
            if prompt_result <> ui_yes_selected then
                flag_ok_to_zap = .f.
            end if 
        end if 
        
        if flag_ok_to_zap = .t. then 
            tbl = table.open(tablename)
            tbl.zap(.t.)
            tbl.pack()
            tbl.close()
        end if 
    end if 
end if 
'Clear White's Corral
dim i as N
for i = 1 to 15 step 1
    eval("Wc"+Alltrim(str(i))+".Default.Hbitmap.Bmpfile")  = "[PathAlias.ADB_Path]\Engine\Images\Blank Trans.png"
Next i
'Clear Black's Corral
for i = 1 to 15 step 1
    eval("Bc"+Alltrim(str(i))+".Default.Hbitmap.Bmpfile")  = "[PathAlias.ADB_Path]\Engine\Images\Blank Trans.png"
Next i


DIM Append as P

a_tbl = table.open("tblgrid")
append.t_db = "init_pieceposition"
append.m_key = "Ranknbr"
append.t_key = "Ranknbr"
append.m_filter = ""
append.t_filter = ""
append.type = "Unique, replace existing"
append.m_count = 33
append.m_case1 = "EITHER"
append.m_field1 = "RANKNBR"
append.m_exp1 = "@INIT_PIECEPOSITION->RANKNBR"
append.m_case2 = "EITHER"
append.m_field2 = "A"
append.m_exp2 = "@INIT_PIECEPOSITION->A"
append.m_case3 = "EITHER"
append.m_field3 = "B"
append.m_exp3 = "@INIT_PIECEPOSITION->B"
append.m_case4 = "EITHER"
append.m_field4 = "C"
append.m_exp4 = "@INIT_PIECEPOSITION->C"
append.m_case5 = "EITHER"
append.m_field5 = "D"
append.m_exp5 = "@INIT_PIECEPOSITION->D"
append.m_case6 = "EITHER"
append.m_field6 = "E"
append.m_exp6 = "@INIT_PIECEPOSITION->E"
append.m_case7 = "EITHER"
append.m_field7 = "F"
append.m_exp7 = "@INIT_PIECEPOSITION->F"
append.m_case8 = "EITHER"
append.m_field8 = "G"
append.m_exp8 = "@INIT_PIECEPOSITION->G"
append.m_case9 = "EITHER"
append.m_field9 = "H"
append.m_exp9 = "@INIT_PIECEPOSITION->H"
append.m_case10 = "EITHER"
append.m_field10 = "AIMG"
append.m_exp10 = "@INIT_PIECEPOSITION->AIMG"
append.m_case11 = "EITHER"
append.m_field11 = "BIMG"
append.m_exp11 = "@INIT_PIECEPOSITION->BIMG"
append.m_case12 = "EITHER"
append.m_field12 = "CIMG"
append.m_exp12 = "@INIT_PIECEPOSITION->CIMG"
append.m_case13 = "EITHER"
append.m_field13 = "DIMG"
append.m_exp13 = "@INIT_PIECEPOSITION->DIMG"
append.m_case14 = "EITHER"
append.m_field14 = "EIMG"
append.m_exp14 = "@INIT_PIECEPOSITION->EIMG"
append.m_case15 = "EITHER"
append.m_field15 = "FIMG"
append.m_exp15 = "@INIT_PIECEPOSITION->FIMG"
append.m_case16 = "EITHER"
append.m_field16 = "GIMG"
append.m_exp16 = "@INIT_PIECEPOSITION->GIMG"
append.m_case17 = "EITHER"
append.m_field17 = "HIMG"
append.m_exp17 = "@INIT_PIECEPOSITION->HIMG"
append.m_case18 = "EITHER"
append.m_field18 = "AN"
append.m_exp18 = "@INIT_PIECEPOSITION->AN"
append.m_case19 = "EITHER"
append.m_field19 = "BN"
append.m_exp19 = "@INIT_PIECEPOSITION->BN"
append.m_case20 = "EITHER"
append.m_field20 = "CN"
append.m_exp20 = "@INIT_PIECEPOSITION->CN"
append.m_case21 = "EITHER"
append.m_field21 = "DN"
append.m_exp21 = "@INIT_PIECEPOSITION->DN"
append.m_case22 = "EITHER"
append.m_field22 = "EN"
append.m_exp22 = "@INIT_PIECEPOSITION->EN"
append.m_case23 = "EITHER"
append.m_field23 = "FN"
append.m_exp23 = "@INIT_PIECEPOSITION->FN"
append.m_case24 = "EITHER"
append.m_field24 = "GN"
append.m_exp24 = "@INIT_PIECEPOSITION->GN"
append.m_case25 = "EITHER"
append.m_field25 = "HN"
append.m_exp25 = "@INIT_PIECEPOSITION->HN"
append.m_case26 = "EITHER"
append.m_field26 = "CAPTURE"
append.m_exp26 = "@INIT_PIECEPOSITION->CAPTURE"
append.m_case27 = "EITHER"
append.m_field27 = "MOVEVALUE"
append.m_exp27 = "@INIT_PIECEPOSITION->MOVEVALUE"
append.m_case28 = "EITHER"
append.m_field28 = "WHITEVAL"
append.m_exp28 = "@INIT_PIECEPOSITION->WHITEVAL"
append.m_case29 = "EITHER"
append.m_field29 = "BLACKVAL"
append.m_exp29 = "@INIT_PIECEPOSITION->BLACKVAL"
append.m_case30 = "EITHER"
append.m_field30 = "CHECK"
append.m_exp30 = "@INIT_PIECEPOSITION->CHECK"
append.m_case31 = "EITHER"
append.m_field31 = "CHECKMATE"
append.m_exp31 = "@INIT_PIECEPOSITION->CHECKMATE"
append.m_case32 = "EITHER"
append.m_field32 = "FIRSTMOVE"
append.m_exp32 = "@INIT_PIECEPOSITION->FIRSTMOVE"
append.m_case33 = "EITHER"
append.m_field33 = "CASTLE"
append.m_exp33 = "@INIT_PIECEPOSITION->CASTLE"
append.t_count = 0
a_tbl.append()
a_tbl.close()
xbasic_wait_for_idle()

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
    if i = 5 .and. xcol = "A"
        cObjNm = xcol+"_"+i 
    else
        cObjNm = xcol+i
    end if        
    if eval("WPiece["+i+"]."+xcol) = "Black King"
        bKingPos = cObjNm
    else if eval("WPiece["+i+"]."+xcol) = "White King"
        WKingPos = cObjNm
    end if        
    xPiece = eval("WPiece["+i+"]."+xcol+"Img")
    eval(cObjNm+".default.hbitmap.bmpfile")  = "[PathAlias.ADB_Path]\Engine\Images\\"+xPiece+".png"
    Next i
Next    
text1.text = "New Game, Your Move!"
PlayerTurn = "White"
parentform.Repaint()

The first thing I do is zap a new table called PieceMoves which tracks all moves in the game and will later be used by black to deterine the score of each move both white and black.

Next I append the table grid from our template table, create a list of Column Names A through H, Build our Array from our tablegrid.tbl and finally we use For each … next to find the image name and assign it to our board squares. If you look at that last section, you will see we also identify the position of the Black and White King and assign them to our new variables as well as set

PlayerTurn to = “White”

Resume Game

Resume game works the same except we do not zap PieceMoves or append from our template table. Also Resume determines who’s move it is based on the last stored message.

Select Piece

Has not changed since the last lesson.

Block Capture

BlockCapture now includes PlayerTurn to determine who is playing,Capture now works and also reflects a change to fix an error when the knight was used. The new code is below.

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 = ""
Status = "Open"
if PlayerTurn = "White"
if word(cPieceNm,2," ",1) = "Knight"
    Status = "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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            list = list + square+ ","
            Status = "Open"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Capture"
            list = list + square+ ","
        END IF
    Next
else    

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"
            list = list + square+ ","
        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"
            list = list + square+ ","
        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"
            list = list + square+ ","
        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"
            list = list + square+ ","
        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"
            list = list + square+ ","
        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"
            list = list + square+ ","
        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"
            list = list + square+ ","
        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"
            list = list + square+ ","
        END IF
    end if
next
end if
else
if word(cPieceNm,2," ",1) = "Knight"
    Status = "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 left(eval("WPiece["+Rank+"]."+pos),1) = "O" THEN
            list = list + square+ ","
            Status = "Open"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        END IF
    Next
else    

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) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        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) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        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) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        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) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        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) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        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) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        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) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        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) = "B" THEN
            Status = "Blocked"
        ELSE IF left(eval("WPiece["+Rank+"]."+pos),1) = "W" THEN
            Status = "Capture"
            list = list + square+ ","
        END IF
    end if
next
end if

end if
if List = "" then
    List = cPieceNm + " is Blocked In and cannot move!"
    tmove = "InValid"
    xDo = "Select"
else
    CKList = List
    List = cPieceNm + " Has the Following available moves! "+List
    xDo = "Move"
end if
Movelist = List

A new routine for us is CapturePiece

CapturePiece

dim i as N
dim WPiece[8] as P
WPiece.initialize_from_table("tblgrid")
Rank = val(Right(Alltrim(vMove),1))
pos = left(alltrim(vMove),1)
Capture = eval("WPiece["+Rank+"]."+Alltrim(pos)+"Img")
if PlayerTurn = "White"
for i = 1 to 15 step 1
    if eval("Wc"+Alltrim(str(i))+".Default.Hbitmap.Bmpfile")  <> "[PathAlias.ADB_Path]\Engine\Images\Blank Trans.png"
        
    else if eval("Wc"+Alltrim(str(i))+".Default.Hbitmap.Bmpfile")  = "[PathAlias.ADB_Path]\Engine\Images\Blank Trans.png" 
        eval("Wc"+Alltrim(str(i))+".Default.Hbitmap.Bmpfile") = "[PathAlias.ADB_Path]\Engine\Images"+chr(92)+Alltrim(Capture)+".png"
        eval("topparent:Wc"+Alltrim(str(i))+".Refresh()")
        goto done
    end if
Next i
else
for i = 1 to 15 step 1
    if eval("Bc"+Alltrim(str(i))+".Default.Hbitmap.Bmpfile")  <> "[PathAlias.ADB_Path]\Engine\Images\Blank Trans.png"
        
    else if eval("Bc"+Alltrim(str(i))+".Default.Hbitmap.Bmpfile")  = "[PathAlias.ADB_Path]\Engine\Images\Blank Trans.png" 
        eval("Bc"+Alltrim(str(i))+".Default.Hbitmap.Bmpfile") = "[PathAlias.ADB_Path]\Engine\Images"+chr(92)+Alltrim(Capture)+".png"
        eval("topparent:Bc"+Alltrim(str(i))+".Refresh()")
        goto done
    end if
Next i
end if
done:

This code is called from WhiteMove (which will be renamed to PieceMove later.) and checks which player captured which piece and moves the piece to the correct corral. To accomplish this I added 15 transparent buttoms to each corral and the code finds the captured piece name in our array and assigns the image name to the varialbe Capture which then assigns it to the next open button in the correct corral.

Finally we are going to look at the code in WhiteMove which is the parent routine whcih calls all other sub routines controlling selection and movement. First the entire script.

WhiteMove

'Date Created: 04-Sep-2014 10:04:59 AM
'Last Updated: 24-Sep-2014 01:04:44 PM
'Created By  : cdc
'Updated By  : cdc
vmove = ""
tmove = ""
cObjNm = Current_object()
if xDo = "Select" then
    if WCheckB = .t. .or. BCheckW = .t. then
        script_play("ClearCheck")
    end if    
    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 = PlayerTurn + " 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)
if Left(eval("t."+Left(Alltrim(vMove),1)),1) <> Left(Alltrim(PlayerTurn),1) .and. Left(eval("t."+Left(Alltrim(vMove),1)),1) <> "O" then
    script_play("CapturePiece")
    Capture = eval("t."+Left(Alltrim(vMove),1))
    tmove = "Captured: " + Capture
end if
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)
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"
pm = table.open("piecemoves")
pm.enter_begin()
pm.whitemove = cPieceNm +" From: "+xMove + " to: " + vMove
pm.twhitemove1 = cPieceNm +" From: "+xMove + " to: " + vMove
pm.enter_end()
pm.close()
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()
script_play("VerifyWCkB")
xbasic_wait_for_idle()
if tmove = "White Checked Black"
    text1.text = PlayerTurn+": "+tmove
else    
text1.text = PlayerTurn+": "+cPieceNm+" Moved to.. "+vMove    
end if
if Capture <> "" then
    text1.text = PlayerTurn+": "+tmove
end if
OrigPos = ""
tmove = ""
xPiece = ""
xMove = ""
cPieceNm = ""
NewPOS = ""
vMove = ""
Capture = ""
if PlayerTurn = "White"
    PlayerTurn = "Black"
else
    PlayerTurn = "White"
end if        
xDo = "Select" i
end if
done:

First at the top of our script we call ClearCheck. This code is not written yet and will be included in a later lesson. Now we determine the piece selected and then call selectPiece and BlockCapture to check the legality of the move.

if xDo = "Select" then
    if WCheckB = .t. .or. BCheckW = .t. then
        script_play("ClearCheck")
    end if    
    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"

Once the user select the target for their piece to move to we check the validty of the move. If the move results in a capture, we run CapturePiece

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)
if Left(eval("t."+Left(Alltrim(vMove),1)),1) <> Left(Alltrim(PlayerTurn),1) .and. Left(eval("t."+Left(Alltrim(vMove),1)),1) <> "O" then
    script_play("CapturePiece")
    Capture = eval("t."+Left(Alltrim(vMove),1))
    tmove = "Captured: " + Capture
end if
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)
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()

Now we write the move to our new table PieceMoves and move the piece on the board.

xDo = "Select"
pm = table.open("piecemoves")
pm.enter_begin()
pm.whitemove = cPieceNm +" From: "+xMove + " to: " + vMove
pm.twhitemove1 = cPieceNm +" From: "+xMove + " to: " + vMove
pm.enter_end()
pm.close()
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()

Finally we check to see if the move results in the opponent being checked. To do this we call another new script

VerifyWcheckB

Actually it will work for both players.

VerifyWCheckB

'Date Created: 23-Sep-2014 12:12:02 PM
'Last Updated: 24-Sep-2014 12:07:37 PM
'Created By  : cdc
'Updated By  : cdc
if PlayerTurn = "White"
    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()
    script_play("SelectPiece")
    script_play("BlockorCapture")
dim list as C
list = comma_to_crlf(CkList)
for each sq in List
    if sq = Var->BKingPos
        tmove = "Checked Black"
    end if
Next    
else
    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()
    script_play("SelectPiece")
    script_play("BlockorCapture")
dim list as C
list = comma_to_crlf(CkList)
for each sq in List
    if sq = Var->BKingPos
        tmove = "Checked White"
    end if
Next    
end if

Please notice when this script runs, it re-runs SelectPiece and BlockorCapture. This is necessary to determine if the new location threatens the opponents king.

I know this seems like a lot of work for just managing the moves of pieces on a chess board and it is. However, there is more to come.

Well that’s it for today’s session. The next session will look at the for the script Check Capture and how we will be 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.