Edit: I began writing this before a different post appeared with the same point. Alas!
I'm sorry to be a jerk, but I found your code pretty tough to decipher. Here's my thought process:
NUMBER_OF_PLAYERS could be, say, 6.
the caller calls: throw(ball, 0);
0
So, what is ball? It's either an integer and you're just passing its address around for no particular reason, or it's an integer array and you're using angle() to move around inside the array somehow? Then what exactly is stored in that array, the positions of people? Let's imagine it's 1s and 0s to denote a person or not.
After making that leap of logic, your code starts to make sense. You're adding "some amount" to the address, and then whatever is at that address (1 for hit, 0 for nothing) to hits, and passing them into the next invocation, as long as it's less than the necessary number of hits. Could easily be a while loop, but I'll get to that in a minute. I have some other points:
- Ball isn't a great name for the list of the positions of the players.
- It isn't clear that ball is an array.
- There's a pointless 'else return'.
- You never address the "I don't have a ball" situation!
This code requires explanations of arrays, pointers, and addresses before you can talk about recursion. I'd express how to play dodgeball to a new programmer as follows:
throw(angle, enemy_positions):
# This function takes an angle and a list of
# the spots where other people are, removing
# them from their spot if it registers a hit.
# Returns true if it's a hit, false if a miss.
find_ball():
# Tries to find a ball; returns the number
# which you found -- 0, 1, or 2.
load_enemy_team():
# Returns a list of the spots where other players are
enemy_positions = load_enemy_team()
players_on_other_team = enemy_positions.length()
ball_count = 0 # all balls start in the middle!
while players_on_other_team > 0:
if ball_count > 0:
angle = get_throw_angle() # user inputs this
hit = throw(angle, team_positions)
if hit:
shout("Yahoo!")
players_on_other_team -= 1 # they're down a guy!
else:
ball_count += find_ball() # reload
That should cover it - though I'm sure there's problems with my code too. :)
The biggest point I want to make is that this example is trivially easy to represent without recursion. While recursion can be used in place of any loop, a non-programmer's mind will likely arrive at iteration first for tail-recursive examples. And once someone understands how to solve a problem one way, teaching them a totally different way can be tough.
I personally think people do pretty well at understanding how recursion applies with respect to exploring a maze, which is almost as universal an experience as dodgeball.