Make the pieces moveList
Make the pieces move
1. Moving a piece
In the subroutine set_panel each panel from the board gets a tag with the row and column number of the cell (or square). The numbers are concatenated to a string.The click event for a panel is handled by the subroutine pnl_Click because each panel is initialized with the "pnl" event name.
The rowcol string variable in the pnl_Click subroutine holds the tag string.
Here's a first version of the pnl_Click subroutine:
Private Sub pnl_Click
Dim pnl As Panel = Sender
Dim rowcol As String = pnl.tag
Log("pnl_click: " & rowcol)
If clickcnt < 1 Then
srctext = pnl.GetView(0).As(Label).Text
Log("source: " & srctext)
clickcnt = 1
Else
desttext = pnl.GetView(0).As(Label).Text
Log("destination: " & desttext)
clickcnt = 0
End If
End Sub
End Sub
To make a piece move the user has to tap (or click) on a panel the first time and then tap on another panel the second time. For this we use the clickcnt variable (click count).To get the content of the cell we get the hidden label text that is on that panel. The label is the first (0) and only view on each panel.
The srctext and desttext string variables hold the rowcol string. These variables are later used in the code.
The variables to be declared:
Private clickcnt As Int = 0
Private srctext As String = ""
Private desttext As String = ""
To test the click event just add the subroutine and the variable declarations and run the app.Verify the log panel for the correct information.
2. Show a border
To show which panel was tapped we draw a colored border on the selected panel.Add these 2 lines in the pnl_Click subroutine below the line with the log("source: "... and log("Destination: "...
cnvscell.Initialize(pnl)
show_cell_border(Colors.Green,10dip,cnvscell)
And then add the following subroutine to show a green border:Private Sub show_cell_border(bordercolor As Int, thick As Int, cnv As Canvas)
cnv.DrawRect(BorderRect, bordercolor, False, thick)
cnv.DrawRect(BorderRect, Colors.Black, False, 1dip)
End Sub
Test the app and you will see a green border on each panel you tap.This is how it looks like:

Private Sub refresh_board
pnlmain.RemoveAllViews
For r = 0 To 7
For c = 0 To 7
Private pnl As Panel = set_panel(r,c)
pnl.GetView(0).As(Label).Text = board(r,c)
pnlmain.AddView(pnl,30dip+(c*40dip),30dip+(r*40dip),40dip,40dip)
put_bitmap_in_cell(r,c,pnl,board(r,c))
Next
Next
End Sub
This subroutine is called after the board array has changed. Insert this line above the clickcnt = 0 line in the pnl_Click subroutine. refresh_board
Now if the user taps a second time on a panel the board is refreshed and the borders disappear.3. Do the move
To move a piece from one panel to another panel the board array needs to be changed.The destination cell gets the source piece information and the source cell gets the empty piece information.
This is the do_move subroutine so far:
Private Sub do_move
Private srow As Int = srcrowcol.SubString2(0,1)
Private scol As Int = srcrowcol.SubString(1)
Private drow As Int = destrowcol.SubString2(0,1)
Private dcol As Int = destrowcol.SubString(1)
board(drow,dcol) = srctext
board(srow,scol) = "EEE"
End Sub
This routine is called before the refresh_board call in the pnl_Click subroutine.The srcrowcol and destrowcol string variables contain the piece string from the first panel and from the second panel. These variables are set in the pnl_Click subroutine. Don't forget to declare these variables in the Class_Globals routine.
The pnl_Click subroutine should look like this now (you can replace the old one with this one):
Private Sub pnl_Click
Dim pnl As Panel = Sender
Dim rowcol As String = pnl.tag
Log("pnl_click: " & rowcol)
If clickcnt < 1 Then
srctext = pnl.GetView(0).As(Label).Text
Log("source: " & srctext)
cnvscell.Initialize(pnl)
show_cell_border(Colors.Green,10dip,cnvscell)
srcrowcol = rowcol
clickcnt = 1
Else
desttext = pnl.GetView(0).As(Label).Text
Log("destination: " & desttext)
cnvscell.Initialize(pnl)
show_cell_border(Colors.Green,10dip,cnvscell)
destrowcol = rowcol
do_move
refresh_board
show_board_log
clickcnt = 0
End If
End Sub
Run the app and test the moves. Look at the log panel for the changes to the board.Now you should have an almost working chess game.
BUT: the user can make a valid move or an invalid move!
And if you tap twice on the same panel the piece vanishes completely!
