Android Tutorial Hangman Tutorial (part 3)

Alright, here is part 3 of the Hangman Tutorial.

In part 2, we added the ability to import our words from an external text file by using File.ReadList to generate a List (which we stored in the variable wordList).

If you look at the flow chart in part 1, you can see what our next step is going to be. It may not be completely clear to you yet why we are going to check to see if wordList is empty (it actually sounds pretty silly considering we just added 750 words to our wordList), but there is a good reason for us doing this and I will explain in a bit....... but for right now, lets actually look at the step that follows checking to make sure wordList is empty...

The next step is to create a function (a sub, that returns a value) that will do two things-
1. Pick a random word out of the wordList (and return this word as a string so we can use it)
2. remove this word from the wordList (so we will not randomly choose this word again until all of the other words have been used)

Now that you know this step, I can go back and explain why we want to check to see if wordList is empty- because each time we pick a new random word, we remove it from the wordList, and at some point we will not have any more words left in our list! SO, we need to make sure that when we are about to get a new word that there is actually one to get. If there is not, we are going to fill our wordList back up by re-loading the words from the text file back into our wordList.


Sorry, I have to come back and finish part 3 tomorrow... I promised my wife I would watch some TV with her before bed ;)

This isn't the final version of the code we will be using, but here is some code you can play with until I can get back to completing part 3:

B4X:
Sub Pick_New_Word() As String
    If wordList.Size > 1 Then
        Dim r As Int = Rnd(0, wordList.Size-1)
        currentWord = wordList.get(r)
        wordList.RemoveAt(r)
        Return currentWord
    Else
        wordList = File.ReadList(File.DirAssets, "words.txt")
        Dim r As Int = Rnd(0, wordList.Size-1)
        currentWord = wordList.get(r)
        wordList.RemoveAt(r)
        Return currentWord
    End If
End Sub


Oh, and here is some other code that will be heavily modified for a future lesson, but you can play with it as well:
B4X:
Sub Globals
    Dim wordList As List
    Dim lbl1 As Label
    Dim currentWord As String
    Dim Buttons(10, 3) As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    lbl1.initialize("lbl1")
    Activity.AddView(lbl1, 300dip, 10dip, 400dip, 50dip)
  
    Dim width, offsetX, offsetY As Int
    width = 50dip
    offsetX = (100%x - width * 10 - 2dip * 9) / 2
    offsetY = (100%y - width * 3 - 2dip * 2) - 5dip
  
    For x = 0 To 9
        For y = 0 To 2
            Dim b As Button
            b.Initialize("button") 'All buttons share the same event sub
            b.TextSize = 30
            Activity.AddView(b,offsetX + x * (width + 2dip), offsetY + y * (width + 2dip), width, width)
            Buttons(x, y) = b 'store a reference to this view
        Next
    Next
  
  
    wordList = File.ReadList(File.DirAssets, "words.txt")
  
    lbl1.Text = Pick_New_Word  
  
    'For i = 1 To wordList.Size *2
    '    Log(Pick_New_Word & wordList.Size)
    'Next
     
End Sub

Sub Pick_New_Word() As String
    If wordList.Size > 1 Then
        Dim r As Int = Rnd(0, wordList.Size-1)
        currentWord = wordList.get(r)
        wordList.RemoveAt(r)
        Return currentWord
    Else
        wordList = File.ReadList(File.DirAssets, "words.txt")
        Dim r As Int = Rnd(0, wordList.Size-1)
        currentWord = wordList.get(r)
        wordList.RemoveAt(r)
        Return currentWord
    End If
End Sub

See you tomorrow for the rest of part 3 (sorry gotta run now :) )
 

Eric H

Active Member
Licensed User
Longtime User
Just wanted to reassure everyone interested in this thread that I haven't forgotten about it. I have been working a ton of overtime at work and have also been taking care of projects at home in preparation for our first child that is due in a little over a month. My current avatar is actually me, when I was younger. I can't wait until my son is that age so I can take a picture of him wearing MY headphones. :)

Thanks for the patience all. Feel free to chime in, ask questions about your hangman project, or suggest things to include in the next parts of this tutorial. I can likely take the time to help you out if you're stuck, even before I am able to resume this tutorial.

Thanks again good folks of the B4a community :)
 

Beja

Expert
Licensed User
Longtime User
Eric,
Congratulation with your new family member.. wish her/him a great bright future and I am sure will be b4a genius!

Now you'r busy (and no sleep) just take your time.. I watched this movie 4 times :)

For your tutorial, I give you triple A credit for documentation.
 
Top