Sunday, 1 March 2015

More Recursion !

Hey everyone, (everyone who's reading this at least)

I hope your reading week was fun, relaxing and as unproductive as mine. I think the only thing I worked on is assignment 2 with my partner Cindy (http://csc148x.blogspot.ca/ ).

Since recursion is part of the assignment this time, it only makes sense that I write about it.
 In my previous post from February 6, I mentioned what recursion is and how it is supposed to work.

In assignment 2, we use a MiniMax which is a strategy generally used to two-player games where the best possible move is chosen by both players. For this particular we need to use recursion.
Since we're not allowed to give out assignment details, I'll try to explain it as simply as possible.
Recursion calls on itself again and again till it reaches the base case or the stopping case, and in the case of Minimax, both players need to have complete knowledge of the game.
For Minimax to work, we use recursion to find out what each move will lead to and will that move end in a win or loss. So we call on our method again and again until it reaches the end of the game for each move available in that game state.

To illustrate how recursion works, I'm going to show a simple example. A simple thing to remember is that recursion works in place of iterative functions.

*****************************
def factorial (n):
    """ (int) -> int

    Return factorial of n.

    >>> factorial (4)
    24
    >>> factorial (12)
    479001600
    """
    if n == 1 :
        return 1
    else :
        return n * factorial (n-1)

*****************************

In the example, "if n == 1 " plays the role of the stopping case or base case. Once it reaches here, the recursion process stops and the values start getting returned.
When n == 4, the else condition is executed and thus factorial calls itself but this time n = 3.
This continues until n = 1 and so the value 24 is returned to the user.

While recursion as a concept is easy to understand, and even easy to trace, it isn't however easy to write.
The assignment is giving us a little bit of trouble at exactly this and we hope to figure it out soon.

Hope this made sense!

Until next time ...

PS - I hope I'm not the only one not getting this part of the assignment ! Good luck to everyone ! :)



1 comment:

  1. I like your description of recursion through the example, nice! Also I could relate to the minimax frustration at the time this was written and I can still relate to it now! Part B) on A3 is going to be the path for me... waaay too done with minimax xD

    ReplyDelete