00:00
00:00
Newgrounds Background Image Theme

Chan99 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Foss: Scrambling Strings

1,588 Views | 6 Replies
New Topic Respond to this Topic

Foss: Scrambling Strings 2005-10-27 12:55:09


i know it isnt much, but i wanted to try my hand at an FOSS, so check it out.
this is to create a word scramble, such as you would find in a puzzle game...
http://media.putfile../wordscrambleV11/640

-=- RAW CODE -=-

var correct:String = "This String Will Be Scrambled"
var broken:Array = new Array();
broken = correct.split(" ")
for (i=0; i<broken.length; i++){
broken[i] = broken[i].split("");
var scram:Array = [];
for (j=broken[i].length; j>0; j--){
var where:Number = int(Math.random()*broken[i].length);
scram.push(broken[i][where]);
broken[i].splice(where, 1);
}
broken[i] = scram
broken[i] = broken[i].join("");
}
var scramver:String = broken.join(" ");
trace(scramver);

-=- EXPLAINATION -=-

var correct:String = "This String Will Be Scrambled"

here is the string that we will start with

var broken:Array = new Array();
broken = correct.split(" ")

this is where the string is broken into seperate words

for (i=0; i<broken.length; i++){

this will run the following code seperately for each word

broken[i] = broken[i].split("");

this will be the array broken into a nested array of the letters, seperately

var scram:Array = [];

here is where we will put the scrambled version of each word

for (j=broken[i].length; j>0; j--){
var where:Number = int(Math.random()*broken[i].length);

selects a random letter out of the word

scram.push(broken[i][where]);

adds the letter to the "scrambled" array

broken[i].splice(where, 1);

remove the letter from the original word

}
broken[i] = scram
broken[i] = broken[i].join("");

replaces the word with the scrambled word, and then puts it back together.

}
var scramver:String = broken.join(" ");

puts all of the words back together into one massive string

trace(scramver);

thats the final product

umm, enjoy. i hope you like the open source, and id like to see if anyone can make this something new. im sure you could combine this with an array stored with where each letters original location was in order to make passwords or some sort of rudimentary encryption. i would imagine that this could have more usage than just a simple word scramble, but that was the original intention of the code.

heres the final product again:
http://media.putfile../wordscrambleV11/640


BBS Signature

Response to Foss: Scrambling Strings 2005-10-27 13:05:09


Haha, very nice!

It's not random, is it?


BBS Signature

Response to Foss: Scrambling Strings 2005-10-27 13:15:08


At 10/27/05 01:05 PM, -Toast- wrote: It's not random, is it?

the scrambling itself is. completely random. the string isnt. also, oftentimes, the short strings (2 or 3 letters) dont scramble because the likelihood of a 3 letter string coming out with the same as the original is 1/(3!) = 1/6, but the likelihood of a 6 letter string coming out with the same as the original is 1/(6!) = 1/720


BBS Signature

Response to Foss: Scrambling Strings 2005-10-27 15:30:23


my only bump ever.
now that lots of regulars are on, i just want to draw a bit of attention.
otherwise, im done with this. no prostitution of my thread.


BBS Signature

Response to Foss: Scrambling Strings 2005-10-27 20:10:21


pretty sweet stuff here

Response to Foss: Scrambling Strings 2005-10-27 20:29:22


ummm yeah
Pretty easy and useless but I geuss it has some applications


- Matt, Rustyarcade.com

Response to Foss: Scrambling Strings 2005-10-27 21:01:46


At 10/27/05 08:29 PM, Ninja-Chicken wrote: ummm yeah
Pretty easy and useless but I geuss it has some applications

well, i just came up with it in about 15 min when my professor in computer programming was trying to teach the class for loops. i got bored, and this is what i wrote in the front of my java textbook.

i think if you stored two arrays... one with one set of numbers, the other with another, and one array simply tells the other array what order to arrange them in, and then display them as ascii characters, it could have some application as a form of password encryption (if you wanted the game to output a password so that the person could come back and play later, but if someone decompiles your game, they couldnt easily see how the password was derived)


BBS Signature