|
-
May 7th, 2013, 01:52 AM
#1
Thread Starter
Member
[RESOLVED] Random Boolean and Procedure Matching
After watching iRobots, and against my better judgment I decided to give my program a brain to play against me.
In a player's turn, the player can choose to execute 10 different actions (each its own procedure).
For each computer player, randomize True Or False 10 times, if True, call corresponding procedure, else generate next random. So it decides for itself to carry out that action or not.
ie
for x = 1 to 10
generate true or false
if true then
call whatever the first procedure is
call the 2nd procedure if x = 2
and so on to 10
Else
'do nothing
End If
Next
Any ideas?
Maybe I just random the strings True and False?
I would probably need to make some sort of a list to match the value of x to my procedure names.
-
May 7th, 2013, 02:11 AM
#2
Re: Random Boolean and Procedure Matching
Here's how I'd do it:
Code:
''' <summary>
''' Random number generator
''' </summary>
Private rng As New Random
''' <summary>
''' References to each of the action methods.
''' </summary>
Private methods As Action() = {New Action(AddressOf Method01),
New Action(AddressOf Method02),
New Action(AddressOf Method03),
New Action(AddressOf Method04),
New Action(AddressOf Method05),
New Action(AddressOf Method06),
New Action(AddressOf Method07),
New Action(AddressOf Method08),
New Action(AddressOf Method09),
New Action(AddressOf Method10)}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each method In methods
'Generate a random number and, if it's even, execute the next method.
If rng.Next() Mod 2 = 0 Then
method.Invoke()
End If
Next
End Sub
-
May 7th, 2013, 02:16 AM
#3
Hyperactive Member
Re: Random Boolean and Procedure Matching
jmcilhinney, you are not just a coder, you are an artist.
----------------------------------------------------
Missing the days of GWBasic 1.1
WaxyStudios.com
-
May 8th, 2013, 01:03 PM
#4
Thread Starter
Member
Re: Random Boolean and Procedure Matching
I wasn't sure this worked at first when I tested it to learn. On first click I drew 2,4,6,8 and then on the second click I drew 3,5,7,9. After further clicking it was more random and it does exactly what it is supposed to do.
Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|