// ------------------------------------------------------- // Using Directives // ------------------------------------------------------- using System.Collections.Generic; using System.Linq; using System.IO; using System; // ------------------------------------------------------- // WordSearch // ------------------------------------------------------- public class WordSearch { public static readonly int MAX_TRIES = 1000; public static string[] GetWordsFromFile(string fileName) { string[] lines = File.ReadAllLines(fileName); var linesProcessed = new List(); foreach(string line in lines) { string theLine = line.Trim().ToLower(); if(theLine != "") linesProcessed.Add(theLine); } return(linesProcessed.ToArray()); } public static string[] GetWords() { // ------------------------------------------------------- // We use a hard-coded list of animals for our words // but you can easily swap this out for a call to // 'GetWordsFromFile' if you'd like to use an different // list of words // ------------------------------------------------------- string[] animals = { "aardvark", "bison", "cat", "dog", "eagle", "fish", "gorilla", "horse", "ibex", "jackal", "kangaroo", "llama", "moose", "narwhal", "ocelot", "porcupine", "quail", "rooster", "snake", "tarsier", "urasa", "vole", "wombat", "yak", "zebra" }; return(animals); } public static void MakePuzzle(string[] words, int rows, int columns) { char[,] thePuzzle = new char[rows, columns]; Random r = new Random(); for(int i = 0; i < rows; i ++) for(int j = 0; j < rows; j ++) thePuzzle[i, j] = ' '; foreach(string s in words) { if(!s.All(Char.IsLetter)) throw new Exception("Not a valid word: " + s); // Now, try to place the word bool placed = false; int tries = 0; while(!placed) { int row = r.Next(rows); int column = r.Next(columns); int rowStep = 0; int columnStep = 0; switch(r.Next(3)) { case 0: rowStep = -1; break; case 1: rowStep = 0; break; case 2: rowStep = 1; break; } switch(r.Next(3)) { case 0: columnStep = -1; break; case 1: columnStep = 0; break; case 2: columnStep = 1; break; } var theCopy = (char[,])thePuzzle.Clone(); int count = 0; for(int i = 0; i < s.Length; i ++) { if(row < 0 || row >= rows || column < 0 || column >= columns) { break; } char current = theCopy[row, column]; if(current == ' ') theCopy[row, column] = s[i]; else if(current != s[i]) break; #if false Console.WriteLine( "placing s[" + i + "] = " + s[i] + " at " + row + "," + column ); #endif theCopy[row, column] = s[i]; row += rowStep; column += columnStep; count ++; } if(count == s.Length) { thePuzzle = theCopy; placed = true; } if(++tries > MAX_TRIES) throw new Exception("Exceeded: " + MAX_TRIES + " tries"); } } Console.WriteLine( "-------------------------\n" + "Puzzle - Before\n" + "-------------------------\n" ); for(int i = 0; i < rows; i ++) { for(int j = 0; j < columns; j ++) { char c = thePuzzle[i, j]; Console.Write(" " + (c == ' ' ? '.' : c) + " "); } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine( "-------------------------\n" + "Puzzle - After\n" + "-------------------------\n" ); string letters = "abcdefghijklmnopqrstuvwxyz"; for(int i = 0; i < rows; i ++) for(int j = 0; j < columns; j ++) if(thePuzzle[i, j] == ' ') thePuzzle[i, j] = letters[r.Next(letters.Length)]; for(int i = 0; i < rows; i ++) { for(int j = 0; j < columns; j ++) Console.Write(" " + thePuzzle[i, j] + " "); Console.WriteLine(); } } public static void Main() { try { MakePuzzle(GetWords(), 15, 15); } catch(Exception e) { Console.WriteLine(e.Message); } Console.ReadLine(); } }