Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Nickname generator plz help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Lancelot




PostPosted: Tue Jul 19, 2005 2:38 pm   Post subject: Nickname generator plz help

I have letters.txt with following data:

bcdfghjklmnpqrstvwxyz
aeiou

I was thinking of making a nickname generator, so it will take first random letter from the first row, second random from the second row, third random from the first and so on...

So, I am trying to use seek, but all I get is a bunch of random numbers instead of letters. It should be simple, but I am really lost with getting letters from the file.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Tue Jul 19, 2005 2:47 pm   Post subject: (No subject)

Well, you don't have to use a file. It would be easier if you set up two constants, like so:
code:

const consonants := "bcdfghjklmnpqrstvwxyz"
const vowels := "aeiou"

Actually, it would even work better if it were an array. That way you could use a for loop to get your letters.
code:

const letter_group : array 1 .. 2 of string := init ("bcdfghjklmnpqrstvwxyz", "aeiou")


But, if you really want to use a file, know that every time you use get the "cursor" jumps to the next line. So you could get a letter from the first line (you'd have to read the line into a variable first, though), get a letter from the second line, then seek back to the start, like this:
code:

seek : fileNo, 0


If that doesn't help, post the code you've got so far. Smile
Delos




PostPosted: Tue Jul 19, 2005 3:55 pm   Post subject: (No subject)

As Cervantes as pointed out, you needn't use a file for this. But let's say that you wanted to use a file, and you found that the command 'seek' could be used for your purposes.
This is all well and good, and Cervantes' method would likely work. However, this is by no means the best way of doing this. Rather, you could use your file as simply a data depository. Your programme would open the file, extract its contents, and then manipulate those contents in the form of variables (or as Cervantes pointed out, elements of an array).

You should be familiar with opening and clos(e)ing files, as well as basic String Manipulation. Check the Tutorials if you need help with either of these topics.
Again, it is customary for one to post their code when asking a question. This helps us to see how much you know, and to accertain the nature of your problem as quickly as possible. When posting code, please use either [code] or [syntax] tags. If your code is particularly long, attach it instead.

Post your solution when you're done, I'd like to see how you tackle this.

Edit: Just realized your Join Date...woah! Shocked
MysticVegeta




PostPosted: Tue Jul 19, 2005 4:30 pm   Post subject: (No subject)

Here is a basic model i made of what you are talking about but i didnt put in the file writing/reading, you have to do that, cant entirely do it for you.

code:
var lines : array 1 .. 2 of string := init ("bcdfghjklmnpqrstvwxyz", "aeiou")

var counter := 1
var randLetter, finalWord : string := ""
loop
    exit when length (lines (1)) = 1 or length (lines (2)) = 1
    randLetter := lines (counter) (Rand.Int (1, length (lines (counter))))
    finalWord += randLetter
    lines (counter) := lines (counter) (1 .. index (lines (counter), randLetter) - 1) + lines (counter) (index (lines (counter), randLetter) + 1 .. *)
    counter += 1
    if counter = 3 then
        counter := 1
    end if
end loop

put finalWord
Cervantes




PostPosted: Tue Jul 19, 2005 11:10 pm   Post subject: (No subject)

Arrays work well with for loops, Mystic. [yoda voice]mMMmm, learn well, this you must[/yoda voice]
Also, I don't think that feature of 'preventing the same letter from appearing more than once in a name' should exist. Just look at "William". Two i's, two l's.
Turing:

const letter_group : array 1 .. 2 of string := init ("bcdfghjklmnpqrstvwxyz", "aeiou")
var name := ""

for rep : 1 .. Rand.Int (2, 4)
    for i : 1 .. 2
        name += letter_group (i) (Rand.Int (1, length (letter_group (i))))
    end for
end for
put name


I've got some pretty neat ones. "Nasaki" and "Rice" were my favourite. Laughing Mmm, I'm hungry...
Lancelot




PostPosted: Wed Jul 20, 2005 2:34 am   Post subject: (No subject)

Thank you, people! Smile

I don't know why I wanted to use a file... Perhaps to have ability to delete letters that I don't like. Smile

Here are some edits:
code:
var letters : array 1 .. 2 of string := init ("bcdfghjklmnpqrstvwxyz", "aeiou")
var randletter, word : string := ""
var amount, lngth, random, i : int := 0

put "How many words?"
get amount
put "How long? (input zero to randomize)"
get lngth
if lngth = 0 then
    random := 1
end if
cls
for rep : 1 .. amount
    if random = 1 then
        lngth := Rand.Int (2, 10)
    end if
    i := Rand.Int (1, 2)
    for count : 1 .. lngth
        if i = 1 then
            i := 2
        else
            i := 1
        end if
        randletter := letters (i) (Rand.Int (1, length (letters (i))))
        word += randletter
    end for
    delay (500)
    put word
    word := ""
end for


Now it can display words with odd amount of letters, but the part that does it looks really ugly. Is there a better way to do it?

And, does anyone has any ideas on how to make first letter capital?
Cervantes




PostPosted: Wed Jul 20, 2005 6:07 am   Post subject: (No subject)

Lancelot wrote:
And, does anyone has any ideas on how to make first letter capital?


You can download my Improved String Module, then use the Capitalize method, like this:
code:

put Str.Capitalize ("helLO WoRLd")

or maybe like this:
code:

var name : string
get name : *
Str.Make_Capitalized (name)
put name


If you look at the code inside the module, you'll get an idea of how to do it. Basically, you work with ASCII values. If the first letter is lower case, subtract 32 to it's ASCII value. If any other letters are upper case, add 32 to their ASCII values.
MysticVegeta




PostPosted: Wed Jul 20, 2005 8:22 am   Post subject: (No subject)

lol yoda voice. lol i forgot about "for" loops Embarassed

Anyways, Isnt there a "Str.Upper" command in turing 4.05?

code:
put Str.Upper("h E l l O WoRlD")
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Wed Jul 20, 2005 3:38 pm   Post subject: (No subject)

Yes, Str.Upper would work as well. Mind you, you then have to go to the trouble of this:
code:

name := Str.Upper (name  (1)) + name (2 .. *)

I hate having to work with strings like that. It sure would be nice if Turing would let us treat the string as an array of chars (and therefore allow us to change any element (or letter, in this case) of the array (or name, in this case)).

Also, doing that would leave all the other letters as whatever case they were, which we probably don't want. To fix that, we'd have to do this:
code:

name := Str.Upper (name (1)) + Str.Lower (name (2 .. *))

That's essentially what the Capitalize method does.
wtd wrote:

Don't reinvent the wheel if there's an existing library function to do it for you

Very Happy
Delos




PostPosted: Wed Jul 20, 2005 6:44 pm   Post subject: (No subject)

Cervantes wrote:

wtd wrote:

Don't reinvent the wheel if there's an existing library function to do it for you



I once did that unknowlingly with strint(). It was back in grade 10, and I was error trapping a programme of mine, so I figured out that if I were to make sure int-input was indeed int, I'd have to accept it as string, type-cast it and check it.
Wow...such a long time ago.
Lancelot




PostPosted: Wed Jul 20, 2005 9:32 pm   Post subject: (No subject)

Ok, thanks everyone, no more questions.

Final version with file output feature.

code:

var letters : array 1 .. 2 of string := init ("bcdfghjklmnpqrstvwxyz", "aeiou")
var randletter, word : string := ""
var amount, lngth, random, i, file : int := 0

put "How many words?"
get amount
put "How long? (input zero to randomize)"
get lngth
if lngth = 0 then
    random := 1
end if
cls
for rep : 1 .. amount
    if random = 1 then
        lngth := Rand.Int (2, 10)
    end if
    i := Rand.Int (1, 2)
    for count : 1 .. lngth
        if i = 1 then
            i := 2
        else
            i := 1
        end if
        randletter := letters (i) (Rand.Int (1, length (letters (i))))
        word += randletter
    end for
    delay (500)
    word := Str.Upper (word (1)) + Str.Lower (word (2 .. *))
    put word
    open : file, "list.txt", put, mod, seek
    seek : file, *
    put : file, word 
    close : file
    word := ""
end for
Delos




PostPosted: Wed Jul 20, 2005 10:36 pm   Post subject: (No subject)

Not too bad. I particularly like the way you use the seek line to go to the end of the file.
Now, here's some notes:
- you don't need that delay, it's just annoying.
- now that you've gotten the basic vowel-consanant-vowel thing going on, how about looking into binary trees? Basically, you select a node (let's say the letter 'r') then decide whether to add a vowel or a consonant. If you choose consonant, you bring up a list of possible consonants that can go after 'r', for example "b, d, k, t". You choose from within this. This becomes your next node, and you make the choice again.
Letters like 'q' will only have one vowel choice, and letters like 'x' might be a little finicky to have a letter after them, so you will need to also designate an 'end of word' character.
Sound like fun? I might just try it myself.

