2. Switches panel
Before we can use the switches we have to fill the clvinfo CustomListView with the layouts.
This can be done with the following code:
Private Sub fill_clvinfo
clvinfo.Clear
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0, 5dip, 5dip, 320dip, 70dip)
pnl.LoadLayout("switches_layout")
clvinfo.Add(pnl, 0)
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0, 5dip, 5dip, 320dip, 185dip)
pnl.LoadLayout("taken_layout")
clvinfo.Add(pnl, 1)
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0, 5dip, 5dip, 320dip, 55dip)
pnl.LoadLayout("notes_layout")
clvinfo.Add(pnl, 2)
End Sub
This subroutine is called in the B4XPage_Created routine. You can insert this line at the end of the subroutine.
fill_clvinfo
Now, you can use the switch (bswhints) to show or hide the hints.
In the show_hints subroutine you can add this code as the first lines:
If bswhints.Value = False Then
Return
End If
When the switch is on the hints are shown and when the switch is off the hints are hidden but are still used to verify the move of a piece. The player has to know where the piece can go.
For the black view switch to work we need to modify the subroutine put_bitmap_in_cell.
Replace the line with the bmp.initialize function with these lines:
If bswblackview.Value = False Then
bmp.Initialize(File.DirAssets, imagemap.Get(pce))
Else
If turncolor = "black" Then
bmp.Initialize(File.DirAssets, "flip_" & imagemap.Get(pce))
Else
bmp.Initialize(File.DirAssets, imagemap.Get(pce))
End If
End If
This code will be executed when the turncolor is black.
So far a player could make more than one move after another and didn't have to wait for his or her turn.
We need to fix this. Create a subroutine switch_turn as follows:
Private Sub switch_turn
If turncolor = "white" Then
turncolor = "black"
cnvs.DrawRect(TurnRect, Colors.ARGB(255,244,244,194), True, 1dip)
cnvs.DrawText(turncolor & " to move",140dip,20dip,tpfont,16,Colors.Blue,"LEFT")
Else
turncolor = "white"
cnvs.DrawRect(TurnRect, Colors.ARGB(255,244,244,194), True, 1dip)
cnvs.DrawText(turncolor & " to move",140dip,20dip,tpfont,16,Colors.Blue,"LEFT")
End If
If bswblackview.Value = True Then refresh_board
End Sub
Notice that if the blackview switch is on then the refresh_board subroutine is called to flip the images.
This subroutine is called in the pnl_Click subroutine above the last endif statement (after the move has been made).
switch_turn
To enforce the switch_turn subroutine we need to test who's turn it is at the first tap.
Below the line log("source:" ... add the following lines to test the color:
If srctext.StartsWith("B") And turncolor = "white" Then
xui.MsgboxAsync("White to move","Turn")
Return
End If
If srctext.StartsWith("W") And turncolor = "black" Then
xui.MsgboxAsync("Black to move","Turn")
Return
End If
Now the player is warned about who's turn it is.
When it's the black player's turn the images on the board are flipped. This way the black player can see the pieces in a normal way and not upside down. When it's the white players turn the pieces are flipped again.
Here's how it should look like:
The save button is used in the Load and Save section and the study button is used in the Study board section.