Android Tutorial Hangman Tutorial (part 2)

Ok, we are back for part 2 of the hangman tutorial. If you haven't seen part 1, here is a link: http://www.b4x.com/android/forum/threads/hangman-tutorial-part-1.32893/

In part 1 we designed a flowchart for the main operation of our hangman game. In part 2 we are going to look at the first part of the flowchart- generating the word list from the word database. For simplicity, we are going to use a simple text file instead of a database, but the idea is the same.

So to begin, we need to create a list of words that we will use in our game. I created my list as a plain text file with each word on it's own line (no commas, not a csv list). I will upload my list for you to use if you like (words.txt), or you can create your own list. My list contains 750 different words.

In order to use your text file word list, you must add it to your project by clicking on the FILES tab on the bottom right of the Basic4Android IDE window. Then click on ADD FILES and navigate to your word list text file.

In the IDE code area, we need to set up a new List variable. A List is basically a variable that allows you to store more than one value. In our case, our list is going to start out containing all 750 of the words (or however many you have) in our text file.

So, to tell Basic4Android that we want a new List called wordList, we will place the following code in the Sub Globals code section:
B4X:
Dim wordList As List
Now Basic4Android knows we want to use our variable wordList to store a List in, and we can use this variable in our code.

The next thing we want to do is add all of the words in our text document to our wordList. To do this, we can use this code in the Sub Activity_Create code section:
B4X:
wordList = File.ReadList(File.DirAssets, "words.txt")
To understand what that is doing, it is helpful to look at the tooltip for File.ReadList:

"File.ReadList - Reads a file and stores each line as an item in a list."

Usage: File.ReadList(Dir As String, FileName As String) As List
The two items inside the parentheses are the values we provide for the directory of our text file, and the filename of our text file. Since you added the file to your project using the 'ADD FILES' option mentioned above in bold, the directory we can call for the location is File.DirAssets.

My text file is named words.txt, so you must change the filename in the code to match your text file name.

The 'As List' at the end of the code tells us the RETURN TYPE of the File.ReadList function. In this case, File.ReadList returns a list, and we can assign the value that is returned (the list) to the variable we declared 'wordList'.

(for more info on working with text files: http://www.b4x.com/android/forum/threads/text-files.6690/)

So to wrap up part 2, we began coding the first part of our flowchart (see part 1 for image). We now have our list of words contained in a List variable called wordList. To test this out, you can add the following code to your Sub Activity_Create code area (make sure it is after the File.ReadList code we entered)
B4X:
For Each i In wordList
    Log(i)
Next
This will display all of the words in your list in the LOGS tab of the IDE (click the LOGS tab on the bottom right of the IDE window and run your program).

Thanks for checking out part 2 of this tutorial. Make sure to watch this thread for updates.
 

Attachments

  • words.txt
    5.4 KB · Views: 558

77_Mark_Gresham

New Member
First. I apologize if this ends up being a duplicate. It seems like my message may not have gone thru? Anyways... Hi. My name is Ed. I was msg. about your article. I had some questions for you, such as in regards to your database mention and the txt file. Just a side note. Your article is very well written. :) I hope to chat more. Thx!
 

Similar Threads

Top