Darwinbots Forum
Welcome To Darwinbots => Newbie => Topic started by: asterixx on April 25, 2008, 05:42:37 PM
-
I've written several simple programs in C++ and VB, and I was wondering how I could go about writing a Weasel Program (http://en.wikipedia.org/wiki/Weasel_program), and I figured there might be someone here who would know how to write one. Any ideas on this?
-
It's probably pretty easy. Is this the sort of algorithm you're thinking?
1. Mark all characters in your string as non static.
2. Produce a random letter for each non static character in your string.
3. Examine the string against the goal, and for each letter that matches, mark it as static.
4. Repeat to 1 if there are any non-static characters.
-
Precisely what I was thinking. Once I could come up with a structure for something like that I would have no problem adapting it and making a pixel version or something. How would I include characters like ". , !" and so on?
-
If you just used a char for each letter, you could have the sentence be any ASCII character, including punctuation.
-
I'm going to experiment a bit. I'll get back to you on this.
-
It would seem that the most common form of these programs are java applets, and there was one particularly good approach here (http://www.cs.laurentian.ca/badams/evolution/EvolutionApplet102.html) that sparked my attention. Im not familiar with java, however. Any ideas on how I might go about putting this sort of a language into C++ or VB?
-
The most difficult part is the string manipulation. How familiar are you with string manipulation in VB or C++?
-
Well, I guess I'm quite familiar, although compared to many other people, including many people on this forum(which is why I'm asking here ) my programming experience is somewhat limited. So really, I don't know how familiar I am with string manipulation. I've written several programs that are capable of perhaps even more complicated code then a Weasel Program might require. What I'm really asking I guess, is what would the core mechanics of a weasel program be? The algorithm seems simple enough but turning it into a program is proving more difficult than I thought.
P.S. Id like to take this opportunity to extend my thanks to all of the people on this forum who have helped me with various things. When I first started experimenting with DB, and read this forum I had a sense that you would all be very helpful. but I didn't think you'd be willing to help a somewhat 'complete stranger' in the way you have. The kindness and decency here are going to make future versions of DB even more successful, Im certain.
-
Try to get started and plunk down some code. When you get stuck, post what your code is and I'll try to give you some help. Often times it's bets to just jump right in and start coding when you get stuck.
-
The most difficult part is the string manipulation. How familiar are you with string manipulation in VB or C++?
VB is much easier for string manipulation.
you can download for free Microsoft Visual Basic 2008 Express Edition
its a programming environment for VB. Takes a long time to download and install but well worth it.
-
Well, I succeeded at making a completely different program. Its a random number generator which I think has helped me get my brain around this. Is this at least a good first step in figuring it out?
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Numb As Integer = Rand(1, 1000)
Label1.Text = (Numb.ToString())
End Sub
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd()) + Low
End Function
End Class
That can pretty much be copy/pasted into VB, just make a Button1 and a Label1.
-
Yep, that's a good first step. The trick now is to realize that a string is stored in memory using chars, and that when you're using chars, something like 'a' is really just a number. So for the next step, try to build something that generates random letters that you're interested in (eg: A-Z, and maybe some punctuation marks). Bonus points if you can do it with less than half a dozen if/else cases.
-
#include <stdio.h>
#include <time.h>
#include <string.h>
char str[1000];
const char ref[1000] = "Methinks it is like a weasel";
int main() {
int I, len;
srand(time(NULL));
len = strlen(ref);
while (strcmp(str, ref) != 0) { /* if we're not done */
for (I = 0; I < len; ++I) { /* iterate through each character */
if (str[I] != ref[I]) /* only if it isn't correct already */
str[I] = rand() % 95 + 32; /* set to random character */
/* 32 = space, 95 printable characters */
/* !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQR
STUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ */
}
printf("%s\r", str); /* spit out the current string */
usleep(5000); /* sleep - without it, the program ends almost immediately */
}
printf("\n");
return 0;
}
Yay C but probably coded just as easily in any other language.
-
[div class=\'codetop\']CODE[div class=\'codemain\' style=\'height:200px;white-space:pre;overflow:auto\']Module Module1
Sub Main()
Dim Create As StringCreator
Dim NumberOfKeys As Integer
Dim i_Keys As Integer
Dim RandomCreate As String
NumberOfKeys = 1
Create = New StringCreator
Create.KeyLetters = "abcdefghijklmnopqrstuvwxyz"
Create.KeyNumbers = "0123456789"
Create.KeyChars = 10
For i_Keys = 1 To NumberOfKeys
RandomCreate = Create.Generate()
Console.WriteLine(RandomCreate)
Next
Console.WriteLine("Press any key to exit...")
Console.Read()
End Sub
End Module
////////////////////////////
Option Strict On
Imports System.Text
Public Class StringCreator
Dim Key_Letters As String
Dim Key_Numbers As String
Dim Key_Chars As Integer
Dim LettersArray As Char()
Dim NumbersArray As Char()
Property KeyLetters() As String
Set(ByVal Value As String)
Key_Letters = Value
End Set
End Property
Property KeyNumbers() As String
Set(ByVal Value As String)
Key_Letters = Value
End Set
End Property
Property Set KeyChars() As String
Set(ByVal Value As String)
Key_Letters = Value
End Set
End Property
Function Generate() As String
Dim i_key As Integer
Dim Random1 As Single
Dim arrIndex As Int16
Dim sb As New StringBuilder
Dim RandomLetter As String
LettersArray = Key_Letters.ToCharArray
NumbersArray = Key_Numbers.ToCharArray
For i_key = 1 To Key_Chars
Randomize()
Random1 = Rnd()
arrIndex = -1
If (CType(Random1 * 111, Integer)) Mod 2 = 0 Then
Do While arrIndex < 0
arrIndex = Convert.ToInt16(LettersArray.GetUpperBound(0) * Random1)
Loop
RandomLetter = LettersArray(arrIndex)
If (CType(arrIndex * Random1 * 99, Integer)) Mod 2 <> 0 Then
RandomLetter = LettersArray(arrIndex).ToString
RandomLetter = RandomLetter.ToUpper
End If
sb.Append(RandomLetter)
Else
Do While arrIndex < 0
arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random1)
Loop
sb.Append(NumbersArray(arrIndex))
End If
Next
Return sb.ToString
End Function
End Class
Ok, this one generates a random 10 character string. Its in VB as well.