Word Scrambling
#1

Now, is it possible to scramble a word if it is, it should be like this:

i enter the word : "duty"
it gives me back : "uydt"

? Help please.
Reply
#2

I'll write this in the next hour if nobody beats me to it
Reply
#3

Well since strings are just arrays you can randomize your array pretty easy...

Код:
CMD:randword(playerid, arg[])
{

	RandomizeWord(arg);
	SendClientMessage(playerid, 0xFFFF0000, arg);
	return 1;
}

stock RandomizeWord(word[])
{
    new tmp;
    new r;

    for(new i = strlen(word) - 1; i >= 0; i--)
    {
        r=random(strlen(word));
        tmp = word[i];
	word[i] = word[r];
	word[r] = tmp;
    }
}
Reply
#4

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
Well since strings are just arrays you can randomize your array pretty easy...

Код:
CMD:randword(playerid, arg[])
{

	RandomizeWord(arg);
	SendClientMessage(playerid, 0xFFFF0000, arg);
	return 1;
}

stock RandomizeWord(word[])
{
    new tmp;
    new r;

    for(new i = strlen(word) - 1; i >= 0; i--)
    {
        r=random(strlen(word));
        tmp = word[i];
	word[i] = word[r];
	word[r] = tmp;
    }
}
lol beat me to it, just wrote one out
Reply
#5

Lets see your code anyways.
Reply
#6

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
Well since strings are just arrays you can randomize your array pretty easy...

Код:
CMD:randword(playerid, arg[])
{

	RandomizeWord(arg);
	SendClientMessage(playerid, 0xFFFF0000, arg);
	return 1;
}

stock RandomizeWord(word[])
{
    new tmp;
    new r;

    for(new i = strlen(word) - 1; i >= 0; i--)
    {
        r=random(strlen(word));
        tmp = word[i];
	word[i] = word[r];
	word[r] = tmp;
    }
}
The only problem with that code is that if I randomized a word, say "computer", it could get scrambled as "mputccoo", as it has the possibility of re-using letters
Reply
#7

pawn Код:
stock Scramble(string[], size=sizeof(string))
{
    new rand, tmp;
    for(new i=0; i < size; ++i) if(string[i] == ' ' || string[i] == '\0') size=i-1;
   
    for(new i=0; i < size; ++i)
    {
        rand=random(size);
        tmp=string[i];
        string[i]=string[rand];
        string[rand]=tmp;
    }
    return 1;
}
Mine's probably not as optimized as yours though, but was making sure was only 1 word gettin scrambled lol
Reply
#8

Quote:
Originally Posted by MattyG
Посмотреть сообщение
The only problem with that code is that if I randomized a word, say "computer", it could get scrambled as "mputccoo", as it has the possibility of re-using letters
I don't know what glue stick your sniffing but that can't happen.

Antonio: It is true if there is spaces the spaces will get mixed into the array so each word wouldn't be scrambled in sequence requires an additional stock function
Reply
#9

Lock, [uL]Pottus, thanks, I took your code.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)