5 Replies - 1390 Views - Last Post: 01 November 2012 - 03:40 AM Rate Topic: -----

#1 eirik226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-November 12

Hangman, word from txt file question

Posted 01 November 2012 - 02:11 AM

Hello,

I am creating a Hangman program in Visual Basic, I am very new to programming overall, and this is the first language I am trying out. I am currently in the process of making a hangman game. The game itself is finished and working fine. But I am not 100% happy about how it's working and I want to add some more "stuff".

Right now the words that the programs uses is in a hidden listbox in the program, the randomize and everything is working fine. But I want the program to pick a random word from a textfile instead, and then make the user guess it.

As stated I have basically no idea what I am doing, and I have no idea how to reach my goal. Since I am new all I have done is googled it, but didn't find any answer that I understood so I could implant it to my code. As long as I get it to work I am happy, and I do not ask anyone to write me the code, but just explain to me how I could do it or guide my in the right direction. At the moment I have no code and no idea how to make the program choose a random word from a textfile, thats why I am asking for help.


On a side note (Not so important): I can also choose numbers in my program, how do I remove that? Because I only have words and no numbers in hangman, so I want it to give a msgbox or something that tells the user that only letters are allowed if he tries to use numbers.

Thanks in advance for the help :)

Is This A Good Question/Topic? 0
  • +

Replies To: Hangman, word from txt file question

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Hangman, word from txt file question

Posted 01 November 2012 - 02:35 AM

I'll assume that player enters letter/word in a TextBox, and then clicks "Submit" button (better then reacting to TextBox.TextChanged event, because he/she could accidentally hit wrong key).

Letters only: on button Submit click check if text from textbox is numeric: IsNumeric("string from TextBox.Text"). I'd suggest checking for one character entered also (perhaps with textbox.text > 1 or something similar).

Reading from text file:
            Imports System.IO 'you need to import IO namespace at the top of the project

            Dim fStream As New System.IO.FileStream(pathToFile, IO.FileMode.Open)
            Dim sReader As New System.IO.StreamReader(fStream)
            Dim listOfMyWords As New List(Of String)

            Do While sReader.Peek >= 0
                listOfMyWords.Add(sReader.ReadLine) 'here you direct stream reader to read a line, and you add it to your list of words
            Loop
            
            fStream.Close()
            sReader.Close()



That way you'll know how many items are in your listOfMyWords, and will be able to pick random one of them. You also won't need to read from file, to get each word.
Was This Post Helpful? 1
  • +
  • -

#3 eirik226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-November 12

Re: Hangman, word from txt file question

Posted 01 November 2012 - 02:49 AM

Thank you for the quick answer, I'll try to implant this to my code and see if I get it to work!

Right now I do not use a textbox. When the user press a key on the keyboard it's registered as either right or wrong guess. But I am thinking of adding a box so to speak with all the letters of the keyboard as buttons, and the user press it with their mouse. But again, not sure how to do it, but haven't Googled it or tried to find anything out, so guess I will come back to that later :)

Again, thanks for the help :) I'll notify you if I run into any big problems I can't solve on myself!
Was This Post Helpful? 0
  • +
  • -

#4 eirik226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-November 12

Re: Hangman, word from txt file question

Posted 01 November 2012 - 02:55 AM

Sorry for double post, but didn't find any Edit post button. Not sure if I am just totally useless when it comes to forums.

How do I make the program "use" the word selected from the textfile? In your last post I saw how to add it to the program and I get that. But not sure how to make the program use it, if you understand what I mean. (Not native speaking English, so not the best describer you could say)
Was This Post Helpful? 0
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Hangman, word from txt file question

Posted 01 November 2012 - 03:34 AM

Quote

Sorry for double post, but didn't find any Edit post button. Not sure if I am just totally useless when it comes to forums.

You'll get edit option available later, when you'll have more posts here. How to use it? If you'd have your listOfWords' available to entire class (by declaring it in scope of class), you can use it everywhere in it. For example:

Class Form1
    Dim listOfWords As New List(Of String) 'this list will be available to entire class

    Sub SomeMethod()
        Dim listOfAnotherWords As New List(Of String) 'this list will be available inside SomeMethod only (not visible to SomeOtherMethod below). Its scope is SomeMethod only.
        listOfAnotherWords.Add("something") 'now when you add "something" to listOfAnotherWords it will be available until you exit SomeMethod, then it's lost
        listOfWords.Add("something else") 'this remains available even when you exit SomeMethod, because listOfWords is declared outside this method, and its scope is class Form1
    End Sub

    Sub SomeOtherMethod()
        listOfWords.Add("another string") 'now listOfWords holds 2 strings. One added in SomeMethod (if it was executed already), and one added right now, in this line of code
        'let's display them
        Dim myString As String = String.Empty 'just create empty string
        For Each item In listOfWords 'here we iterate through entire list
            myString &= item & Environment.NewLine 'and add each "line" from list to myString
        Next
        MessageBox.Show(myString) 'finaly we display it

        'but!! you can't access listOfAnotherWords in this method, because it was declared inside SomeMethod, and it's available only there
        'uncomment the line below, and you'll get error
        'MessageBox.Show(listOfAnotherWords(0))
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'let's run those methods
        SomeMethod()
        SomeOtherMethod()
    End Sub
End Class




You said in your previous post, that you have random pick done already. Use it the same way, to pick one of the listOfWords items. Lists have .Count property, that will tell you how many items are in it.

Quote

(Not native speaking English, so not the best describer you could say)

Me neither, and a lot of users here aren't, so no problem, just do your best.
Was This Post Helpful? 1
  • +
  • -

#6 eirik226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-November 12

Re: Hangman, word from txt file question

Posted 01 November 2012 - 03:40 AM

Thank you very much for your time and help :) I will take a look at it later when I go home. I really appreciate it. Hope it works out well for me and the program!

And thanks for adding the 'comments to explain everything. That helped me out understanding it a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1