在C#控制台中随机构建ASCII艺术

时间:2018-01-21 00:40:09

标签: c# arrays random console char

我正在尝试为舞会做一些很酷的事情,我采用ASCII文本艺术设计,将每一行剪切为一个列表(字符串),然后将该字符串中的每个字符保存到2D字符数组中。一旦构建了2D char数组,程序应该在列和行之间随机选择以移动控制台光标并键入属于该位置的任何内容。

然而,当我运行它时,看起来好像随机选择器在中途停止选择或者它已遍历整个char数组。我甚至将ASCII模板艺术转换为其他东西,但仍然遇到了同样的问题。 如果我正在思考,那么程序应该以随机方式正确地遍历每个单独的坐标,然后分别访问2D数组的值。

任何人都可以告诉问题在哪里或为什么它没有完全完成?

    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);
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:-1)

试试这个

using System;
using System.Collections.Generic;
using System.Threading;

namespace ConsoleArt
{
    class Program
    {
        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,200 ];
            char[] let1 = textArt[0].ToCharArray();
            //Console.WriteLine("Länge der Zeile {0} = {1}",   textArt[0],let1.Length);
            //Console.WriteLine("");

            for (int y = 0; y < textArt.Count; y++)
            {
                //Console.WriteLine("Get Line y"+y);
                char[] let = textArt[y].ToCharArray();

                for (int x = 0; x < let.Length; x++)
                {
                    //Console.WriteLine("Get X{0},Y{1},{2}",x,y,let.GetValue(x));
                    letters[y, x] = (char)let.GetValue(x);
                }
            }

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

            foreach(char character in letters)
            {
                int row = r.Next(textArt.Count);
                int column = r.Next(0, textArt[row].Length);

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

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

你换了x(列)和y(行)。

祝你好运,

弗兰西