
Title:
Foxes And Hens Solution  Design of Computer Programs

Description:

Here's my answer. Here's the definition for the do function.

I make a random choice of the cards,

and I replace that card in the deck with the empty string.

I do that once, and that gives me the cards left without that 1 card.

Then if the action is gathering, I add to my score the number of hens that were in the yard,

and there's no more hens left in the yard.

If the action was waiting and a hen shows up,

then I add that to the number of hens in the yard.

And if the action was waiting and a fox shows up,

then, oops, I don't add anything to my score and the yard goes down to 0,

and I've still got those cards left.

Then if something went wrong, if the strategy function returned a bad value or something,

then I just don't do anything and I return the state.

And since we didn't specify what to do, you're free to do what you want in that case.

You could have raised an error. That would be a reasonable thing to do.

But the easiest is just to return the state.

Here's my strategy function.

I didn't bother writing out the optimal function, although you certainly could do that.

What I did is first I said, "We're counting cards."

"We better take advantage of that to some degree."

"If there are no foxes left, then we might as well wait

"and gather up all the rest of the chickens."

So I made sure I did that.

And then I tried all possible values of how long we should wait for

and found that 3 was the best.

So we wait until there's 3, then we gather them,

and if there's less than 3, then we wait.

Is this strategy superior? The answer is yes, it is.

By how much? By almost 3 points.

Take5 averaged 29.57, and my strategy averaged 32.3.