mercredi 16 décembre 2015

Get a random value and write it out for set amount of times

So I am currently trying to make a console application for experience and funzees, however, I have with the generation of different words encountered a issue that I can't wrap my head around. I have posted my code below, but first let me explain what I am going for. I have a list of random signs, which does exactly what it should. In some of these lines I want a word that I have pre-set somewhere (and if theres a better way of doing it than I have, please let me know), and while I did get the general idea up there, I can either

1) Pretty much loop the whole list, and have different chunks with the words and signs, which doesn't at all work.

2) Only get that same word over and over again.

So the code I've currently got is:

Random random = new Random();

            int firstRandom = random.Next(1, 10);
            int secondRandom = random.Next(1, 10);
            int thirdRandom = random.Next(1, 10);
            int fourthRandom = random.Next(1, 10);

            // Sets the random sign combinations etc.
            var chars = ",.-;:_!#¤%&/()=?";
            var output = Enumerable.Range(1, 30).Select(i => chars.Select(c => chars[random.Next(chars.Length)]).ToArray());

            int randomWord = random.Next(1, 5);

            // Predefines variables
            string wordDisplay = "";
            string inFrontOfWord = "";
            string behindWord = "";


            // Checks what word to pick, based from the random statement above
            if (randomWord == 1)
            {
                wordDisplay = "SLAY";

                inFrontOfWord = "#%/&-.¤";
                behindWord = "$%!#         ";
            }

            if (randomWord == 2)
            {
                wordDisplay = "VILLAIN";

                inFrontOfWord = "34-.";
                behindWord = "00..     ";
            }

            if (randomWord == 3)
            {
                wordDisplay = "FACILITY";

                inFrontOfWord = ",,.-";
                behindWord = "09=!    ";
            }

            if (randomWord == 4)
            {
                wordDisplay = "DOG";

                inFrontOfWord = "=)435-";
                behindWord = "#¤%--.     ";
            }

            // Predefines variables
            string display = "";
            int count = 0;

            // Handles the printing out lines
            foreach (var s in output)
            {

                display = new string(s);
                var line = "";

                if (count == 4 || count == 9 || count == 14 || count == 19)
                {
                    line = "4X" + 3 + firstRandom + secondRandom + "   " + inFrontOfWord + wordDisplay + behindWord + "4X" + 5 + thirdRandom + fourthRandom + "   " + inFrontOfWord + wordDisplay + behindWord;

                }

                else
                {
                    line = "4X" + 3 + firstRandom + secondRandom + "   " + display + "    " + "4X" + 5 + thirdRandom + fourthRandom + "   " + display;

                }

                Console.WriteLine(line);

                count++;

            }

The code I currently have does NOT include anything that would loop the random statement, what I have tried however is adding :

int countForWord = 0;

while (countForWord != 4) {

int randomStmt = random.Next(1,5);

// if statements goes here

countForWord++;
}




Aucun commentaire:

Enregistrer un commentaire