Ejemplo n.º 1
0
            public static WordSearch CreateNew(int width, int height, string[] words)
            {
                var ws = new WordSearch {
                    Width = width, Height = height, WordSearchLetters = new char[width, height]
                };
                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
                    {
                        ws.AddWord(hiddenWord);
                    }
                }

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