A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Recursive programming

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834

    Recursive programming

    Does anyone know where some good recursive tutorials are. I would like to try and learn more about this concept. I'm sorry if this has been covered but I haven't really been able to find anything good.
    .

  2. #2
    FK Newb Beater
    Join Date
    Dec 2002
    Location
    Seattle
    Posts
    676
    I would be interested in this also. It seems to have only whimsical applications in flash but it looks like good brain exercise!
    Just because you changed the code, doesn't mean it's yours

    Most Recent Work:
    Commercial tanning beds website

  3. #3
    Senior Member
    Join Date
    Apr 2006
    Posts
    1,059
    I doubt theres any good online tutorials for recursion...most people learn it in a class setting i think....


    But its not really all that complicated If you have specific questions ask em Im sure someone can help you

  4. #4
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Are there any good explanations about it? I would like to get a good overview about it. All that I've really found for it is: http://www.actionscript.com/Article/...f/Default.aspx but that deals with xml and http://en.wikipedia.org/wiki/Recursive_function which I'm not sure what to think and somewhere here http://www.gotoandplay.it/_articles/index.php These are all kind of insuficiant or maybe I should just read them over again ):
    .

  5. #5
    Senior Member
    Join Date
    Apr 2006
    Posts
    1,059
    hmm maybe your looking at more complex stuff than i was...or maybe its just alot easier than those make it sound

    recursive functions to me are just functions that call themselves...but then again ive forgotten most of the math stuff i used to know

  6. #6
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    I would like some examples so that I can become familiar with them. My brother talked about the best way to make a rubics cube solver would be through recursive functions. Would you happen to have any good examples for it's use? How about a list of some things that recursive functions would be good for so that I can try it out that way. I do have a book that talks a bit about it but only about 3 pages.
    .

  7. #7
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Any problem that involves looping or something like a series can be solved recursively.

    There's a base case and a recursive case.

    The base case is when you're done and you actually return a final response.

    The recursive case is where you call the function again with a modification to the paramaters, so it's not an infinite loop.

    For example, you can find the maximum element in an array with a recursive function.

    Code:
    public static int maxElement(int[] array, int start, int max) {
    		if(start == (array.length)) {
    			return max;
    		} else if(array[start] > max) {
    			return maxElement(array, (start+1), array[start]);
    		} else {
    			return maxElement(array, (start+1), max);
    		}
    	}
    That's a recursive method I've written in Java.. it's very similar to Flash, so I didn't rewrite it.

  8. #8
    Senior Member
    Join Date
    Apr 2006
    Posts
    1,059
    heh id have to be able to solve a rubiks cube normally to figure out a function to solve it

    AD's example is a good example of a recursive function...Im sure you could make a recursive function to solve a rubiks cube but first youd have to figure out a logic to use to solve the rubiks cube

  9. #9
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Actually, a great example would be factorial.

    The base case would be when the variable is equal to zero that you return 1.

    Otherwise, multiply the variable by calling the function again with the variable minus one.

    Code:
    function factorial(x:Number):Number {
    	if(x == 0) { //base case
    		return 1;
    	} else {     //recursive case
    		return(x * factorial(x - 1));
    	}
    }

  10. #10
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    This is accually the examle that I found in the book.
    Quote Originally Posted by AfternoonDelite
    function factorial(x:Number):Number {
    if(x == 0) { //base case
    return 1;
    } else { //recursive case
    return(x * factorial(x - 1));
    }
    }
    Does anyone know where a group of these functions. What I would like to do is try a function that compairs two strings to see how alike they are. I kind of figure that recursive functions would be the best.
    Last edited by swak; 05-11-2006 at 12:06 AM.
    .

  11. #11
    Senior Member
    Join Date
    Apr 2006
    Posts
    1,059
    what criteria are you trying to use to compare the strings...a recursive function might not be the best for what you are trying to do

  12. #12
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    The download is a swf but just change the extention to and swf or just open it up with the player. It's a little movie of what I'm aiming for. The problem with what a currently have is it gets unaligned. I guess that I'll have to try limits.
    Last edited by swak; 10-08-2007 at 07:42 AM.
    .

  13. #13
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Yeah, you could do that recursively.

    Just pass in both String objects and a starting position for each string

    if the starting position for a String has incremented to equal the string's length, return false

    else, if the character for both starting positions are equal to each other.. call the function by increasing both starting positions

    else, call the function by increasing only the starting position of the string you want to continue looking for the character at... well, like the swf you attached does.

    That's just right off the top of my head, so if it doesn't make sense, I can clarify. ;P

  14. #14
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Or, another way to do it without using starting positions would be to substr the String each time you call the function depending on the condition.

  15. #15
    Senior Member
    Join Date
    Apr 2006
    Posts
    1,059
    yeah but Im still not sure how you want to compare the strings....

    do you just want to see how many letters the two strings have in common?
    or how manyletters they have in common in the same position

    all strings are basically arrays

    If you used AS for that example im sure you know that...but since i cant see your code I have to assume that you just made a movieclip...
    code:

    string1 = "ths is Text."
    string2 = "this is text." //set the strings

    if(string1.length >= string2.length){MaxIdx = string1.length - 1}//check which string is longer
    else{ MaxIdx = string2.length - 1} //set our max number to the last possible index
    var sameLetterCount:Number = 0
    for(i = 0;i < MaxIdx ; i++){
    if(string1[i] == string2[i]){sameLetterCount++}

    }
    if(sameLetterCount == MaxIdx+1){trace("These 2 strings are Identical")}
    else if (sameLetterCount > (MaxIdx *0.9)){trace("These 2 strings are Very 90%+ simillar but not Identical")
    else if(sameLetterCount > MaxIdx*0.70){trace("These to strings are fairly similar 70%+ match")
    ...(and so on)...
    else{
    trace("These two strings have "+sameLetterCount+"Of the exact same letter in the exact same possition")}



    might not be the most efficient or cleanest way of doing it but it should work

    if your just trying to find how many letters are in the exact same position in both strings(case Sensitive)

    AD's recursive function would also work...since you were asking about recursive functions(my example is not recursive at all)

  16. #16
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Look at http://www.typhoonent.com/text%20comparison.swf This is how far I've gotten. It works perfetly fine until you miss a letter then it becomes unaligned and gets totally messed up. Note: I've recently tried it and it didn't seem to want to work in the webbrowser so you can try it from the desktop. The correct answer for it is (This is a string.) just like that punctuation and all. if you type in something like (this is a string.) than it doesn't work because it keeps on searching for the capital 'T'. I guess another option that I could try is count how many of each type of character there is in there and compaire and see if they get all the correct letters and check the lenght to see that they don't go over. I'm just thinking about other avenues because this doesn't seem to be working for me. If anyone can think of a better way that does work than I would like to see it. Here's the fla: http://www.typhoonent.com/text%20comparison.fla
    .

  17. #17
    Senior Member
    Join Date
    Apr 2006
    Posts
    1,059
    srry to sound dense but let me make sure i undestand what you want to do

    you are checking for Acurracy of a typed string against a preset string(some type of learn to type program)
    and you want to record how many letters are the same, how many letters are wrong and what % they got to typing it right

    so far so good?
    now is where i get confused you want to have focuse on matching exact(T not t) or it doesnt matter capitalization and punctuation?)

    Im guessing you want exact(including caps and punc.)?

    if someone

    types

    This
    is that 5 right and 0 wrong or is that 5 right and 13 wrong (" is a string." was not typed)?

  18. #18
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    What I'm trying to do is make a game where instead of leveling up you have to "reprogram" your character's moves to improve them, not real programing. I want to have them type in something and depending on how close it is to the real thing they get a percentage. Check out my example. My example is pretty much what I want exept for the problem of it becoming unaligned. Say if you type in Ths is a string. It will see that the i is missing then it will continue until it finds another i.
    .

  19. #19
    Senior Member
    Join Date
    Apr 2006
    Posts
    1,059
    code:

    var sentence:String = "This is a string.";




    onEnterFrame = function () {
    var fault:Number = 0;
    var correct:Number = 0
    var accuracy:Number = 0 ;
    var myStringToMatch:String = IN.text
    for(i = 0 ; i< myStringToMatch.length; i++){

    if(sentence.charAt(i) == myStringToMatch.charAt(i)){ correct++ }
    }
    fault = myStringToMatch.length - correct
    accuracy = (correct/myStringToMatch.length)*100
    accuracy = Math.floor(accuracy);
    if(isNaN(accuracy)){accuracy = 0}
    if(isNaN(fault)){fault = 0}
    flt.text = fault;
    acure.text = accuracy+"%";
    OUT.text = correct;
    };




    try that ans see if thats what you want(just replace your code with that)


    you still did if you want the accuracy based on the whole sentance(instead of just what they type) change

    accuracy = (correct/myStringToMatch.length)*100
    to
    accuracy = (correct/sentence.length)*100

    hehe its not recursive at all but meh :P
    Last edited by joran420; 05-11-2006 at 01:41 AM.

  20. #20
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    This code has a bit of the same trouble as mine does. It gets really funky if you miss a letter. It still needs work. I'm thinking that I'll have to limit it within boundries and if it exceeds then move on.
    .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center