Author Topic: Methinks it is like a weasel  (Read 5742 times)

Offline asterixx

  • Bot Neophyte
  • *
  • Posts: 48
    • View Profile
Methinks it is like a weasel
« 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, and I figured there might be someone here who would know how to write one. Any ideas on this?
"Tell a man there are 300 billion stars in the universe and he'll believe you.  Tell him a bench has wet paint on it and he'll have to touch it to be sure."

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Methinks it is like a weasel
« Reply #1 on: April 25, 2008, 05:55:08 PM »
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.

Offline asterixx

  • Bot Neophyte
  • *
  • Posts: 48
    • View Profile
Methinks it is like a weasel
« Reply #2 on: April 25, 2008, 07:21:15 PM »
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?
"Tell a man there are 300 billion stars in the universe and he'll believe you.  Tell him a bench has wet paint on it and he'll have to touch it to be sure."

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Methinks it is like a weasel
« Reply #3 on: April 25, 2008, 07:24:13 PM »
If you just used a char for each letter, you could have the sentence be any ASCII character, including punctuation.
« Last Edit: April 25, 2008, 07:24:45 PM by Numsgil »

Offline asterixx

  • Bot Neophyte
  • *
  • Posts: 48
    • View Profile
Methinks it is like a weasel
« Reply #4 on: April 25, 2008, 07:31:04 PM »
I'm going to experiment a bit. I'll get back to you on this.  

"Tell a man there are 300 billion stars in the universe and he'll believe you.  Tell him a bench has wet paint on it and he'll have to touch it to be sure."

Offline asterixx

  • Bot Neophyte
  • *
  • Posts: 48
    • View Profile
Methinks it is like a weasel
« Reply #5 on: April 26, 2008, 09:40:04 AM »
It would seem that the most common form of these programs are java applets, and there was one particularly good approach here 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?

"Tell a man there are 300 billion stars in the universe and he'll believe you.  Tell him a bench has wet paint on it and he'll have to touch it to be sure."

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Methinks it is like a weasel
« Reply #6 on: April 26, 2008, 02:40:32 PM »
The most difficult part is the string manipulation.  How familiar are you with string manipulation in VB or C++?

Offline asterixx

  • Bot Neophyte
  • *
  • Posts: 48
    • View Profile
Methinks it is like a weasel
« Reply #7 on: April 26, 2008, 10:39:10 PM »
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.
 
« Last Edit: April 26, 2008, 10:39:47 PM by asterixx »
"Tell a man there are 300 billion stars in the universe and he'll believe you.  Tell him a bench has wet paint on it and he'll have to touch it to be sure."

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Methinks it is like a weasel
« Reply #8 on: April 28, 2008, 03:19:02 AM »
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.

Offline Gambit

  • Bot Builder
  • **
  • Posts: 52
    • View Profile
Methinks it is like a weasel
« Reply #9 on: April 28, 2008, 07:25:22 AM »
Quote from: Numsgil
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.

Offline asterixx

  • Bot Neophyte
  • *
  • Posts: 48
    • View Profile
Methinks it is like a weasel
« Reply #10 on: April 28, 2008, 10:51:32 PM »
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?

Quote
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.
« Last Edit: April 28, 2008, 11:40:23 PM by asterixx »
"Tell a man there are 300 billion stars in the universe and he'll believe you.  Tell him a bench has wet paint on it and he'll have to touch it to be sure."

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Methinks it is like a weasel
« Reply #11 on: April 29, 2008, 01:53:04 AM »
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.
« Last Edit: April 29, 2008, 01:53:30 AM by Numsgil »

Offline goffrie

  • Bot Builder
  • **
  • Posts: 65
    • View Profile
Methinks it is like a weasel
« Reply #12 on: April 29, 2008, 07:25:25 PM »
Code: [Select]
#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.

Offline asterixx

  • Bot Neophyte
  • *
  • Posts: 48
    • View Profile
Methinks it is like a weasel
« Reply #13 on: April 29, 2008, 08:17:15 PM »
[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.
"Tell a man there are 300 billion stars in the universe and he'll believe you.  Tell him a bench has wet paint on it and he'll have to touch it to be sure."