SharpLibs/MadLib/StoryGenerator.cs
2025-11-05 10:38:32 -06:00

58 lines
No EOL
1.5 KiB
C#

namespace MadLib;
public class StoryGenerator(StoryGenerator.Categories category)
{
public enum Categories
{
Adventure,
Science,
Fantasy,
Horror,
Mystery
}
private StoryTemplate _template;
public static void DisplayCategories()
{
foreach (var category in Enum.GetValues<Categories>()) Console.WriteLine($"{(int)category + 1}) {category}");
Console.WriteLine("\n");
}
public void Generate()
{
var storyWords = GetWords();
var story = _template.Text;
foreach (var placeholder in _template.Placeholders)
story = story.Replace($"[{placeholder}]", storyWords[placeholder]);
DisplayStory(story);
}
private Dictionary<string, string> GetWords()
{
if (!StoryTemplates.Templates.TryGetValue(category, out var template))
throw new Exception("Could not find Story Template.");
_template = template;
var words = new Dictionary<string, string>();
foreach (var placeholder in template.Placeholders)
{
Console.WriteLine($"Enter a {placeholder}: ");
var input = Console.ReadLine() ?? string.Empty;
words.Add(placeholder, input);
}
return words;
}
private void DisplayStory(string story)
{
Console.WriteLine("====================================================\n");
Console.WriteLine(story);
Console.WriteLine("\n====================================================");
}
}