Private Sub get_hints(prowcol As String, pcolor As String, pletter As String) As List
hintslst.Clear
Private hilst As List
hilst.Initialize
Select pletter
Case "P"
hilst = pawn_hints(prowcol,pcolor,hilst)
Case "R"
Private is_king As Boolean = False
hilst = rook_hints(prowcol,pcolor,hilst,is_king)
Case "N"
hilst = knight_hints(prowcol,pcolor,hilst)
Case "B"
Private is_king As Boolean = False
hilst = bishop_hints(prowcol,pcolor,hilst,is_king)
Case "Q"
hilst = queen_hints(prowcol,pcolor,hilst)
Case "K"
hilst = king_hints(prowcol,pcolor,hilst)
End Select
Return hilst
End Sub
private Sub pawn_hints(phrowcol As String, phcolor As String, hlst As List) As List
Private phrow As Int = phrowcol.SubString2(0,1)
Private phcol As Int = phrowcol.SubString(1)
If phcolor = "W" Then
If (phrow-1) >= 0 And board((phrow-1),phcol) = "EEE" Then
hlst.Add((phrow-1)&phcol)
End If
If phrow = 6 And (phrow-2) >= 0 And board((phrow-2),phcol) = "EEE" And hlst.IndexOf((phrow-1)&phcol) <> -1 Then
hlst.Add((phrow-2)&phcol)
End If
' take possible
If (phrow-1) >= 0 And (phcol-1) >= 0 And board((phrow-1),(phcol-1)).StartsWith("B") Then
hlst.Add((phrow-1)&(phcol-1))
End If
If (phrow-1) >= 0 And (phcol+1) <= 7 And board((phrow-1),(phcol+1)).StartsWith("B") Then
hlst.Add((phrow-1)&(phcol+1))
End If
' in passing
If phrow = 3 And (phcol-1) >= 0 And board(phrow,(phcol-1)).StartsWith("BP") Then
hlst.Add((phrow-1)&(phcol-1))
End If
If phrow = 3 And (phcol+1) <= 7 And board(phrow,(phcol+1)).StartsWith("BP") Then
hlst.Add((phrow-1)&(phcol+1))
End If
End If
If phcolor = "B" Then
If (phrow+1) <= 7 And board((phrow+1),phcol) = "EEE" Then
hlst.Add((phrow+1)&phcol)
End If
If phrow = 1 And (phrow+2) <= 7 And board((phrow+2),phcol) = "EEE" And hlst.IndexOf((phrow+1)&phcol) <> -1 Then
hlst.Add((phrow+2)&phcol)
End If
' take possible
If (phrow+1) <= 7 And (phcol-1) >= 0 And board((phrow+1),(phcol-1)).StartsWith("W") Then
hlst.Add((phrow+1)&(phcol-1))
End If
If (phrow+1) <= 7 And (phcol+1) <= 7 And board((phrow+1),(phcol+1)).StartsWith("W") Then
hlst.Add((phrow+1)&(phcol+1))
End If
' in passing
If phrow = 4 And (phcol-1) >= 0 And board(phrow,(phcol-1)).StartsWith("WP") Then
hlst.Add((phrow+1)&(phcol-1))
End If
If phrow = 4 And (phcol+1) <= 7 And board(phrow,(phcol+1)).StartsWith("WP") Then
hlst.Add((phrow+1)&(phcol+1))
End If
End If
Return hlst
End Sub
Private Sub rook_hints(rrowcol As String,rcolor As String,hlst As List,iking As Boolean) As List
Private offset As Int
Private rrow As Int = rrowcol.SubString2(0,1)
Private rcol As Int = rrowcol.SubString(1)
' up
offset = 1
Do While (rrow-offset) >= 0
If board((rrow-offset),rcol) = "EEE" Then
hlst.Add((rrow-offset)&rcol)
Else
If board((rrow-offset),rcol).SubString2(0,1) <> rcolor Then
hlst.Add((rrow-offset)&rcol)
End If
Exit
End If
If iking = True Then Exit
offset = offset + 1
Loop
' down
offset = 1
Do While (rrow+offset) <= 7
If board((rrow+offset),rcol) = "EEE" Then
hlst.Add((rrow+offset)&rcol)
Else
If board((rrow+offset),rcol).SubString2(0,1) <> rcolor Then
hlst.Add((rrow+offset)&rcol)
End If
Exit
End If
If iking = True Then Exit
offset = offset + 1
Loop
' left
offset = 1
Do While (rcol-offset) >= 0
If board(rrow,(rcol-offset)) = "EEE" Then
hlst.Add(rrow&(rcol-offset))
Else
If board(rrow,(rcol-offset)).SubString2(0,1) <> rcolor Then
hlst.Add(rrow&(rcol-offset))
End If
Exit
End If
If iking = True Then Exit
offset = offset + 1
Loop
' right
offset = 1
Do While (rcol+offset) <= 7
If board(rrow,(rcol+offset)) = "EEE" Then
hlst.Add(rrow&(rcol+offset))
Else
If board(rrow,(rcol+offset)).SubString2(0,1) <> rcolor Then
hlst.Add(rrow&(rcol+offset))
End If
Exit
End If
If iking = True Then Exit
offset = offset + 1
Loop
' castling
Return hlst
End Sub
Private Sub knight_hints(nrowcol As String,ncolor As String,hlst As List) As List
Private nrow As Int = nrowcol.SubString2(0,1)
Private ncol As Int = nrowcol.SubString(1)
' 8 places 2-1 or 1-2
' lower half
If (nrow+2) <= 7 And (ncol-1) >= 0 Then
If board((nrow+2),(ncol-1)) = "EEE" Then
hlst.Add((nrow+2)&(ncol-1))
Else
If board((nrow+2),(ncol-1)).SubString2(0,1) <> ncolor Then
hlst.Add((nrow+2)&(ncol-1))
End If
End If
End If
If (nrow+2) <= 7 And (ncol+1) <= 7 Then
If board((nrow+2),(ncol+1)) = "EEE" Then
hlst.Add((nrow+2)&(ncol+1))
Else
If board((nrow+2),(ncol+1)).SubString2(0,1) <> ncolor Then
hlst.Add((nrow+2)&(ncol+1))
End If
End If
End If
If (nrow+1) <= 7 And (ncol-2) >= 0 Then
If board((nrow+1),(ncol-2)) = "EEE" Then
hlst.Add((nrow+1)&(ncol-2))
Else
If board((nrow+1),(ncol-2)).SubString2(0,1) <> ncolor Then
hlst.Add((nrow+1)&(ncol-2))
End If
End If
End If
If (nrow+1) <= 7 And (ncol+2) <= 7 Then
If board((nrow+1),(ncol+2)) = "EEE" Then
hlst.Add((nrow+1)&(ncol+2))
Else
If board((nrow+1),(ncol+2)).SubString2(0,1) <> ncolor Then
hlst.Add((nrow+1)&(ncol+2))
End If
End If
End If
' upper half
If (nrow-2) >= 0 And (ncol-1) >= 0 Then
If board((nrow-2),(ncol-1)) = "EEE" Then
hlst.Add((nrow-2)&(ncol-1))
Else
If board((nrow-2),(ncol-1)).SubString2(0,1) <> ncolor Then
hlst.Add((nrow-2)&(ncol-1))
End If
End If
End If
If (nrow-2) >= 0 And (ncol+1) <= 7 Then
If board((nrow-2),(ncol+1)) = "EEE" Then
hlst.Add((nrow-2)&(ncol+1))
Else
If board((nrow-2),(ncol+1)).SubString2(0,1) <> ncolor Then
hlst.Add((nrow-2)&(ncol+1))
End If
End If
End If
If (nrow-1) >= 0 And (ncol-2) >= 0 Then
If board((nrow-1),(ncol-2)) = "EEE" Then
hlst.Add((nrow-1)&(ncol-2))
Else
If board((nrow-1),(ncol-2)).SubString2(0,1) <> ncolor Then
hlst.Add((nrow-1)&(ncol-2))
End If
End If
End If
If (nrow-1) >= 0 And (ncol+2) <= 7 Then
If board((nrow-1),(ncol+2)) = "EEE" Then
hlst.Add((nrow-1)&(ncol+2))
Else
If board((nrow-1),(ncol+2)).SubString2(0,1) <> ncolor Then
hlst.Add((nrow-1)&(ncol+2))
End If
End If
End If
Return hlst
End Sub
Private Sub bishop_hints(browcol As String,bcolor As String,hlst As List,iking As Boolean) As List
Private offset As Int
Private brow As Int = browcol.SubString2(0,1)
Private bcol As Int = browcol.SubString(1)
' left up
offset = 1
Do While (brow-offset) >= 0 And (bcol-offset) >= 0
If board((brow-offset),(bcol-offset)) = "EEE" Then
hlst.Add((brow-offset)&(bcol-offset))
Else
If board((brow-offset),(bcol-offset)).SubString2(0,1) <> bcolor Then
hlst.Add((brow-offset)&(bcol-offset))
End If
Exit
End If
If iking = True Then Exit
offset = offset + 1
Loop
' left down
offset = 1
Do While (brow+offset) <= 7 And (bcol-offset) >= 0
If board((brow+offset),(bcol-offset)) = "EEE" Then
hlst.Add((brow+offset)&(bcol-offset))
Else
If board((brow+offset),(bcol-offset)).SubString2(0,1) <> bcolor Then
hlst.Add((brow+offset)&(bcol-offset))
End If
Exit
End If
If iking = True Then Exit
offset = offset + 1
Loop
' right up
offset = 1
Do While (brow-offset) >= 0 And (bcol+offset) <= 7
If board((brow-offset),(bcol+offset)) = "EEE" Then
hlst.Add((brow-offset)&(bcol+offset))
Else
If board((brow-offset),(bcol+offset)).SubString2(0,1) <> bcolor Then
hlst.Add((brow-offset)&(bcol+offset))
End If
Exit
End If
If iking = True Then Exit
offset = offset + 1
Loop
' right down
offset = 1
Do While (brow+offset) <= 7 And (bcol+offset) <= 7
If board((brow+offset),(bcol+offset)) = "EEE" Then
hlst.Add((brow+offset)&(bcol+offset))
Else
If board((brow+offset),(bcol+offset)).SubString2(0,1) <> bcolor Then
hlst.Add((brow+offset)&(bcol+offset))
End If
Exit
End If
If iking = True Then Exit
offset = offset + 1
Loop
Return hlst
End Sub
Private Sub queen_hints(qrowcol As String,qcolor As String,hlst As List) As List
Private is_king As Boolean = False
' rook hints
hlst = rook_hints(qrowcol,qcolor,hlst,is_king)
' bishop hints
Private hlst1 As List
hlst1.Initialize
hlst1 = bishop_hints(qrowcol,qcolor,hlst1,is_king)
hlst.AddAll(hlst1)
Return hlst
End Sub
Private Sub king_hints(krowcol As String,kcolor As String,hlst As List) As List
Private is_king As Boolean = True
' rook hints 1 cell
hlst = rook_hints(krowcol,kcolor,hlst,is_king)
' bishop hints 1 cell
Private hlst1 As List
hlst1.Initialize
hlst1 = bishop_hints(krowcol,kcolor,hlst1,is_king)
hlst.AddAll(hlst1)
Return hlst
End Sub
Private Sub show_hints(hlst As List)
For i = 0 To hlst.Size -1
Dim hrowcol As String = hlst.Get(i)
Dim pnl1 As Panel = get_board_panel(hrowcol,pnlmain)
cnvscell.Initialize(pnl1)
show_cell_border(Colors.Green,10dip,cnvscell)
Next
End Sub
The get_board_panel subroutine is used to get the panel from the list.Public Sub get_board_panel(pnlrowcol As String,pnlboard As Panel) As Panel
For Each v As B4XView In pnlboard.GetAllViewsRecursive
If v Is Panel Then
If v.Tag = pnlrowcol Then
Return v
End If
End If
Next
Return Null
End Sub
The subroutine get_hints is called in the pnl_Click subroutine and after that the show_hints routine is called. Private pcscolor As String = srctext.SubString2(0,1)
Private pcsletter As String = srctext.SubString2(1,2)
hintslst = get_hints(rowcol,pcscolor,pcsletter)
Log("hintslst: " & hintslst)
show_hints(hintslst)
So now you are ready to test the app, but first we have to fix the problem with the vanishing pieces. If hintslst.IndexOf(rowcol) = -1 Then
refresh_board
clickcnt = 0
Return
End If
Now if you tap on the same panel or on a panel that is not in the hints list then the board is refreshed, the click count is reset to 0 and with the return statement we leave the subroutine.