With ample time on hand past Saturday, I decided to dig up some past tutorials just to refresh some material that may have slipped my mind. I have found that going through old stuff once in a while is a handy way to polish ASP .NET skills - or any programming environment for that matter. So, the first time that caught my eye was a Hangman tutorial by Scott Mitchell at 4GuysFromRolla. 4GuysFromRolla is a great place to learn about ASP .NET; I do a tutorial every night or every week (depending on the complexity and the series) to stay current with this ever changing dynamic platform.
I decided to use the Hangman tutorial and re-code it using different classes and logic. Here are parts and pieces of the changed code that I would like to share with the readers here. I have kept the method names same as the one found on 4GuysFromRolla for easy comparison.
1 Protected Sub generateLetterBoard()
2 'generates the letters from A-Z and creates linkbuttons
3 'called in page_PreInit()
4
5 Dim i As Integer = 0
6 Dim j As Integer = 65
7
8 For i = 0 To 25
9 If j < 92 Then
10 Dim myLinkButton As New LinkButton()
11 myLinkButton.ID = ChrW(j).ToString()
12 myLinkButton.Text = ChrW(j).ToString()
13 myLinkButton.CommandArgument = ChrW(j).ToString()
14 AddHandler myLinkButton.Command, AddressOf LetterGuessed
15 form1.Controls.Add(myLinkButton)
16 End If
17 j = j + 1
18 Next
19 End Sub
As you can see, instead of creating link buttons singularly in the source view, I took the approach of dynamically creating the link buttons. What the above snippet does is it declares a variable to loop 26 times for each alphabet in the English language. The inner loop then executes to display letters; letters are extracted from their ASCII values (65 represents an uppercase A, 90 represents an uppercase Z). The inner loop, then, create a link button, and assigns its Text, ID, and CommandArgument properties. It then adds an event handler to handle the Command event. Command event is raised each time that a link button is clicked. Finally, the link button is added to the form's controls collection.
Here is an ASCII chart for reference (taken from http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm).
The second piece of code that I changed was getRandomWordFromTextFile() method. Here's the implementation:
1 Protected Function getRandomWordFromTextFile(ByVal filePath As String) As String
2 'Dim myFileStream As New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)
3 'Dim words As New ArrayList()
4 'Dim myStreamReader As New StreamReader(myFileStream)
5
6 'While myStreamReader.Peek > -1
7 ' words.Add(myStreamReader.ReadLine())
8 'End While
9
10 'myFileStream.Close()
11 'myStreamReader.Close()
12
13 'Dim randomNumberGenerator As New Random()
14 'Dim randomNumber As Integer = randomNumberGenerator.Next(words.Count)
15 'Dim chosenWord As String = words(randomNumber)
16 'Return chosenWord
17
18 'THE FOLLOWING USES OLEDBDATAADAPTER TO READ THE TEXT FILE AND RETURN RANDOM WORD
19
20 Dim ds As DataSet
21 Dim chosenWord As String = ""
22
23 If Not Page.IsPostBack Then
24 Dim objConn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & filePath & ";extended properties='text;hdr=yes;'")
25 Dim objAdapter As New OleDbDataAdapter("SELECT * FROM WORDS.txt", objConn)
26 ds = New DataSet()
27 objAdapter.Fill(ds)
28 Dim randomNumberGenerator As New Random()
29 Dim randomNumber As Integer = randomNumberGenerator.Next(ds.Tables(0).Rows.Count)
30 chosenWord = ds.Tables(0).Rows(randomNumber).Item(0).ToString()
31
32 End If
33
34 Return chosenWord
35
36
37
38 End Function
Instead of using a TextReader, I opted for an OleDBDataAdapter to perform the task of connecting to the text file. A random value is then extracted from the dataset; random row is selected and the value in the first column is set as the Hangman word.
The remainder of the code is similar.
0 comments:
Post a Comment