Programming The Robot from Diary of a Wimpy Kid

My daughter Lily has been reading her first ever novel, ' Diary of a Wimpy Kid' (recommended by Joe Cooney) and one part of the book led her to do some programming with me. Yay for reading! Yay for programming! and yay for my daughter!

Here's the story that inspired her:

We got to work. Someone had the idea that you can say your name to the robot and it can say it back to you.

Robot says "Hi BOB it is very nice to meet you BOB."

But then someone else pointed out that you shouldn't be able to use bad words for your name, because the robot shouldn't be able to curse. So we decided we should come up with a list of all the bad words the robot shouldn't be able to say.

We came up with all the regular bad words, but then Ricky Fisher came up with twenty more the rest of us had never even heard before.

So Ricky ended up being one of the most valuable contributors on this project.

Right before the bell rang, Mr Darnell came back in the room to check on our progress. He picked up the piece of paper we were writing on and read it over.

Mr Darnell looks over the piece of paper

To cut a long story short, Independent Study is cancelled for the rest of the year.

Diary of Wimpy Kid, Jeff Kinney

I said to Lily we could make a program like that and—get this—she was actually excited about the idea. So we sat down together and made a program. I asked for her help with every bit of it. Here is how it went:

namespace feedback
{
    using System;
    using System.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello.");

            while (true)
            {
                Console.WriteLine("What is your name?");
                var name = Console.ReadLine();
                Console.WriteLine("Hi " + name + " it is very nice to meet you " + name + ".");
            }
        }
    }
}

Next we put in a secret codeword, 'Exit' that you could use when you want to stop.

if (name == "exit")
{
    break;
}

And then a secret codeword so if it recognised you it would change color:

if (name == "Lily")
{
    Console.ForegroundColor = ConsoleColor.Magenta;
}
else
{
    Console.ResetColor(); 
}

Lily said we could make it say your name backwards! So I tried this...

var backwardsName = name.Reverse();
Console.WriteLine("Hello " + backwardsName);

But instead of printing the name backwards, this printed...

Hello System.Linq.Enumerable+<ReverseIterator>‌d__a0`1[System.Char]

Try explaining THAT to a 6 year old.

It was easier to do it like this:

var backwardsName = "";

foreach (var letter in name)
{
    backwardsName = letter + backwardsName;
}

Console.WriteLine("Hello " + backwardsName);

That worked much better.

Now Lily had an even better idea. What if the robot said a different word for every letter of their name.

My work friends will know that Phonetic Alphabets are something we play with quite a bit, in order to make dealing with IT support easier. But Lily independently came up with this idea, without any prompting from me.

So if you're name is Leon it would say "Hello Lemon-Echo-Octopus-Nose". Or words to that effect.

                var wordyName = "";
                foreach (var letter in name.ToUpperInvariant())
                {
                    letterNumber = Convert.ToInt32(letter) - 65;
                    if (letterNumber >= 0 && letterNumber < AlphabetWords.Length)
                    {
                        wordyName += AlphabetWords[letterNumber] + " ";
                    }
                }
                
                Console.WriteLine("Hello " + WordyName);

And this relied on coming up with a big alphabetic list of words. We had fun doing this together.

static string[] AlphabetWords = { 
    "Apple",
    "Bunya-Mountains",
    "Custard",
    "Dirt",
    "Eyeball",
    "Funny",
    "Gorilla-Gnome",
    "Hexaphone",
    "Igloo",
    "Jellybean",
    "Kitten",
    "Lolly",
    "Monkey",
    "NO!!!!",
    "Octopus",
    "Penny",
    "Queen",
    "Rhino",
    "Sugar",
    "Trickster",
    "Up",
    "Viking",
    "Warrior",
    "Xray-fish",
    "Yucky",
    "Zebra"};

Well, there it is. And a big thumbs up to Jeff Kinney for teaching people from an early age that user input is not to be trusted.

 

I'm currently writing a book about how to build your first product. If building a product is something you'd like to do, then sign up to be notified when the book is available.

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.