Cursussen/Courses Codesnippets     Top 
B4A-chess - Load and save


1. Show a dialog
When the save button is clicked a B4Xdialog will be shown.
For each saved game we create a folder in the Games folder.
Note: we are using the RuntimePermissions library.
The saved games are listed in the files list (clvfiles) from the dialog.
Let's declare and initialize the variables.
In the Class_Globals routine:
	Private savedialog As B4XDialog
	Private gamesfolder As String
	Private rtpermissions As RuntimePermissions
In the B4XPage_Created routine above the hintslst.initialize line:
	savedialog.Initialize(Root)
	savedialog.PutAtTop = True
	savedialog.BackgroundColor = Colors.LightGray
	gamesfolder = rtpermissions.GetSafeDirDefaultExternal("Games")
And the show_savedialog looks like this:
private Sub show_savedialog
	Dim pnl As B4XView = xui.CreatePanel("")
	pnl.SetLayoutAnimated(0, 0, 0, 300dip, 200dip)
	pnl.LoadLayout("savedialog_layout")
	edtfilename.Text = ""
	Dim rsub As ResumableSub =  savedialog.ShowCustom(pnl, "SAVE", "LOAD", "CANCEL")
	fill_clvfiles
	Wait For (rsub) Complete (Result As Int)
	If Result = xui.DialogResponse_Positive Then
		If edtfilename.Text <> "" Then
			If File.Exists(gamesfolder, edtfilename.Text) = False Then
				save_game(edtfilename.Text)
			Else
				xui.MsgboxAsync("The game name already exists.Choose another one!","Game")
			End If
		Else
			xui.MsgboxAsync("The game name is empty!","Game")
		End If
	End If
	If Result = xui.DialogResponse_Negative Then
		If edtfilename.Text <> "" Then
			If File.Exists(gamesfolder, edtfilename.Text) = True Then
				load_game(edtfilename.Text)
			Else
				xui.MsgboxAsync("Select an existing game name first!","Game")
			End If
		Else
			xui.MsgboxAsync("Select a game name first!","Game")
		End If
	End If
End Sub
In the btnsave_Click subroutine we call the show_savedialog routine:
Private Sub btnsave_Click
	show_savedialog
End Sub
This is how the dialog should look like:|


2. Fill the files list
The savedialog contains a files list.
With the fill_clvfiles subroutine we get the games folder names from the main "Games" folder.
In the clvfiles_ItemClick subroutine the variable edtfilename text is set to the selected name.
If you longclick or hold your finger on a item in the list then the delete game question is asked. This allows you to delete game folders you no longer need.
Private Sub fill_clvfiles
	clvfiles.Clear
	Private lst As List = File.ListFiles(gamesfolder)
	For i = 0 To lst.Size - 1
		clvfiles.AddTextItem(lst.get(i),lst.get(i))
	Next
End Sub
Private Sub clvfiles_ItemClick (Index As Int, Value As Object)
	edtfilename.Text = clvfiles.GetValue(Index)
End Sub
Private Sub clvfiles_ItemLongClick (Index As Int, Value As Object)
	' delete game folder and files
	Dim answ As Object = xui.Msgbox2Async("Delete game?","Delete","yes","","no",Null)
	Wait For (answ) Msgbox_Result (Result As Int)
	If Result = xui.DialogResponse_Positive Then
		Dim folder As String = clvfiles.GetValue(Index)
		File.Delete(gamesfolder & "/" & folder,"notes.txt")
		File.Delete(gamesfolder & "/" & folder,"board.txt")
		File.Delete(gamesfolder & "/" & folder,"taken.txt")
		File.Delete(gamesfolder,folder)
	End If
	fill_clvfiles
End Sub


3. Save a game
In this subroutine the game is saved.
The routine tests if the game folder exists and if it doesn't exist then a folder with the game name is created.
The board two-dimensional array is turned into a string and saved as a string in a text file using the WriteString function.
The noteslst arraylist is saved in a text file using the WriteList function.
The takenmap is saved in a text file using the WriteMap function.
Private Sub save_game(foldername As String)
	Private boardstr As String = ""
	For r = 0 To 7
		For c = 0 To 7
			If c < 7 Then
				boardstr = boardstr & board(r,c) & ","
			Else
				boardstr = boardstr & board(r,c)
			End If
		Next
		boardstr = boardstr & CRLF
	Next
	Log("save notes: " & noteslst)
	Log("save board: " & boardstr)
	Log("save takenmap: " & takenmap)
	If File.Exists(gamesfolder,foldername) = False Then
		File.MakeDir(gamesfolder,foldername)
	End If
	File.WriteList(gamesfolder & "/" & foldername,"notes.txt",noteslst)
	File.WriteString(gamesfolder & "/" & foldername,"board.txt",boardstr)
	File.WriteMap(gamesfolder & "/" & foldername,"taken.txt",takenmap)
End Sub


4. Load a game
When the user taps on the save button and selects a game name in the list then that game can be loaded.
The players can continue playing from the last saved board situation.
The board array is refilled with the information from the text string.
The noteslst list is read from the text file and the notes panels are added to the clvinfo list.
The takenmap is read from the text file and filled. The panels are added to the taken panel (pnltaken).
Private Sub load_game(foldername As String)
	' reset board and info list
	is_study = False
	turncolor = "white"
	setup_board
	fill_clvinfo
	btrow = 0
	btcol = 0
	wtrow = 2
	wtcol = 0
	' load the textfiles from the folder
	Private boardlst As List
	Private rowstr As String
	Private r As Int = 0
	boardlst = File.ReadList(gamesfolder & "/" & foldername,"board.txt")
	For i = 0 To boardlst.Size - 1
		rowstr = boardlst.get(i)
		For c = 0 To 7
			board(r,c) = rowstr.SubString2(0+(c*4),3+(c*4))
		Next
		r = r + 1
	Next
	show_board_log
	refresh_board
	noteslst = File.ReadList(gamesfolder & "/" & foldername,"notes.txt")
	Log(noteslst)
	clvinfo.RemoveAt(2)
	For i = 0 To noteslst.Size - 1
		Dim pnl As B4XView = xui.CreatePanel("")
		pnl.SetLayoutAnimated(0, 5dip, 5dip, 320dip, 55dip)
		pnl.LoadLayout("notes_layout")
		lblnotes.RemoveView
		lblnotes.Text = noteslst.Get(i)
		pnl.AddView(lblnotes,5dip,5dip,pnl.Width-5dip,40dip)
		clvinfo.Add(pnl,(i+2))
	Next
	notescnt = noteslst.Size + 1
	Dim pnl As B4XView = xui.CreatePanel("")
	pnl.SetLayoutAnimated(0, 5dip, 5dip, 320dip, 55dip)
	pnl.LoadLayout("notes_layout")
	clvinfo.Add(pnl, (notescnt+2))
	takenmap.Clear
	takenmap = File.ReadMap(gamesfolder & "/" & foldername,"taken.txt")
	Log(takenmap)
	For Each key In takenmap.Keys
		Dim pcs As String = key
		Dim filename As String = takenmap.Get(key)
		add_piece_to_pnltaken(pcs,filename)
	Next
End Sub