public void CheckWord(int StartColumn, int StartRow, int EndColumn, int EndRow) { var FoundWords = from w in AssembledWords where (w.x1 == StartColumn && w.y1 == StartRow && w.x2 == EndColumn && w.y2 == EndRow) || (w.x2 == StartColumn && w.y2 == StartRow && w.x1 == EndColumn && w.y1 == EndRow) select w; foreach (Word FoundWord in FoundWords) { FoundWord.Found = true; int i = 0; Letter foundLetter = new Letter(); foreach (char letter in FoundWord.Text.Replace(" ", String.Empty)) { switch (FoundWord.Direction) { case Direction.Right: foundLetter = (from l in Letters where (l.x == FoundWord.x1 + i) && (l.y == FoundWord.y1) select l).First(); break; case Direction.RightDown: foundLetter = (from l in Letters where (l.x == FoundWord.x1 + i) && (l.y == FoundWord.y1 + i) select l).First(); break; case Direction.Down: foundLetter = (from l in Letters where (l.x == FoundWord.x1) && (l.y == FoundWord.y1 + i) select l).First(); break; case Direction.LeftDown: foundLetter = (from l in Letters where (l.x == FoundWord.x1 - i) && (l.y == FoundWord.y1 + i) select l).First(); break; case Direction.Left: foundLetter = (from l in Letters where (l.x == FoundWord.x1 - i) && (l.y == FoundWord.y1) select l).First(); break; case Direction.LeftUp: foundLetter = (from l in Letters where (l.x == FoundWord.x1 - i) && (l.y == FoundWord.y1 - i) select l).First(); break; case Direction.Up: foundLetter = (from l in Letters where (l.x == FoundWord.x1) && (l.y == FoundWord.y1 - i) select l).First(); break; case Direction.RightUp: foundLetter = (from l in Letters where (l.x == FoundWord.x1 + i) && (l.y == FoundWord.y1 - i) select l).First(); break; } foundLetter.Found = true; i++; } } }
public LetterGrid (List<Word> words) { Letters = new List<Letter>(); List<Path> Paths = new List<Path>(); AssembledWords = new List<Word>(); rnd = new Random(); foreach (var word in words) { word.Text = word.Text.ToUpper(); } for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { if (i == 0 && j == 0) { //TopLeft Path NewPath; NewPath = new Path(); NewPath.Direction = Direction.Right; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.RightDown; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.Down; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); } else if (i == 0 && j == (Width - 1)) { //TopRight Path NewPath; NewPath = new Path(); NewPath.Direction = Direction.Left; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.LeftDown; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.Down; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); } else if (i == (Height - 1) && j == 0) { //BottomLeft Path NewPath; NewPath = new Path(); NewPath.Direction = Direction.Up; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.RightUp; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.Right; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); } else if (i == (Height - 1) && j == (Width - 1)) { //BottomRight Path NewPath; NewPath = new Path(); NewPath.Direction = Direction.Left; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.LeftUp; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.Up; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); } else if (j == 0) { //Right*3 Path NewPath; NewPath = new Path(); NewPath.Direction = Direction.RightUp; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.Right; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.RightDown; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); } else if (i == 0) { //Down*3 Path NewPath; NewPath = new Path(); NewPath.Direction = Direction.LeftDown; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.Down; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.RightDown; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); } else if (i == (Height - 1)) { //Up*3 Path NewPath; NewPath = new Path(); NewPath.Direction = Direction.LeftUp; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.Up; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.RightUp; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); } else if (j == (Width - 1)) { //Left*3 Path NewPath; NewPath = new Path(); NewPath.Direction = Direction.LeftUp; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.Left; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); NewPath = new Path(); NewPath.Direction = Direction.LeftDown; NewPath.x = j; NewPath.y = i; Paths.Add(NewPath); } } } //Randomize Paths List<Path> RandomPaths = new List<Path>(); while (Paths.Count > 0) { int rndPath = rnd.Next(Paths.Count); RandomPaths.Add(Paths[rndPath]); Paths.RemoveAt(rndPath); } Paths = RandomPaths; //Fill in words AssembledWords.Clear(); foreach (Path path in Paths) { int PathLength = GetMask (path).Length; String Mask = GetMask (path); var MatchingLocations = from q in words where (PathLength - q.Text.Replace(" ", String.Empty).Length + 1 > 3) where ( from a in AssembledWords where a.Text == q.Text select a ).Count () == 0 from m in Enumerable.Range (0, PathLength - q.Text.Replace(" ", String.Empty).Length + 1) where ( from s in Enumerable.Range (m, q.Text.Replace(" ", String.Empty).Length) where (q.Text.Replace(" ", String.Empty).Substring (s - m, 1) == Mask.Substring (s, 1)) || (Mask.Substring (s, 1) == ".") select s ).Count () == q.Text.Replace(" ", String.Empty).Length select new { MatchedWord = q, //Text = q.Text, Offset = m }; if (MatchingLocations.Count () > 0) { int rndWord = rnd.Next (MatchingLocations.Count ()); Word AssembledWord = MatchingLocations.ElementAt (rndWord).MatchedWord; AssembledWord.Direction = path.Direction; AssembleWord (path.x, path.y, path.Direction, MatchingLocations.ElementAt (rndWord).Offset, AssembledWord); AssembledWords.Add (AssembledWord); } } String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { if (!Letters.Exists(l => l.x == j && l.y == i)) { Letter letter = new Letter(); letter.x = j; letter.y = i; letter.Text = alphabet.Substring(rnd.Next(0, 26), 1); Letters.Add(letter); } } } }
private void AssembleWord(int x, int y, Direction dir, int offset, Word NewWord) { switch (dir) { case Direction.Right: x = x + offset; break; case Direction.RightDown: x = x + offset; y = y + offset; break; case Direction.Down: y = y + offset; break; case Direction.LeftDown: x = x - offset; y = y + offset; break; case Direction.Left: x = x - offset; break; case Direction.LeftUp: x = x - offset; y = y - offset; break; case Direction.Up: y = y - offset; break; case Direction.RightUp: x = x + offset; y = y - offset; break; } NewWord.x1 = x; NewWord.y1 = y; foreach (char c in NewWord.Text.Replace(" ", String.Empty)) { if (Letters.Exists(l => l.x == x && l.y == y)) { Letter letter = Letters.Where(l => l.x == x && l.y == y).First(); letter.Text = c.ToString(); } else { Letter letter = new Letter(); letter.x = x; letter.y = y; letter.Text = c.ToString(); Letters.Add(letter); } NewWord.x2 = x; NewWord.y2 = y; switch (dir) { case Direction.Right: x++; break; case Direction.RightDown: x++; y++; break; case Direction.Down: y++; break; case Direction.LeftDown: x--; y++; break; case Direction.Left: x--; break; case Direction.LeftUp: x--; y--; break; case Direction.Up: y--; break; case Direction.RightUp: x++; y--; break; } } }