public Balda(ILettersSet lettSer, int fieldSize = 5) { _commonTree = new TrieTree(lettSer); _invertedPrefixTree = new TrieTree(lettSer); _lettSet = lettSer; _fieldSize = fieldSize; _grid = GetEmptyGrid(fieldSize); _visited = new bool[fieldSize, fieldSize]; }
private bool FindWords( char[,] grid, int size, TrieTree tree, int startX, int startY, ICollection <string> coll) { return(FindWords(grid, size, tree, startX, startY, startX, startY, coll)); }
/// <summary> /// Adds a given word into the tree starting at given index /// </summary> /// <param name="word"></param> /// <param name="ind">Start index</param> /// <returns>true if thw word was added successfuly</returns> private bool Add(string word, int ind) { if (word.Length == ind) { bool res = word == null; Word = word; return(res); } int indLet = _lettSet.GetIndex(word[ind]); if (_next[indLet] == null) { _next[indLet] = new TrieTree(_lettSet); } return(_next[indLet].Add(word, ind + 1)); }
/// <summary> /// Gets all possible words on current step /// </summary> /// <param name="grid">The game field</param> /// <param name="size">The size of the field</param> /// <param name="currStep">The current step</param> /// <param name="maxStep">The max step</param> /// <param name="words"></param> private void GetPossiblewWords_Rec( char[,] grid, int size, int currStep, int maxStep, ISet <string>[] words) { var goodGrids = new List <char[, ]>(); for (int i = 0; i < _fieldSize; ++i) { for (int j = 0; j < _fieldSize; ++j) { if (!IsAdjacent(grid, size, i, j)) { continue; } foreach (char lett in _lettSet) { grid[i, j] = lett; TrieTree node = _invertedPrefixTree.GetNodeByLett(lett); if (node == null) { continue; } _visited[i, j] = true; if (FindWords(grid, size, node, i, j, words[currStep - 1])) { char[,] copy = (char[, ])grid.Clone(); goodGrids.Add(copy); } _visited[i, j] = false; grid[i, j] = EMPTY; } } } if (currStep + 1 > maxStep) { return; } foreach (var gr in goodGrids) { GetPossiblewWords_Rec(gr, size, currStep + 1, maxStep, words); } }
/// <summary> /// Finds possible words /// </summary> /// <param name="grid">The game field</param> /// <param name="size">The size of the field</param> /// <param name="tree">The subtree from which start is doing</param> /// <param name="x">Current row index</param> /// <param name="y">Current column index</param> /// <param name="startX">The row index of placed</param> /// <param name="startY">The column index of placed</param> /// <param name="words"></param> /// <returns>True if any word was found</returns> private bool FindWords( char[,] grid, int size, TrieTree tree, int x, int y, int startX, int startY, ICollection <string> words) { bool wasAdded = false; if (tree.Word != null) { if (_commonTree.Find(tree.Word)) { words.Add(tree.Word); wasAdded = true; } if (_invertedPrefixTree.Find(tree.Word)) { TrieTree t = _commonTree; for (int i = tree.Word.Length - 1; i >= 0; --i) { t = t.GetNodeByLett(tree.Word[i]); if (t == null) { break; } } if (t != null) { wasAdded = wasAdded || FindWords(grid, size, t, startX, startY, startX, startY, words); } } } if (x - 1 >= 0 && grid[x - 1, y] != EMPTY && !_visited[x - 1, y]) { TrieTree node = tree.GetNodeByLett(grid[x - 1, y]); if (node != null) { _visited[x - 1, y] = true; wasAdded = wasAdded || FindWords(grid, size, node, x - 1, y, startX, startY, words); _visited[x - 1, y] = false; } } if (x + 1 < size && grid[x + 1, y] != EMPTY && !_visited[x + 1, y]) { TrieTree node = tree.GetNodeByLett(grid[x + 1, y]); if (node != null) { _visited[x + 1, y] = true; wasAdded = wasAdded || FindWords(grid, size, node, x + 1, y, startX, startY, words); _visited[x + 1, y] = false; } } if (y - 1 >= 0 && grid[x, y - 1] != EMPTY && !_visited[x, y - 1]) { TrieTree node = tree.GetNodeByLett(grid[x, y - 1]); if (node != null) { _visited[x, y - 1] = true; wasAdded = wasAdded || FindWords(grid, size, node, x, y - 1, startX, startY, words); _visited[x, y - 1] = false; } } if (y + 1 < size && grid[x, y + 1] != EMPTY && !_visited[x, y + 1]) { TrieTree node = tree.GetNodeByLett(grid[x, y + 1]); if (node != null) { _visited[x, y + 1] = true; wasAdded = wasAdded || FindWords(grid, size, node, x, y + 1, startX, startY, words); _visited[x, y + 1] = false; } } return(wasAdded); }