public static void DrawThePuzzle(CrossWord cw, bool showCheats)
        {
            if (cw == null)
            {
                return;
            }
            var centerFormat = new StringFormat();

            centerFormat.LineAlignment = StringAlignment.Center;
            centerFormat.Alignment     = StringAlignment.Center;

            var font     = new Font(FontFamily.GenericSansSerif, SquareSize, FontStyle.Regular, GraphicsUnit.Pixel);
            var tinyFont = new Font(FontFamily.GenericSansSerif, SquareSize / 2, FontStyle.Regular, GraphicsUnit.Pixel);
            int w        = cw.Width * SquareSize;
            int h        = cw.Height * SquareSize;

            using (Bitmap image = new Bitmap(w, h))
                using (Graphics g = Graphics.FromImage(image))
                {
                    g.FillRectangle(Brushes.Black, 0, 0, w, h);


                    for (var y = 0; y < cw.WordSearchLetters.GetLength(1); y++)
                    {
                        for (var x = 0; x < cw.WordSearchLetters.GetLength(0); x++)
                        {
                            var hasWord = cw.HasWord(x, y);
                            if (hasWord)
                            {
                                var sx   = x * SquareSize;
                                var sy   = y * SquareSize;
                                var word = cw.GetWordIfFirstIndex(x, y);

                                g.FillRectangle(Brushes.White, sx, sy, SquareSize, SquareSize);
                                g.DrawRectangle(Pens.Black, sx, sy, SquareSize, SquareSize);

                                if (showCheats)
                                {
                                    g.DrawString($"{cw.WordSearchLetters[x, y]}", font, Brushes.Red, sx + (SquareSize / 2), sy + (SquareSize / 2), centerFormat);
                                }
                                else if (word != null)
                                {
                                    g.DrawString($"{word.Number}", tinyFont, Brushes.Black, sx + 1, sy + 1, StringFormat.GenericTypographic);
                                }
                            }
                        }
                        Console.WriteLine();
                    }

                    image.Save($"mycrossword.png", ImageFormat.Png);

                    var process = new ProcessStartInfo()
                    {
                        FileName  = "mspaint.exe",
                        Arguments = $"mycrossword.png"
                    };
                    System.Diagnostics.Process.Start(process);
                }
        }
示例#2
0
        private static void Main(string[] args)
        {
            var showCheats = false;

            Console.Write("Show cheats? (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 wordList = new Dictionary <string, string> {
                { "elephant", "an animal that always carries its trunk." },
                { "zip", "This is the sound that a zipper makes." },
                { "loner", "Someone that likes to be alone." },
                { "andromeda", "The sleepy, blonde kdrama lover." },
                { "banana", "What fruit minions like to eat." },
                { "spencer", "Awesome history teacher... or maybe not so awesome." },
                { "aurora", "The loudest and most talkative chick around." },
                { "perry", "A energetic young punk that torments his sisters." },
                { "cinderella", "A fairy tale princess that was half barefoot." },
                { "mickeymouse", "Perry used to sound like this cartoon character when he was small." },
                { "pineapple", "There's one in every episode." },
                { "greg", "The ____ Gutfeld Show is a silly news show dad used to watch." }
            };

            CrossWord cw = null;

            for (int tryIdx = 0; tryIdx < 100; tryIdx++)
            {
                cw = CrossWord.CreateNew(15, 15, wordList);
                if (cw != null)
                {
                    break;
                }
            }

            if (cw == null)
            {
                Console.WriteLine("No luck making the crossword!");
            }
            else
            {
                for (var y = 0; y < cw.WordSearchLetters.GetLength(1); y++)
                {
                    for (var x = 0; x < cw.WordSearchLetters.GetLength(0); x++)
                    {
                        var hasWord = cw.HasWord(x, y);
                        if (hasWord)
                        {
                            var word = cw.GetWordIfFirstIndex(x, y);
                            Console.BackgroundColor = ConsoleColor.Black;
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            if (showCheats)
                            {
                                Console.Write($"{cw.WordSearchLetters[x, y]} ");
                            }
                            else
                            {
                                if (word != null)
                                {
                                    Console.ForegroundColor = (word.Direction == WordDirection.VERTICAL) ? ConsoleColor.Green : ConsoleColor.Cyan;
                                    Console.Write(word.Number.ToString().PadRight(2));
                                }
                                else
                                {
                                    Console.Write($"  ");
                                }
                            }
                        }
                        else
                        {
                            Console.BackgroundColor = ConsoleColor.White;
                            Console.Write($"  ");
                            Console.BackgroundColor = ConsoleColor.Black;
                            Console.Write($"");
                        }
                    }
                    Console.WriteLine();
                }

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("------------Down------------");
                foreach (var word in cw.Words.Where(x => x.Direction == WordDirection.VERTICAL).OrderBy(x => x.Number))
                {
                    Console.WriteLine($"{word.Number} - {word.Clue}");
                }
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("-----------Across-----------");
                foreach (var word in cw.Words.Where(x => x.Direction == WordDirection.HORIZONTAL).OrderBy(x => x.Number))
                {
                    Console.WriteLine($"{word.Number} - {word.Clue}");
                }
            }
            CrosswordRenderer.DrawThePuzzle(cw, showCheats);
            Console.ReadKey();
        }