PDA

View Full Version : Word Muddling in AS?



ben_is_sparky
April 1st, 2005, 03:26 AM
I thought it would be cool to try and make a translator/word muddler based on this pic:

The user types some text into a textbox and it gets "translated" into another.

I have no idea where to start. Maybe I need to use an array and put each letter in and then return them tin a different order? (I have never used an array before)

Is this too tricky for my limited AS knowledge?

Thanks

Shimi
April 1st, 2005, 05:56 AM
First of all you get a String with the sentence
next you split each word by the spaces
now all you need to do is go over the words array
and play with the middle letters placing them randomly

here's an example of scrambling the middle letters:

var word:String = "Apple";
var tmpArray:Array = new Array();
var tmpArray2:Array = new Array();
var tmpLetter:String;
var rndI:Number;
var i:Number;
//Split the word
for (i = 1; i < word.length-1; i++) { // Run only on the middle words
tmpArray[i] = word.substr(i, 1);
tmpArray2[i] = "";
}
//Set first and last letters
tmpArray[0] = word.substr(0, 1);
tmpArray2[0] = word.substr(0, 1);
tmpArray[word.length-1] = word.substr(word.length-1, 1);
tmpArray2[word.length-1] = word.substr(word.length-1, 1);
//Loop and scramble
for (i = 1; i < word.length-1; i++) {
rndI = getRandomEmpty(tmpArray2);
tmpArray2[rndI] = tmpArray[i];
}
//Done (tmpArray2 is the scrabled array)
trace("Before\t -> " + tmpArray.join(""));
trace("After\t -> " + tmpArray2.join(""));
//Function which gets an empty random cell in the new array.
function getRandomEmpty(arr:Array) : Number {
var i:Number;
i = Math.round(Math.random()*(arr.length-2))+1;
if (arr[i] == "") {
return i;
} else {
return getRandomEmpty(arr);
}
}

virusescu
April 1st, 2005, 06:25 AM
shimi was faster :hurt:


String.prototype.shuffleLetters = function() {
var arr:Array = this.split("");
for (i=0; i<arr.length; i++) {
var tmp = arr[i];
var randomNum = random(arr.length);
arr[i] = arr[randomNum];
arr[randomNum] = tmp;
}
return arr.join("");
};
// Word Muddling
function shuffleWord(word:String):String {
word_length = word.length;
while (word.charAt(word_length-1) == "." || word.charAt(word_length-1) == "?" || word.charAt(word_length-1) == ",") {
word_length--;
}
trace("for "+word+" the word length is "+word_length);
if (word_length<=2) {
return word;
} else {
firstLetter = word.charAt(0);
lastLetter = word.substr(word_length-1);
midString = word.substr(1, word_length-2);
return firstLetter+midString.shuffleLetters()+lastLetter;
}
}
String.prototype.muddled = function() {
//create array for each word.
var eachWord = new Array();
eachWord = this.split(" ");
for (x in eachWord) {
eachWord[x] = shuffleWord(eachWord[x]);
}
return eachWord.join(" ");
};
//------------------
mytext = "Well, here we go. I had a try on it too. Shimi was faster though. Anyway, see if this helps you. Longer words work better.";
mytext = mytext.muddled();
trace(mytext);

ben_is_sparky
April 1st, 2005, 06:43 AM
Thanks for the replies.

Could either of you explain your code a little more? I think I get what it does, just the code makes little or no sense.

virusescu - yours returns theese errors:


Scene=Scene 1, Layer=Layer 1, Frame=1: Line 12: '{' expected
function shuffleWord(word:String):String{

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 26: Unexpected '}' encountered
}
if I take out the :String then there are no more errors, but it just repeats "for the word length is" over and over. :puzzle:

Shimi - yours gives me these errors:

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 26: '{' expected
function getRandomEmpty(arr:Array):Number{

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 34: Unexpected '}' encountered
}
Again, if I take the :Number out, it has no errors but just traces Beforet --> and Aftert -->

Sorry, I really am a n00b at this... :D

claudio
April 1st, 2005, 07:13 AM
Either you're using MX or exporting your movie in AS 1.0
Try using this code instead.
String.prototype.shuffleLetters = function() {
var arr = this.split("");
for (i=0; i<arr.length; i++) {
var tmp = arr[i];
var randomNum = random(arr.length);
arr[i] = arr[randomNum];
arr[randomNum] = tmp;
}
return arr.join("");
};
// Word Muddling
function shuffleWord(word) {
word_length = word.length;
while (word.charAt(word_length-1) == "." || word.charAt(word_length-1) == "?" || word.charAt(word_length-1) == ",") {
word_length--;
}
trace("for "+word+" the word length is "+word_length);
if (word_length<=2) {
return word;
} else {
firstLetter = word.charAt(0);
lastLetter = word.substr(word_length-1);
midString = word.substr(1, word_length-2);
return firstLetter+midString.shuffleLetters()+lastLetter;
}
}
String.prototype.muddled = function() {
//create array for each word.
var eachWord = new Array();
eachWord = this.split(" ");
for (x in eachWord) {
eachWord[x] = shuffleWord(eachWord[x]);
}
return eachWord.join(" ");
};
//------------------
mytext = "Well, here we go. I had a try on it too. Shimi was faster though. Anyway, see if this helps you. Longer words work better.";
mytext = mytext.muddled();
trace(mytext);

