0

I'm a beginner in C# and am creating a little MadLibs console game for practice. It's all working and now I'm trying to make it better, code cleaner, etc. Just switched from individual strings to an array for the user input and the story output, but I think there must be a more efficient way to do both.

Player class

namespace MadLib
{
static class Player
{
    public static string Input (string part)
    {
        string name;
        Console.WriteLine("Please enter a {0}: ", part);
        name = Console.ReadLine();
        return name;

        }

    }

}

Story code block

static class Story
{
    static public void Spooky()
    {
         string[] part = new string[10];

        //ask player for words

        part[0] = Player.Input("noun");

        part[1] = Player.Input("adjective");

        part[2] = Player.Input("adjective");

        part[3] = Player.Input("adjective");

        part[4] = Player.Input("occupation");

        part[5] = Player.Input("occupation");

        part[6] = Player.Input("occupation");

        part[7] = Player.Input("adjective");

        part[8] = Player.Input("noun");

        part[9] = Player.Input("noun");

        //output finished story

        Console.WriteLine("They all agreed that it was a huge {0}, {1}, {2}, and {3}."
            +" I have cross-examined these men, one of them a hard-headed "
                + "{4}, one a {5}, and one a moorland {6}"
                + ", who all tell the same story of this "
                + "{7} {8}, exactly corresponding to the {9} of the legend."
                , part[0], part[1], part[2], part[3], part[4], part[5]
                , part[6], part[7], part[8], part[9]);


 }
Amaan Iqbal
  • 761
  • 2
  • 9
  • 25
Richard H
  • 31
  • 6
  • 1
    It looks like you would want to use an object instead of an array. https://stackoverflow.com/questions/14747095/best-way-to-create-object – Master Jul 14 '17 at 15:14
  • 1
    Another thing: Why make the player class static? You clearly have an actual player, so you could end up doing `Player myPlayer = new Player();` and work off of that instance. Same with your story. Not that you *need* to, I've just found it's good practice to not make all of your stuff static. – Broots Waymb Jul 14 '17 at 15:28
  • I'm not even sure I would call it Person. A better name might be something like MadLibSentence. Then an instance of MadLibSentence could have its own collection of strings (noun, adjective, etc). MadLibSentence could have a method called GetInput that loops through the collection of strings to get the input for each string. – Blake Thingstad Jul 14 '17 at 15:39

3 Answers3

2

I would recommend repurposing the Player class and rename it MadLibSentence. This will allow you to create MadLibSentence objects ahead of time in a static class so that the parts and sentences are all defined. The MadLibSentence knows how to get input for its sentence and how to show the sentence one its done getting input.

class Program
{
    static void Main(string[] args)
    {
        MadLibs.Legend.Start();
        MadLibs.Another.Start();

        // to keep the console open
        Console.ReadKey(true);
    }
}

public static class MadLibs
{
    public static MadLibSentence Legend = new MadLibSentence(
        new List<string>()
        {
            "noun", "adjective", "adjective", "adjective",
            "occupation", "occupation", "occupation",
            "adjective", "noun", "noun"
        },
        "They all agreed that it was a huge {0}, {1}, {2}, and {3}."
            + " I have cross-examined these men, one of them a hard-headed "
            + "{4}, one a {5}, and one a moorland {6}"
            + ", who all tell the same story of this "
            + "{7} {8}, exactly corresponding to the {9} of the legend.");

    public static MadLibSentence Another = new MadLibSentence(
        new List<string>()
        {
            "noun", "adjective", "adjective"
        },
        "This is a {0} mad lib. I don't think it is {1} {2}.");
}

public class MadLibSentence
{
    private List<string> _parts { get; set; }
    private string _sentence { get; set; }

    public MadLibSentence(List<string> parts, string sentence)
    {
        this._parts = parts;
        this._sentence = sentence;
    }

    private List<string> GetInput()
    {
        var input = new List<string>();
        foreach (var part in _parts)
        {
            Console.WriteLine("Please enter a {0}: ", part);
            input.Add(Console.ReadLine());
        }
        return input;
    }

    public void Start()
    {
        Console.WriteLine(_sentence, GetInput().ToArray());
    }
}
Blake Thingstad
  • 1,639
  • 2
  • 12
  • 19
2

Here's another version, similar to what Blake had already posted, but not object oriented:

class Program
{

    static void Main(string[] args)
    {
        Console.WriteLine(Spooky());

        Console.WriteLine("");
        Console.Write("Press Enter to Quit...");
        Console.ReadKey();
    }

    static public string Spooky()
    {
        string story = "They all agreed that it was a huge {0}, {1}, {2}, and {3}."
            + " I have cross-examined these men, one of them a hard-headed "
            + "{4}, one a {5}, and one a moorland {6}"
            + ", who all tell the same story of this "
            + "{7} {8}, exactly corresponding to the {9} of the legend.";

        string[] prompts = {
            "noun", "adjective", "adjective", "adjective",
            "occupation", "occupation", "occupation",
            "adjective", "noun", "noun" };

        return MadLib(story, prompts);
    }

    static string MadLib(string story, string[] prompts)
    {
        List<string> answers = new List<string>();
        foreach (string prompt in prompts)
        {
            answers.Add(Input(prompt));
        }
        return String.Format(story, answers.ToArray());
    }

    static string Input(string part)
    {
        Console.Write("Please enter a {0}: ", part);
        return Console.ReadLine();
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

You should create an object (Person) with the properties needed, like a name, something, another and print it using the ToString method or putting all the strings in a List or Array as you wish and use the String.Join

Gilberto
  • 77
  • 7