-2

I am new to C# so this might be really dumb and I am doing it all wrong but... I am trying to write a text adventure and I need to be able to slow type each letter out and wrap each word to 80 chars, so it looks like a smooth paragraph when there is a chunk of text.

public static void Wrap()
{
var words = text.Split(' ');
var lines = words.Skip(1).Aggregate(words.Take(1).ToList(), (l, w) =>
{
    if (l.Last().Length + w.Length >= 80)
        l.Add(w);
    else
        l[l.Count - 1] += " " + w;
    return l;
});
}

To do this I just change the var text to whatever I want to write then call on Wrap()

var text = "text"
Wrap();

That works, and is all good - maybe inefficient, but I have a separate thing that makes it type out each char with a 40 millisecond delay.

        public static void MW(string str)
        {
            foreach (char c in str)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(c);
                Thread.Sleep(40);
            }
        }

I just do

MW("Text");

To do this.

So overall, I need it to slow type, wrap the words neatly and change the colour. Is this possible. Sorry if this is inefficient or wrong, I don't know much about this language yet.

I tried to combine them on my own, but I don't have enough experience and could not figure it out.

1 Answers1

1

Check this out:

Show("I am new to C# so this might be really dumb and I am doing it all wrong but... I am trying to write a text adventure and I need to be able to slow type each letter out and wrap each word to 80 chars, so it looks like a smooth paragraph when there is a chunk of text.", 80);

static void Show(string text, int wrap)
{
    var words = text.Split(" ").Select(x => x + " ");
    var col = 0;
    foreach (var word in words)
    {
        if (col + word.Length >= wrap)
        {
            Console.WriteLine();
            col = 0;
        }
        foreach (var c in word)
        {
            Console.Write(c);
            Thread.Sleep(40);
            col++;
        }
    }
}

I have changed the code to support low justification.

Mohammad Mirmostafa
  • 1,720
  • 2
  • 16
  • 32