samedi 20 janvier 2018

Randomly building ASCII art in C# console

I'm trying to do something cool for prom where I take an ASCII text art design, snip each line to a list(string), and then make each char in that string saved to a 2D char array. Once the 2D char array has been built, the program should randomly choose between the column and row to move the console cursor and type whatever belongs to that position.

However when I run it, it would look as though the random selector stops selecting halfway through or it has gone through the entire char array. I've even switched out the ASCII template art to something else and still encountered the same issue. If I'm thinking right then the program should properly go through every single coordinate once in a random manner, and then accessing the value of the 2D array respectively.

Can anyone tell where the issue is or why it does't finish completely?

    static void Main(string[] args)
    {
        AskOut();
        Console.Read();
    }

    private static void AskOut()
    {
        /*
         * Takes stored word art
         * Breaks down each line into an individual char
         * Randomly builds a 2d array of chars
        */

        Console.WriteLine();
        Console.ForegroundColor = ConsoleColor.Red;

        //Initilize ASCII wordart in a list
        List<string> textArt = new List<string>();
        textArt.Add("db   d8b   db d888888b db      db        db    db  .d88b.  db    db ");
        textArt.Add("88   I8I   88   `88'   88      88        `8b  d8' .8P  Y8. 88    88 ");
        textArt.Add("88   I8I   88    88    88      88         `8bd8'  88    88 88    88 ");
        textArt.Add("Y8   I8I   88    88    88      88           88    88    88 88    88 ");
        textArt.Add("`8b d8'8b d8'   .88.   88booo. 88booo.      88    `8b  d8' 88b  d88 ");
        textArt.Add(" `8b8' `8d8'  Y888888P Y88888P Y88888P      YP     `Y88P'  ~Y8888P' ");
        textArt.Add(" ");
        textArt.Add(" ");
        textArt.Add(" d888b   .d88b.    d888888b  .d88b.    d8888b. d8888b.  .d88b.  .88b  d88.  ");
        textArt.Add("88' Y8b .8P  Y8.      88    .8P  Y8.   88  `8D 88  `8D .8P  Y8. 88'YbdP`88  ");
        textArt.Add("88      88    88      88    88    88   88oodD' 88oobY' 88    88 88  88  88  ");
        textArt.Add("88  ooo 88    88      88    88    88   88      88`8b   88    88 88  88  88  ");
        textArt.Add("88. ~8~ `8b  d8'      88    `8b  d8'   88      88 `88. `8b  d8' 88  88  88  ");
        textArt.Add(" Y888P   `Y88P'       YP     `Y88P'    88      88   YD  `Y88P'  YP  YP  YP  ");
        textArt.Add(" ");
        textArt.Add(" ");
        textArt.Add("db   d8b   db d888888b d888888b db   db   .88b  d88. d88888b .d888b.  ");
        textArt.Add("88   I8I   88   `88'      88    88   88   88'YbdP`88 88'     VP  `8D  ");
        textArt.Add("88   I8I   88    88       88    88ooo88   88  88  88 88ooooo    odD'  ");
        textArt.Add("Y8   I8I   88    88       88    88~~~88   88  88  88 88        8P'    ");
        textArt.Add("`8b d8'8b d8'   .88.      88    88   88   88  88  88 88.       oo     ");
        textArt.Add(" `8b8' `8d8'  Y888888P    YP    YP   YP   YP  YP  YP Y88888P   VP     ");

        //Test the word art
        foreach (string line in textArt)
        {
            Console.WriteLine(line);
        }
        Thread.Sleep(1000);
        Console.Clear();

        //Store each letter in list into 2d array
        char[,] letters = new char[textArt.Count, 80];

        for (int x = 0; x < textArt.Count(); x++)
        {
            char[] let = textArt.ElementAt(x).ToCharArray();

            for (int y = 0; y < let.Count(); y++)
            {
                letters[x, y] = (char)let.GetValue(y);
            }
        }

        //Use the 2d array to randomly print out the hidden message
        Random r = new Random();

        foreach (char character in letters)
        {
            int row = r.Next(letters.GetLength(0));
            int column = r.Next(1, letters.GetLength(1));

            if (character.Equals(' ') == false)
            {

                Console.SetCursorPosition(column, row + 1);
                Console.Write(letters[row, column]);
                Thread.Sleep(5);
            }
        }
    }
}

}




Aucun commentaire:

Enregistrer un commentaire