Cursussen/Courses Codesnippets     Top 
B4A-chess - Study board


1. Show studyboard
The following subroutines use some variables. The declarations go in the Class_Globals subroutine:
	Private instrow As Int = 0
	Private is_study As Boolean = False
	Private studypce As String
When the study button is clicked the first thing we do is show the empty studyboard.
The board gets filled with empty pieces.
Private Sub show_studyboard
	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 = "EEE"
			pnlmain.AddView(pnl,30dip+(c*40dip),30dip+(r*40dip),40dip,40dip)
			board(r,c) = pnl.GetView(0).As(Label).Text
			put_bitmap_in_cell(r,c,pnl,board(r,c))
			grid(r,c) = cletters.SubString2(c,c+1) & rnumbers.SubString2(r,r+1)
		Next
	Next
	set_sides(cnvs,tpfont)
End Sub


2. Show instructions
The taken panel (pnltaken) is used to give instructions and to present the chess pieces to select from.
Here's the code:
Private Sub show_pieces_with_instructions
	pnltaken.RemoveAllViews
	Private lbl As Label
	lbl.Initialize("")
	lbl.Text = "Select a piece and tap on a board cell"
	lbl.Typeface = Typeface.DEFAULT_BOLD
	lbl.TextSize = 18
	lbl.Visible = True
	pnltaken.AddView(lbl,5dip,5dip+(instrow*40dip),300dip,40dip)
	' black
	For i = 0 To 4
		Dim piece As String = bpcs1.Get(i)
		Dim leftpos As Int = 10dip+(i*50dip)
		set_piece_panel_with_image(piece, leftpos,40dip)		
	Next
	Dim piece As String = bpcs2.Get(0)
	Dim leftpos As Int = 260dip
	set_piece_panel_with_image(piece, leftpos,40dip)
	' white
	For i = 0 To 4
		Dim piece As String = wpcs1.Get(i)
		Dim leftpos As Int = 10dip+(i*50dip)
		set_piece_panel_with_image(piece, leftpos,90dip)
	Next
	Dim piece As String = wpcs2.Get(0)
	Dim leftpos As Int = 260dip
	set_piece_panel_with_image(piece, leftpos,90dip)
	Private lbl As Label
	lbl.Initialize("")
	lbl.Text = "Tap the study button to start playing."
	lbl.Typeface = Typeface.DEFAULT_BOLD
	lbl.TextSize = 18
	lbl.Visible = True
	pnltaken.AddView(lbl,5dip,140dip,300dip,40dip)
End Sub
This is how it looks:


3. Place a piece
The piece panels are created in this subroutine:
Private Sub set_piece_panel_with_image(pce As String, lpos As Int,tpos As Int)
	Private pnl As Panel
	pnl.Initialize("pieces")
	pnl.Tag = pce
	Private lbl As Label
	lbl.Initialize("")
	lbl.Text = pce
	lbl.Visible = False
	pnl.AddView(lbl,0,0,40dip,40dip)
	pnltaken.AddView(pnl,lpos,tpos,40dip,40dip)
	Dim rect As Rect
	rect.Initialize(0dip,0dip,40dip,40dip)
	cnvscell.Initialize(pnl)
	cnvscell.DrawRect(rect, Colors.Black, False, 1dip)
	Dim bmp As Bitmap
	bmp.Initialize(File.DirAssets, imagemap.Get(pce))
	cnvscell.DrawBitmap(bmp, Null, rect)
End Sub


4. Click events
When the player taps on the study button then the study board and the instructions panel are shown.
The second time the study button is clicked (is_study boolean is true) the game can be played with the board setup.
The clvinfo list is refilled and is ready to register the taken pieces and notes.
Private Sub btnstudy_Click
	If is_study = True Then
		is_study = False
		show_board_log
		fill_clvinfo
		btrow = 0
		btcol = 0
		wtrow = 2
		wtcol = 0
		turncolor = "white"
		notescnt = 1
		Return
	End If
	is_study = True
	show_studyboard
	show_board_log
	fill_clvinfo
	instrow = 0
	show_pieces_with_instructions
End Sub
When the player taps on a piece in the instructions panel then that piece gets a blue border to indicate that its ready to be placed on the board. The variable studypce holds the piece information.
Private Sub pieces_Click
	Dim pnl As Panel = Sender
	Dim pce As String = pnl.tag
	Log("pieces click: " & pce)
	cnvscell.Initialize(pnl)
	show_cell_border(Colors.Blue,10dip,cnvscell)
	studypce = pce
End Sub
The second tap on the board is captured in the pnl_Click subroutine.
Add these lines of code below the line with Log("pnl_click: " ...:
	If is_study = True Then
		Dim row As Int = rowcol.SubString2(0,1)
		Dim col As Int = rowcol.SubString(1)
		board(row,col) = studypce
		refresh_board
		show_pieces_with_instructions
		Return
	End If
Now you are ready to make chess puzzles and try them out. Or you could find puzzles online and put them in the app for later studying.
You can play a game in several time periods and saving the state of the game each time the period stops. The next time you only need to load up the game and continue playing.
If you have a tablet then you might want to look at the screen adjustments in the next section.