Free Games Forum
Free Games Games Forums Music Forums TV Forums

  Free Games Forum Home FORUM
HOME
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN
Rules & FAQ RULES / FAQ
REPORT SPAM

Free Games Forum: Game Technology: Visual Basic / VB:
Generating random words or sentences.

 

 


Athlon XP
Veteran


Feb 2, 2007, 1:06 PM

Post #1 of 13 (409 views)
Shortcut
Generating random words or sentences. Can't Post

Generating a random integer in VB2005 is pretty easy, I just input these three lines of code into a subroutine:

Randomize()
Dim value As Integer = CInt(Int((6 * Rnd()) + 1))
Me.lblRandom.Text = value

How would be a simple and easily remembered way of doing this for words/sentences? A friend of mine did this for a school project once, but I never got the source code and now it's gone. The Interwebs ain't helpin'.


Lord I was born a shamblin' man
Archives (Newest Addition: The Judgment of Tate's Father)



Peach Pit
Veteran / Moderator


Feb 3, 2007, 3:17 PM

Post #2 of 13 (404 views)
Shortcut
Re: [Athlon XP] Generating random words or sentences. [In reply to] Can't Post

Crap. We just started to do Visual Basic in Computer sciences, but I can do it in Ruby...

Do the words have to make sense? Like, actual words? You could jsut assign each letter in the alphabet a number, and then randomize for an integer, and whatever pops up is your first letter. Then re-randomize for the second for the 2nd letter etc. You could even randomize for the number of letters to randomize for.


bye.


Athlon XP
Veteran


Feb 4, 2007, 12:45 AM

Post #3 of 13 (403 views)
Shortcut
Re: [Peach Pit] Generating random words or sentences. [In reply to] Can't Post

Oh, nevermind. I figured out how to do it whilst staring at my bedroom ceiling yesterday.

I believe it went something like this:

Const intRand1 As Integer = 1
Const intRand2 As Integer = 2
Const intRand3 As Integer = 3
Dim intRandom As Integer

^ Goes in the beginning of the code. Then these two lines go wherever you want them to:

Randomize()
intRandom = Int(3 * Rnd()) + 1

The subroutine I used this random sentences thing was a maze of If statements, which you probably don't want to see, but the actual randomization went like this:

If intRandom = 1 Then
Me.Label1.Text = "Random1"
ElseIf intRandom = 2 Then
Me.Label1.Text = "Random2"
ElseIf intRandom = 3 Then
Me.Label1.Text = "Random3"
End If

And that worked. If you want to see all my code for the project, it's here.


Lord I was born a shamblin' man
Archives (Newest Addition: The Judgment of Tate's Father)


TazG
Veteran / Moderator


Feb 6, 2007, 4:54 AM

Post #4 of 13 (388 views)
Shortcut
Re: [Athlon XP] Generating random words or sentences. [In reply to] Can't Post

*stumbles into forum*

*downloads code*

*opens Form1.vb in mspaint*

*stares at it*


Holy crap.

Man, learn to use arrays (for both objects and variables). Look it up, it's easy.

You end up with numbered objects like Label(1), Label(2) instead of numbers hardcoded into their names.

The good thing about that is you can replace those numbers with variables, and cut down on the size of your code immensely by turning those repetitive parts into loops.

Like, first I'd go like this:

Code
Const total=5

(total being the total number of label-thingies you're using)

Then you can change this crap

Code
Label1.Top = 20 
Label2.Top = 20
Label3.Top = 20
Label4.Top = 20
Label5.Top = 20

to this:

Code
For tazg=1 to total 
Label(tazg).Top = 20
Next


Likewise, I'd condense that monstrous timer sub like this (new stuff in red):

Code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
danger=false
for rofl=1 to total
Label(rofl).Top = Label(rofl).Top + 10
If Label(rofl).Top > 160 Then
'Lose ten points
Label(rofl).Top = 20
Score = Score - 10
'Display the updated score
lblScore.Text = "SCORE: " & Score
End If
next
for lmao=1 to total
If Label(lmao).Top > 120 Then
'Display a danger message when a label starts to get too low
lblTitle.Text = "Danger!"
danger=true
end if
next
if danger=false then
'Change the title back to normal when not in danger
lblTitle.Text = "Keep those boxes up there"
end if
If Score = -50 Then
'End and reset the game if you have -50 points
lblTitle.Text = "game Over!"
btnStart.Text = "Start"
Timer1.Enabled = False
For tazg=1 to total
Label(tazg).Top = 20
Next
End If
End Sub


For the next part, I'd start by taking out your Const lines in the beginning and replacing it with this beauty:

Code
Const wordz=3 

Dim randomword(1 to total, 1 to wordz) As String

randomword(1,1)="broom"
randomword(1,2)="system"
randomword(1,3)="machine"

randomword(2,1)="message"
randomword(2,2)="cutter"
randomword(2,3)="property"

randomword(3,1)="inside"
randomword(3,2)="emulate"
randomword(3,3)="heatsink"

randomword(4,1)="plate"
randomword(4,2)="sausage"
randomword(4,3)="cookie"

randomword(5,1)="shoes"
randomword(5,2)="hands"
randomword(5,3)="fingers"

See, randomword is a wonderful 2-dimensional array now. It has 5 variables (total=5), representing the 5 boxes, and each of those contains 3 possible words (wordz=3). It's simply awe-inspiring.

Then you can shorten the last part with this marvelous loop:

Code
Private Sub txtInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged 
'Type the words exactly as they appear to send them back up
for lol=1 to total
If txtInput.Text <> "" Then
Randomize()
intRandom = Int(wordz * Rnd()) + 1
If txtInput.Text = Label(lol).Text Then
Me.Label(lol).Text = randomword(lol, intRandom)
txtInput.Text = ""
Label(lol).Top = 20
Score = Score + 1
lblScore.Text = "SCORE: " & Score
End If
next
End Sub


So try that. I can't test it but I'm pretty sure it works fine. And it shortens your code by like 70% or something. Plus, if you want to add more boxes or words, all you have to do is add to the array and edit the "total" and "wordz" constants accordingly. In fact I would get into the habit of making every reused number a constant at the beginning of the code, so you can make widespread changes easier (such as "Const startingpoint=20" for resetting the boxes).


MY BLOG,

(This post was edited by TazG on Feb 6, 2007, 4:57 AM)


TazG
Veteran / Moderator


Feb 6, 2007, 5:05 AM

Post #5 of 13 (385 views)
Shortcut
Re: [Athlon XP] Generating random words or sentences. [In reply to] Can't Post

Just noticed you need to change

Code
If Score = -50

to

Code
If Score < -51

Score=-50 requires you to have exactly -50 points to lose, so if you have -45 points and lose 10, you have -55 and get to keep playing.

And the textbox should be disabled along with the timer.


MY BLOG,

(This post was edited by TazG on Feb 6, 2007, 5:08 AM)


Athlon XP
Veteran


Feb 6, 2007, 11:34 AM

Post #6 of 13 (374 views)
Shortcut
Re: [TazG] Generating random words or sentences. [In reply to] Can't Post

Yay! Input!

*prints*

EDIT: mod tools source pls


Lord I was born a shamblin' man
Archives (Newest Addition: The Judgment of Tate's Father)


(This post was edited by Athlon XP on Feb 6, 2007, 11:36 AM)


jabman
Senior Member


Feb 6, 2007, 6:04 PM

Post #7 of 13 (364 views)
Shortcut
Re: [TazG] Generating random words or sentences. [In reply to] Can't Post

why use code when you can use monkeys?

http://en.wikipedia.org/wiki/Infinite_monkeys


God-
Leader of peace and prosperity.
Bringer of war and violence.


Athlon XP
Veteran


Feb 7, 2007, 2:19 AM

Post #8 of 13 (359 views)
Shortcut
Re: [TazG] Generating random words or sentences. [In reply to] Can't Post


In Reply To

Code
Const total=5

(total being the total number of label-thingies you're using)


That won't work in the place of my current constants; it wants it in a subroutine. What do I use in place of "total"? It whines about an unused local variable, but so do any other names for it I try.


Lord I was born a shamblin' man
Archives (Newest Addition: The Judgment of Tate's Father)


TazG
Veteran / Moderator


Feb 7, 2007, 2:32 AM

Post #9 of 13 (357 views)
Shortcut
Re: [Athlon XP] Generating random words or sentences. [In reply to] Can't Post

I think all that means is that you set the variable but haven't used it. And there's this annoying "feature" that shows you errors like that every time you finish typing a single line. I think you can turn it off.

Anyways, do you have "total" in any other parts of the code yet? Once you start using it, it will stop whining I think.


MY BLOG,


Athlon XP
Veteran


Feb 7, 2007, 3:47 AM

Post #10 of 13 (354 views)
Shortcut
Re: [TazG] Generating random words or sentences. [In reply to] Can't Post

Now hold on a sec.

Const Total = 5

For tazg = 1 To Total
Label(TazG).Top = 20

What's "tazg" there for, and what do I replace it with? Same thing for those others you threw in in red.


Lord I was born a shamblin' man
Archives (Newest Addition: The Judgment of Tate's Father)


TazG
Veteran / Moderator


Feb 7, 2007, 4:01 AM

Post #11 of 13 (351 views)
Shortcut
Re: [Athlon XP] Generating random words or sentences. [In reply to] Can't Post

So I take it you never used For and Next before. Those are just temporary variables to be used by the loop. It starts at 1 and goes to 5. (for tazg = 1 to total)

So like, on the first loop, it looks like this: Label(1).Top=20. Second time through, it's Label(2).Top=20. Simple. The tazg variable is just holding that number for a reference. It doesn't matter what you call it. You could even use the same variable in each loop because they reset at the beginning of the loop. I guess it'd make sense to call it "increment" or something, but I just named mine tazgroflolmao because I was lazy.

So, to make it clear... that For-Next bit is the eqivalent of this:

tazg = 1
Do Until tazg = 5
Label(tazg).Top = 20
tazg = tazg + 1
Loop

You've used loops before haven't you?


MY BLOG,


Athlon XP
Veteran


Feb 8, 2007, 8:18 AM

Post #12 of 13 (342 views)
Shortcut
Re: [TazG] Generating random words or sentences. [In reply to] Can't Post

Actually, no. I think that the code you put in that massive post above was for VB6, because, while it gave a host of build errors in 2005, I slammed 'em in my VB6 code editor and didn't get any blue/red squigglies. I did do this:

Dim randLabel As Integer
randLabel = 1
Do Until randLabel = 9
Label(randLabel).Top = 20
randLabel = randLabel + 1
Loop

But where "Label" is I get a build error saying that Label is a type and cannot be used an expression. But the code appears to make perfect sense nonetheless. The "unused local variable" error did go away though.


Lord I was born a shamblin' man
Archives (Newest Addition: The Judgment of Tate's Father)


TazG
Veteran / Moderator


Feb 8, 2007, 8:25 AM

Post #13 of 13 (340 views)
Shortcut
Re: [Athlon XP] Generating random words or sentences. [In reply to] Can't Post

Well I guess you can't call it Label then. Call it lbl or something. Did you actually make the object array?


MY BLOG,

 
 
 


Search for (options) Web Design by Web Ideas - Page loaded in: 0.18 s on (CGI/1.1)