<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jonathon Bolster &#187; bored</title>
	<atom:link href="http://www.bolsterweb.com/tag/bored/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bolsterweb.com</link>
	<description>Programming, drawing, random, life.</description>
	<lastBuildDate>Mon, 03 Oct 2011 17:30:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Little Red Riding Hood &#8211; In C#</title>
		<link>http://www.bolsterweb.com/2010/05/little-red-riding-hood-in-csharp/</link>
		<comments>http://www.bolsterweb.com/2010/05/little-red-riding-hood-in-csharp/#comments</comments>
		<pubDate>Sat, 29 May 2010 22:31:21 +0000</pubDate>
		<dc:creator>Jonathon Bolster</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Random Musings]]></category>
		<category><![CDATA[bedtime]]></category>
		<category><![CDATA[bored]]></category>
		<category><![CDATA[stories]]></category>

		<guid isPermaLink="false">http://www.bolsterweb.com/?p=136</guid>
		<description><![CDATA[Tonight, I was bored. Seriously, it was either this or watching the Eurovision song contest. Sadly, with BBC iPlayer, I was able to do both. I&#8217;ve decided that when I have kids, they&#8217;re going to know how to use computers and learn how to program (I can wish). So, I&#8217;ve rewritten the story of Little [...]]]></description>
			<content:encoded><![CDATA[<p>Tonight, I was bored. Seriously, it was either this or watching the Eurovision song contest. Sadly, with BBC iPlayer, I was able to do both.</p>
<p>I&#8217;ve decided that when I have kids, they&#8217;re going to know how to use computers and learn how to program (I can wish).</p>
<p>So, I&#8217;ve rewritten the story of Little Red Riding Hood. This time it&#8217;s in C#. I have no idea why I decided I would do this &#8211; by the time I&#8217;d realised, it was too late</p>
<p>So if you want to tell your kids a bedtime story whilst edging them into becoming programmers, read on.</p>
<p><span id="more-136"></span></p>
<pre class="brush: csharp; title: ; notranslate">
// The story of Little Red Riding Hood - as told in C#
var story = new Story()
{
	Name = &quot;Little Red Riding Hood&quot;,
	Description = &quot;The story of a girl who meets a wicked wolf on her way to her grandmother's house&quot;
};

// Once upon a time, there was one little girl who was loved by everyone. Her
// grandmother especially loved her and gave her a Red Riding Hood, which suited
// her so well that she would never wear anything else. So she was called 'Little
// Red Riding Hood'
var littleRedRidingHood = population.Where(x =&gt;
								x.Sex == Sex.Female &amp;&amp;
								x.Characteristics.HasFlag(Characteristics.Lovely) &amp;&amp;
								x.AgeGroup == AgeGroup.Child &amp;&amp;
								x.Clothes.Any(y =&gt;
									y.Name == &quot;Riding Hood&quot; &amp;&amp;
									y.Colour == Color.Red
									)
								)
								.OrderBy(x =&gt; x.Popularity)
								.First();

// One day, her mother gave her some cake and wine to take to her grandmother as
// she was ill. Little Red Riding Hood put them into her basket, and set off for
// grandmother's house.
var foodForGrandmother = cupboard.Where(x =&gt; x is Cake || x is Wine).ToList();

foreach (var foodItem in foodForGrandmother)
{
	cupboard.Remove(foodItem);
}

littleRedRidingHood.Basket.AddRange(foodForGrandmother);

// Her grandmother lived out in the wood and, just as Little Red Riding Hood entered
// the wood, she met a wolf. She was not afraid of him, since she did not know how
// wicked he was.
wood.Occupants.Add(littleRedRidingHood);

var wolf = new Character()
{
	Clothes = Enumerable.Empty&lt;Clothes&gt;(),
	CurrentLocation = wood,
	Home = wood,
	Popularity = 0,
	Characteristics = Characteristics.Wicked,
	Sex = Sex.Male
};

littleRedRidingHood.CurrentLocation = wolf.CurrentLocation;
littleRedRidingHood.Mood ^= Mood.Fear;

// The wolf asked where Little Red Riding Hood was going. She explained that she
// was bringing her grandmother some food, as she was poorly. The wolf asked
// where her grandmother lived.
wolf.Knowledge.Add(grandmother.Home);

// The wolf thought Little Red Riding Hood looked tasty and would be much better
// to eat than the old woman. He knew he must be crafty, to catch both.
wolf.Appetite.Add(grandmother);
wolf.Appetite.Add(littleRedRidingHood);
wolf.Mood |= Mood.Hungry;

// The wolf convinced Little Red Riding Hood to stray from the path and pick flowers
// for her grandmother. Then he ran off to her grandmother's house whilst Little Red
// Riding Hood picked flowers; until she could carry no more.
wolf.CurrentLocation = grandmother.Home;

littleRedRidingHood.Basket.AddRange(
	wood.Items.Where(x =&gt; x is Flower).Take(littleRedRidingHood.Basket.MaxSize - littleRedRidingHood.Basket.Count)
	);

// The wolf reached grandmother's house and knocked on the door. 'Who is it?', she asked.
// 'Little Red Riding Hood', replied the wolf. 'She is bringing wine and cake'.
// 'Lift the latch', grandmother said, 'I am too weak to open it'. The wolf opened the
// door and, without saying a word, immediately jumped in and gobbled up grandmother.
wolf.Belly.Add(grandmother);
wood.Occupants.Remove(grandmother);

// The wolf heard Little Red Riding Hood come close, and quickly put on the grandmother's
// clothes and jump into her bed.
wolf.Clothes = grandmother.Clothes;

// Little Red Riding Hood reached her grandmother's house and, seeing the door open, felt
// very uneasy - but entered anyway.
littleRedRidingHood.CurrentLocation = grandmother.Home;
littleRedRidingHood.Mood |= Mood.Nervous;

// 'Oh grandma, what big ears you have', said Little Red Riding Hood
// 'All the better to hear you with, my child', replied the wolf
if (!wolf.Features.Ears.Equals(grandmother.Features.Ears))
{
	littleRedRidingHood.Mood |= Mood.Nervous;
}

// 'But, grandmother, what big eyes you have', she said.
// 'All the better to see you with, my dear'
if (!wolf.Features.Eyes.Equals(grandmother.Features.Eyes))
{
	littleRedRidingHood.Mood |= Mood.Nervous;
}

// 'But, grandmother, what large hands you have!'
// 'All the better to hug you with'
if (!wolf.Features.Hands.Equals(grandmother.Features.Hands))
{
	littleRedRidingHood.Mood |= Mood.Nervous;
}

// 'Oh! grandma, what a terribly big mouth you have'
// 'All the better to eat you with!'
if (!wolf.Features.Mouth.Equals(grandmother.Features.Mouth))
{
	littleRedRidingHood.Mood |= Mood.Fear;
}

// And with that, the wolf jumped out of bed and gobbled up Little
// Red Riding Hood!
wolf.Belly.Add(littleRedRidingHood);
wolf.Appetite.Clear();

// After appeasing his appetite, the wolf lay on the bed, fell
// asleep and began snoring very loudly.
wolf.Mood = Mood.Tired;

// Just then, a huntsman was passing and thought to himself 'How
// the old woman is snoring! I must see if she wants anything.'
var huntsman = new Character()
{
	Characteristics = Characteristics.Strong,
	AgeGroup = AgeGroup.Adult,
	Sex = Sex.Male,
};

huntsman.CurrentLocation = grandmother.Home;

// The huntsman entered the house and saw the wolf sleeping.
// 'You!', exclaimed the huntsman, 'How long I have looked for you
// wicked thing' and raised his gun to shoot the wolf
huntsman.Mood = Mood.Anger;

// The huntsman realised the wolf may have eaten the old woman, and
// put his gun down. Instead, he used some scissors to open the wolf's
// belly. Out sprang Little Red Riding Hood. 'How frightened I have
// been', she told him.
wolf.Belly.Remove(littleRedRidingHood);
littleRedRidingHood.Mood ^= Mood.Fear;
littleRedRidingHood.Mood |= Mood.Happy;

// Out followed her grandmother, but scarcely able to breath.
wolf.Belly.Remove(grandmother);
grandmother.Mood = Mood.Happy | Mood.Tired | Mood.Hungry;

// Little Red Riding Hood ran out into the wood and picked up stones
// to fill the wolf's belly with
var stones = wood.Items.Where(x =&gt; x is Stone).Take(wolf.Belly.MaxSize);
wolf.Belly.AddRange(stones);

// When the wolf awoke, he tried to run away but the stones were so heavy
// that he fell at once, and fell dead
wolf.Mood ^= Mood.Tired;
wolf.Mood |= Mood.Shocked;
wood.Occupants.Remove(wolf);
wolf = null;

// The wolf had died, and the three were happy. The grandmother ate the cake
// and drank the wine, and was revived.
huntsman.Mood = Mood.Happy;
littleRedRidingHood.Mood = Mood.Happy;

grandmother.Belly.AddRange(littleRedRidingHood.Basket.Where(x=&gt; x is Cake || x is Wine));
grandmother.Mood = Mood.Happy;

// The huntsman took the skin off the wolf and took it home, and Little Red
// Riding Hood learnt that she should never stray off the path, especially
// when wolves tempt her.
//Assert.IsTrue(wolf.Characteristics.HasFlag(Characteristics.Wicked));

// The End
story.Ending = Ending.Happy;
</pre>
<p>This actually compiles and runs to a happy ending (with the source of the other classes, of course). If you&#8217;re going to read it as a bed time story to your children, it may as well compile.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bolsterweb.com/2010/05/little-red-riding-hood-in-csharp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