ben_is_sparky
April 1st, 2005, 07:18 AM
YaY! :D That works. Thanks

Yeah, I'm using MX, I wondered if it was that.

BTW, what changes did you make to the code to make it MX compatible?

claudio
April 1st, 2005, 07:43 AM
MX is not compatible with strict data typing.
ie:
var myvar:Data Type

Check this for more info:
http://www.kirupa.com/developer/oop2/AS2OOPChangedAndAdded3.htm

ben_is_sparky
April 1st, 2005, 08:10 AM
Aahh I see Thanks y'all! :D

claudio
April 1st, 2005, 08:14 AM
Anytime ;)

ben_is_sparky
April 1st, 2005, 09:09 AM
I was playing, and made this:
www.benspencer.info/translator.html

It works fine, until you put a new line in. It doesn't count the enter's as new words and so doesn't start a new word until you have another space. So, if you have this in the input box:

This
is
simply
a
test

All the words will be counted as one and muddled as one.

How could I fix this?

Shimi
April 1st, 2005, 10:19 AM
If im not wrong u need to seperate by \n too..
but im not 100% sure..
because i think it seperates by spaces and between the end of the line and the start
of the next one there is no spaces only \n thats why it count it as a one word and mixes the \n

ben_is_sparky
April 1st, 2005, 11:09 AM
Yeah, that's what I thought. I'm starting to understand the code a bit more, but I can't find where in the code it seperates the words. :puzzle:

Shimi
April 1st, 2005, 11:11 AM
which code did you use? :S

ben_is_sparky
April 1st, 2005, 11:13 AM
The third one, which claudio posted - It was the only one that worked in MX.

Shimi
April 1st, 2005, 11:46 AM
Ok found the fix!
just did another split by \n and muddeled each row by it self
and joined at the end and walla!


String.prototype.muddled = function() {
//create array for each word.
var returnString = "";
var eachWord = new Array();
var eachLine = new Array();
eachLine = this.split("\\n");
for (i in eachLine) {
eachWord = eachLine[i].split(" ");
for (x in eachWord) {
eachWord[x] = shuffleWord(eachWord[x]);
}
returnString = eachWord.join(" ") + "\\n" + returnString;
}
return returnString;
};

ben_is_sparky
April 1st, 2005, 01:20 PM
I'm not sure I'm putting it in right. If I use your code to replace the function at the top of claudio's code, is that right?

If I do that, then it doesn't seem to put the enters back in.

I tried adding in a + "/n" in a couple of places where there was (" ") at the end to make it put them back in, but I don't really yunderstand the code enough so I'm just stabbing in the dark. :(

Shimi
April 1st, 2005, 01:41 PM
just replace the exact function from claudio code to the updated function i posted
i took his function and updated it thats all
(String.prototype.muddled)

ben_is_sparky
April 1st, 2005, 01:54 PM
It doesn't seem to work.

If I enter this text:


here is some sample text.

This is another line of sample text

and here is another

I get this out:


he is se se ts is ar le of se td he is ar

And this is my trace:


for another the word length is 7
for is the word length is 2
for here the word length is 4
for text

and the word length is 9
for sample the word length is 6
for of the word length is 2
for line the word length is 4
for another the word length is 7
for is the word length is 2
for text.

This the word length is 11
for sample the word length is 6
for some the word length is 4
for is the word length is 2
for here the word length is 4

I can see from the trace that it is treating the new lines as a part of the previous word. for example, it has

for text

and the word length is 9

that is being treated as one word. text = 4 letters, and = 3 letters and then 2 new lines.

Even so, it doesn't explain the output. :puzzle:

I've put a new vrsion (with your new function) up here if yo ant to have a look:

www.benspencer.info/translator2.html

Shimi
April 1st, 2005, 02:00 PM
can you give me the fla file?
ill check it out cuz it work for me fine

ben_is_sparky
April 1st, 2005, 02:02 PM
I'm using MX and AS 1 (I think)

Shimi
April 1st, 2005, 02:27 PM
OK i found the problem..
when i used the function i sent the string "This is a\nTest"
pushing the \n by my self
the problem is that when you press enter on the input box it doesnt push \n,
instead, its adding the char number 13 (in ascii) so instead of splitting by \n i splitted by the char 13
like so: String.fromCharCode(13)
btw its the only static function that the String class have lol

and another thing.. your code was alittle messy.. the Prototypes were on the button itself and the release event fired the function instead of fireing the call to it
so i fixed it up alittle, heres the fixed file (saved as mx fla)

ben_is_sparky
April 1st, 2005, 02:30 PM
Cool - thanks. I'll have a look.

About the mess - I was wondering if it wouldn't be better to not have all my code on the button. it seems awfully odd. Guess I just need more practice. :D

Edit: Yes. That works perfectly! Thanks for your help.

I'll have a look at the code and see if I can understand it :D :D