[edit]

Ok, I just finished it. It's really primitive. It would work much better if it ran based on syllabals instead of just letters. However, it works somewhat well. I've almost gotten some understandable words out of it!
Note that the 'wordLen' thing doesn't quite work...Thinking

Turing:

type node :
    record
        vowels : string
        cons : string
    end record

var tree : flexible array 0 .. 0 of node
const wordLen : int := 6
const numWords : int := 20

function handleInput (inStr : string) : node
    var outN : node
    assert index (inStr, "%") > 0
    outN.vowels := Str.Upper (inStr (1 .. index (inStr, "%") - 1))
    outN.cons := Str.Upper (inStr (index (inStr, "%") + 1 .. *))
    result outN
end handleInput
var file : int

setscreen ("text")
open : file, "wordgensource.txt", get
for i : 1 .. 26
    exit when eof (file)
    var temp : string
    new tree, upper (tree) + 1
    get : file, temp : *
    tree (upper (tree)) := handleInput (temp)
end for
close : file

var word : string := ""

for k : 1 .. numWords
    word := chr (Rand.Int (ord ('A'), ord ('Z')))
    for i : 1 .. Rand.Int (1, wordLen)
        if Rand.Int (0, 1) = 0 then
            % vowels
            word += tree (ord (word (*)) - 64).vowels (Rand.Int (1, length (tree (ord (word (*)) - 64).vowels)))
        else
            word += tree (ord (word (*)) - 64).cons (Rand.Int (1, length (tree (ord (word (*)) - 64).cons)))
        end if
    end for
    put word
end for


Here's the source file:

[Edit 2]
MOIST, SAD, PLOP, QUA, HYMN Yeah! Real words! w00t.



wordgensource.txt
 Description:

Download
 Filename:  wordgensource.txt
 Filesize:  442 Bytes
 Downloaded:  173 Time(s)

Lancelot




PostPosted: Thu Jul 21, 2005 12:25 am   Post subject: (No subject)

Wow... Impressive. A bit too hard for me yet, but i'll try to understand.

Delay thingy - just so you could read the words as they appear, cause if you want like hundred of words then it goes over the screen.
MysticVegeta




PostPosted: Thu Jul 21, 2005 7:34 am   Post subject: (No subject)

well take your time and remember the Turing Walkthrough is your best friend. I learned records only a couple of days ago through the walkthrough. Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: