ExquisiteCorpse - Call method with random arguments

I am doing the first extension to the ExquisiteCorpse project: “Extend the BuildACreature() method so that all of its parameters are optional. It should assign a random body part if a parameter is not specified.”

It seems straightforward to make the parameters optional, thus:

static void BuildACreature(string head = “bug”, string body = “bug”, string feet = “bug”)

How can I randomise a body part if no argument is specified? I’ve searched the .NET documentation to no avail.

Full code:

using System;

namespace ExquisiteCorpse
{
class Program
{
static void Main(string args)
{
BuildACreature(body: “monster”);
}

    static void BuildACreature(string head = "bug", string body = "bug", string feet = "bug")
    {
        int headNum = TranslateToNumber(head);
        int bodyNum = TranslateToNumber(body);
        int feetNum = TranslateToNumber(feet);
        SwitchCase(headNum, bodyNum, feetNum);
    }

    static void RandomMode()
    {
        Random randomNumber = new Random();
        int head = randomNumber.Next(1, 4);
        int body = randomNumber.Next(1, 4);
        int feet = randomNumber.Next(1, 4);
        SwitchCase(head, body, feet);
    }

    static void SwitchCase(int head, int body, int feet)
    {
        switch (head)
        {
            case 1:
                GhostHead();
                break;
            case 2:
                BugHead();
                break;
            case 3:
                MonsterHead();
                break;
        }
        switch (body)
        {
            case 1:
                GhostBody();
                break;
            case 2:
                BugBody();
                break;
            case 3:
                MonsterBody();
                break;
        }
        switch (feet)
        {
            case 1:
                GhostFeet();
                break;
            case 2:
                BugFeet();
                break;
            case 3:
                MonsterFeet();
                break;
        }
    }

    static int TranslateToNumber(string creature)
    {
        switch (creature)
        {
            case "ghost":
                return 1;

            case "bug":
                return 2;

            case "monster":
                return 3;

            default:
                return 1;
        }
    }

    static void GhostHead()
    {
        Console.WriteLine("     ..-..");
        Console.WriteLine("    ( o o )");
        Console.WriteLine("    |  O  |");
    }

    static void GhostBody()
    {
        Console.WriteLine("    |     |");
        Console.WriteLine("    |     |");
        Console.WriteLine("    |     |");
    }

    static void GhostFeet()
    {
        Console.WriteLine("    |     |");
        Console.WriteLine("    |     |");
        Console.WriteLine("    '~~~~~'");
    }

    static void BugHead()
    {
        Console.WriteLine("     /   \\");
        Console.WriteLine("     \\. ./");
        Console.WriteLine("    (o + o)");
    }

    static void BugBody()
    {
        Console.WriteLine("  --|  |  |--");
        Console.WriteLine("  --|  |  |--");
        Console.WriteLine("  --|  |  |--");
    }

    static void BugFeet()
    {
        Console.WriteLine("     v   v");
        Console.WriteLine("     *****");
    }

    static void MonsterHead()
    {
        Console.WriteLine("     _____");
        Console.WriteLine(" .-,;='';_),-.");
        Console.WriteLine("  \\_\\(),()/_/");
        Console.WriteLine("   (,___,)");
    }

    static void MonsterBody()
    {
        Console.WriteLine("   ,-/`~`\\-,___");
        Console.WriteLine("  / /).:.('--._)");
        Console.WriteLine(" {_[ (_,_)");
    }

    static void MonsterFeet()
    {
        Console.WriteLine("    |  Y  |");
        Console.WriteLine("   /   |   \\");
        Console.WriteLine("   \"\"\"\" \"\"\"\"");
    }
}

}

Isn’t that two separate problems? Determining whether something was specified, and choosing a random value from some set of alternatives?
If you’re trying to find one thing that solves both, maybe that’s why you don’t find anything.

1 Like

I don’t think that’s two separate things. All I want to know is if I can have a method signature with something like:

static void BuildACreature(string head = RandomMode1, string body = RandomMode2, string feet = RandomMode3)

So the default value would be randomly generated. It should be possible, because the Codecademy question is: “Extend the BuildACreature() method so that all of its parameters are optional. It should assign a random body part if a parameter is not specified.”

Of course, I may be barking up the wrong tree, but that’s another matter.

In C# it is. A default value has to be assigned prior to compiling. The result of a call to another method may eventually be a value, but it isn’t resolved prior to compiling. See this simplified example. After running the code as is, uncomment the commented out code, and run it again noting the exception thrown.

You could assign a default value of say an empty string, and then test each parameter inside the method. If it is an empty string, assign a random value instead.

That’s expressed as two separate things.

If missing
choose random

I am struggling with that part as well.

I tried to rebuild the BuildACreature() method like this:

static void BuildACreature(string head = "", string body = "", string feet ="")
    {
      if (head == "")
      {
        Random randomNumber = new Random();
        int headInt = randomNumber.Next(1,4);
        switch (headInt)
      {
        case 1:
          GhostHead();
          break;
          
        case 2:
          BugHead();
          break;
          
        case 3:
          MonsterHead();
          break;
          
        default:
          Console.WriteLine("Error. No valid number.");
          break;
      }
      }
      else if (body == "")
      {
        Random randomNumber = new Random();
        int bodyInt = randomNumber.Next(1,4);
        switch (bodyInt)
      {
        case 1:
          GhostHead();
          break;
          
        case 2:
          BugHead();
          break;
          
        case 3:
          MonsterHead();
          break;
          
        default:
          Console.WriteLine("Error. No valid number.");
          break;
      }
      }
      else if (feet == "")
      {
        Random randomNumber = new Random();
        int feetInt = randomNumber.Next(1,4);
        switch (feetInt)
      {
        case 1:
          GhostHead();
          break;
          
        case 2:
          BugHead();
          break;
          
        case 3:
          MonsterHead();
          break;
          
        default:
          Console.WriteLine("Error. No valid number.");
          break;
      }
      }
      else
      {
        int headNum = TranslateToNumber(head);
        int bodyNum = TranslateToNumber(body);
        int feetNum = TranslateToNumber(feet);
        SwitchCase (headNum, bodyNum, feetNum);
      }
    }

If I call the method in Main() it doesn’t throw an error and it kind of works, BUT it only prints random heads - my creature doesn’t have a body or feet. Calling the method with one parameter works as well, but it only prints the head of the creature i entered, e.g BuildACreature (“monster”) only prints a monster head withoud body or feet.

So I am stuck at these phase. If somebody has an idea how to chage the code it would be very helpful.

Check your code again, all of your switch cases refer back to methods featuring “Head”. :+1:t5: