コード例 #1
0
        static void Main(string[] args)
        {
            var showCheats = false;

            Console.Write("Show cheat colors? (Y/N)");
            ConsoleKeyInfo key;

            do
            {
                key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.Y)
                {
                    showCheats = true;
                    break;
                }
                else if (key.Key == ConsoleKey.N)
                {
                    break;
                }
                else
                {
                    Console.Beep();
                }
            } while (true);
            Console.Clear();

            var listToAddItemsTo = new List <string> {
                "Justin", "kelly", "Whitney", "spencer", "andromeda", "aurora", "perry", "greenwood",
                "josephine", "abraham", "eugene", "renee", "bob", "foster", "elizabeth"
            };

            listToAddItemsTo.Add("Mary");

            WordSearch ws = null;

            try
            {
                ws = WordSearch.CreateNew(16, 16, listToAddItemsTo);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return;
            }

            for (var y = 0; y < ws.WordSearchLetters.GetLength(1); y++)
            {
                for (var x = 0; x < ws.WordSearchLetters.GetLength(0); x++)
                {
                    if (showCheats && ws.HasHiddenWord(x, y))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    Console.Write($"{ws.WordSearchLetters[x, y]} ");
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
コード例 #2
0
        public static WordSearch CreateNew(int width, int height, List <string> words)
        {
            words = words.OrderByDescending(word => word.Length).ToList();

            var ws = new WordSearch
            {
                Width             = width,
                Height            = height,
                WordSearchLetters = new char[width, height]
            };

            // add the words
            var maxAttempts = ws.Width * ws.Height * 10;

            foreach (var word in words)
            {
                var hiddenWord = new HiddenWord
                {
                    Word = word.ToUpper()
                };

                int attempts = 0;
                do
                {
                    hiddenWord.Direction = (WordDirection)_random.Next(3);

                    switch (hiddenWord.Direction)
                    {
                    case WordDirection.HORIZONTAL:
                        hiddenWord.X = _random.Next(ws.Width - word.Length);
                        hiddenWord.Y = _random.Next(ws.Height);
                        break;

                    case WordDirection.VERTICAL:
                        hiddenWord.X = _random.Next(ws.Width);
                        hiddenWord.Y = _random.Next(ws.Height - word.Length);
                        break;

                    case WordDirection.DIAGONAL:
                        hiddenWord.X = _random.Next(ws.Width - word.Length);
                        hiddenWord.Y = _random.Next(ws.Height - word.Length);
                        break;
                    }
                    attempts++;
                } while (!hiddenWord.FitsInWordSearch(ws) && attempts < maxAttempts);

                if (attempts >= maxAttempts)
                {
                    throw new Exception("SORRY! This ain't going to work.");
                }
                else
                {
                    Console.WriteLine($"{word} - {attempts} attempt(s)");
                    ws.AddWord(hiddenWord);
                }
            }

            // fill the rest with random letters
            for (var y = 0; y < ws.WordSearchLetters.GetLength(1); y++)
            {
                for (var x = 0; x < ws.WordSearchLetters.GetLength(0); x++)
                {
                    if (ws.WordSearchLetters[x, y] == '\0')
                    {
                        ws.WordSearchLetters[x, y] = (char)('A' + _random.Next(0, 26));
                    }
                }
            }

            return(ws);
        }