// Helper function to resolve points cleaner. // This will also work for negative indexes private static Point GetWordCoord(Word word, Point startPoint, int index, int parallelOffset = 0) { startPoint.X += word.GetDirection() == Direction.Vertical ? parallelOffset : 0; startPoint.Y += word.GetDirection() == Direction.Horizontal ? parallelOffset : 0; int x = startPoint.X + (word.GetDirection() == Direction.Horizontal ? index : 0); int y = startPoint.Y + (word.GetDirection() == Direction.Vertical ? index : 0); return(new Point(x, y)); }
// Will return -1 if the word cannot be placed. private int CountIntersections(Word word, Point start) { int intersections = 0; if (!CanWordFit(word, start)) { return(-1); } for (int i = 0; i < word.GetLength(); ++i) { Point p = GetWordCoord(word, start, i); if (blocks[p.X, p.Y] != null) { if (blocks[p.X, p.Y].GetAnswer() == word.GetCorrectWord()[i]) { intersections++; } else if (!blocks[p.X, p.Y].CanOverwrite(word.GetDirection())) { return(-1); } } } return(intersections); }
// Excpects a word that can be placed private void PlaceWord(Word word, Point start) { Point before = GetWordCoord(word, start, -1); Point after = GetWordCoord(word, start, word.GetLength()); // Its ok to overwrite the old block here since black blocks do not get stored anywhere else than the block[,] array blocks[before.X, before.Y] = new BlackBlock(BlockOverwrite.None); blocks[after.X, after.Y] = new BlackBlock(BlockOverwrite.None); for (int i = 0; i < word.GetLength(); i++) { Point p = GetWordCoord(word, start, i); if (blocks[p.X, p.Y] == null || blocks[p.X, p.Y] is BlackBlock) { blocks[p.X, p.Y] = new CharacterBlock(word.GetCorrectWord()[i]); } word.SetSharedBlock(blocks[p.X, p.Y] as CharacterBlock, i); ProhibitOverwrite(GetWordCoord(word, start, i, 1), word.GetDirection()); ProhibitOverwrite(GetWordCoord(word, start, i, -1), word.GetDirection()); } words.Add(word); }