-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvisual-basic-6.mod
89 lines (67 loc) · 2.36 KB
/
visual-basic-6.mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Option Explicit
Private Const MUTATE_CHANCE = 0.04
Private Const NUM_CHILDREN = 100
Private Const TARGET = "METHINKS IT IS LIKE A WEASEL"
Private Const CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
Public Sub WeaselProgram()
Randomize
'Declare variables
Dim current As String
Dim children(NUM_CHILDREN) As String
Dim tries As Long
Dim score As Integer
Dim best_score As Integer
Dim best_child As Integer
Dim i As Integer
Dim c As Integer
'initialize current generation to completely random string
current = ""
For i = 1 To Len(TARGET)
current = current & GetRandomChar()
Next
Debug.Print "0: " & current
' main loop
tries = 0
While current <> TARGET
'reproduce: clone current into children and mutate their characters
For c = LBound(children) To UBound(children)
children(c) = current
For i = 1 To Len(children(c))
If Rnd < MUTATE_CHANCE Then
children(c) = Left(children(c), i - 1) & GetRandomChar() & Mid(children(c), i + 1)
End If
Next
Next
'get the closes match and promote to current
best_score = -1
For c = LBound(children) To UBound(children)
score = CompareWeasels(children(c), TARGET)
If score > best_score Then
best_score = score
best_child = c
End If
Next
current = children(best_child)
'print current status
tries = tries + 1
Debug.Print tries & ": " & current
DoEvents
Wend
Debug.Print "Completed in " & tries & " generations!"
End Sub
Private Function GetRandomChar()
'Returns a random character from the allowed list characters
Dim c As Integer
c = Int((Len(CHARS) - 1 + 1) * Rnd + 1) 'Get a random number in an interval: Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
GetRandomChar = Mid(CHARS, c, 1)
End Function
Private Function CompareWeasels(s1 As String, s2 As String) As Integer
'Compares two strings and returns the number of matching characters
CompareWeasels = 0
Dim i As Integer
For i = 1 To Len(s1)
If Mid(s1, i, 1) = Mid(s2, i, 1) Then
CompareWeasels = CompareWeasels + 1
End If
Next
End Function