Exemplo n.º 1
0
 /// <summary>
 /// Creates a new <see cref="WordgridGrid"/> based on an exisiting <paramref name="grid"/> but with a new position
 /// </summary>
 /// <param name="grid">The existing grid that is the base for the new <see cref="WordgridGrid"/></param>
 /// <param name="startX">The new start position on the X-Axis</param>
 /// <param name="startY">The new start position on the Y-Axis</param>
 /// <remarks>The <paramref name="startX"/> and <paramref name="startY"/> positions must be reachable from the exisiting <paramref name="grid"/>, because the new <see cref="Path"/> is computed based on the existing <see cref="Path"/> of the given <paramref name="grid"/>. It is not checked if this requirement is met!</remarks>
 public WordgridGrid(WordgridGrid grid, int startX, int startY)
 {
     X = startX;
     Y = startY;
     WordMatrix = new WordgridCell[4, 4];
     for (var i = 0; i < 4; i++)
     {
         for (var j = 0; j < 4; j++)
         {
             var cell = grid.WordMatrix[i, j];
             WordMatrix[i, j] = new WordgridCell(cell);
         }
     }
     Path = grid.Path + GetCurrentChar();
     GetCurrentCell().Visited = true;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Gets possible word in the Word Matrix of this instace
        /// </summary>
        /// <returns>Words found in the grid that are also in the dictionary</returns>
        public IEnumerable<string> GetWords()
        {
            //Stack that will contain our grids that we have to check
            Grids = new Queue<WordgridGrid>();

            //Create the starting positions - each cell might be the start of the word and therefore we need to create 16 starting positions
            for (var i = 0; i < 4; i++)
            {
                for (var j = 0; j < 4; j++)
                {
                    var grid = new WordgridGrid(WordMatrix, i, j);
                    Grids.Enqueue(grid);
                }
            }

            //Save the already found words, so that words aren't displayed multiple times if they can be created using different paths
            var foundWords = new HashSet<string>();

            //Iterate as long as we have grids in our stack
            while (Grids.Count > 0)
            {
                //Get the first grid
                var grid = Grids.Dequeue();

                var word = grid.Path;
                if (word.Length > 2)
                {
                    //Check if the word is not already found and not in the dictionary
                    if (!foundWords.Contains(word) && Dictionary.IsWord(word))
                    {
                        yield return word;
                        foundWords.Add(word);
                    }
                }

                //Add possible grids to our stack
                foreach (var nextGrid in grid.GetNextGrids())
                {
                    //Add the grid only if the path of the grid is in the dictionary tree. Otherwise, the word and all words that start with this word are not on the dictionary and we can ignore them.
                    if (Dictionary.ContainsWordBase(nextGrid.Path))
                    {
                        Grids.Enqueue(nextGrid);
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets a <see cref="WordgridGrid"/> with the given parameters. If the parameters are illegal (<paramref name="x"/> or <paramref name="y"/> are out of the grid, <c>null</c> is returned)
        /// </summary>
        /// <param name="x">The new X-Position</param>
        /// <param name="y">The new Y-Position</param>
        /// <returns>The new <see cref="WordgridGrid"/> based on the given parameters</returns>
        private WordgridGrid GetNextGrid(int x, int y)
        {
            if (x < 0 || x > 3 || y < 0 || y > 3)
            {
                //Out of bounds
                return null;
            }

            if (WordMatrix[x, y].Visited)
            {
                //Already visited
                return null;
            }

            var grid = new WordgridGrid(this, x, y);
            return grid;
        }