Raised This Month: $51 Target: $400
 12% 

[ Solved ] with code that change 1 letter


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
daniel46
Senior Member
Join Date: Dec 2011
Old 06-18-2014 , 06:39   [ Solved ] with code that change 1 letter
Reply With Quote #1

so i made this code
Code:
{
	new Rletter[ 1 ];
	formatex( Rletter, charsmax(Rletter),"%s" ,  Chars[ random( sizeof( Chars ) ) ] )
	while( !(containi( TheWord , Rletter ) ) )
	{
		formatex( Rletter, charsmax(Rletter),"%s" ,  Chars[ random( sizeof( Chars ) ) ] )
	}
	replace( TheWord , charsmax(TheWord) , Rletter , "_" )
}
and it dosent work i need to first check if the letter is in the word and then change the letter by _ but its seems to not work and debug say its in the replace function tho i think i did it bad
daniel46 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-18-2014 , 06:44   Re: Help with code that change 1 letter
Reply With Quote #2

  • A string declared with only one cell will never work. A 1 character string requires 2 cells. Also, you would need to use the %c instead of %s.
  • You potentially have an infinite loop.
  • Simply use a for loop to loop through all the characters and put replace_all() in the loop for each character.
__________________

Last edited by fysiks; 06-18-2014 at 06:46.
fysiks is online now
daniel46
Senior Member
Join Date: Dec 2011
Old 06-18-2014 , 06:59   Re: Help with code that change 1 letter
Reply With Quote #3

first 2 things ty i didnt know it but the last one so i need new meta cause it will just remove the same letter evry time and i want it to remove random words
daniel46 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-18-2014 , 07:02   Re: Help with code that change 1 letter
Reply With Quote #4

Quote:
Originally Posted by daniel46 View Post
first 2 things ty i didnt know it but the last one so i need new meta cause it will just remove the same letter evry time and i want it to remove random words
What? That makes no sense. Explain what you are actually trying to do (without mentioning any code).
__________________
fysiks is online now
daniel46
Senior Member
Join Date: Dec 2011
Old 06-18-2014 , 07:12   Re: Help with code that change 1 letter
Reply With Quote #5

ok so i want to replace randomly letter every time the i will use this function means

the word hello could be one time
_ello
hell_

so if i will just loop it it will always be h_llo cause the first letter in the constant is E from the other letters

sorry for my bad english and sorry it is hard to understand me and tank you for helping me

Last edited by daniel46; 06-18-2014 at 07:21.
daniel46 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-18-2014 , 07:22   Re: Help with code that change 1 letter
Reply With Quote #6

If the error only occurs in the replace funciton then it is likely because Rletter is not a valid string. Make it a valid string (dimension it as with 2 cells).

However, it would be better to completely eliminate Rletter and any formats. Simply use the Chars[random(sizeof(Chars))] in the containi() function (make sure that it is an array of strings and not characters; I can't tell if you have the correct structure or not based on the code that you have given).

This still leaves the possibility of an infinite loop so you would want to put in a timeout. Alternatively, you can find a "only once random function" so that you will be able to know that you hit each letter at least once. This means your loop will have a maximum number of executions (you probably would use a for loop with a break in this case).
__________________
fysiks is online now
daniel46
Senior Member
Join Date: Dec 2011
Old 06-18-2014 , 07:35   Re: Help with code that change 1 letter
Reply With Quote #7

yeah i thought about what you said and i did it like this

Code:
new Num = random(sizeof(Chars));
	while( !(contain( TheWord , Chars[ Num ] ) ) )
	{
		Num = random(sizeof(Chars));
	}
	replace( TheWord , charsmax(TheWord) , Chars[ Num ] , "_" )
but it wont work it just write normaly the word
daniel46 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-18-2014 , 07:38   Re: Help with code that change 1 letter
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
I can't tell if you have the correct structure or not based on the code that you have given.
Aka, please post the whole code.
__________________

Last edited by fysiks; 06-18-2014 at 07:38.
fysiks is online now
daniel46
Senior Member
Join Date: Dec 2011
Old 06-18-2014 , 07:41   Re: Help with code that change 1 letter
Reply With Quote #9

well this is the whole code and when its doing the function

Code:
public athegame( client )
{
	WordNumber = random( sizeof( words_count ) )
	formatex(TheWord,charsmax(TheWord),"%s", games_words[ WordNumber ] )
	Reworkword()
	ColorChat( 0, "%s", TheWord )
}
Code:
stock Reworkword( )
{
	new Num = random(sizeof(Chars));
	while( !(containi( TheWord , Chars[ Num ] ) ) )
	{
		Num = random(sizeof(Chars));
	}
	replace( TheWord , charsmax(TheWord) , Chars[ Num ] , "_" )
}
and words are taken from file and the file works fine the function that should replace random letter in _ dont work cause its just type the normal word
daniel46 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-18-2014 , 07:45   Re: Help with code that change 1 letter
Reply With Quote #10

First of all, that is not the whole code because I can't see how the Chars variable is created. Also, I can't tell if there are other issues created by your global variables. You should attache the whole .sma file to your next post to make it easier for people to help you.
__________________
fysiks is online now
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:18.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode