Exemplo n.º 1
0
        public Dictionary <string, bool> ResolveWords(PrefixTree dictionary, DieFace[] currentBoard)
        {
            int[,] nodes = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0xA, 0xB, 0xC }, { 0xD, 0xE, 0xF, 0x10 } };
            var letters = new char[4, 4];
            int x       = 0;
            int y       = 0;

            foreach (var dieFace in currentBoard)
            {
                letters[x, y++] = dieFace.FaceCharacter[0];
                if (y == 4)
                {
                    y = 0;
                    x++;
                }
            }
            wordList = new Dictionary <string, bool>();
            dict     = dictionary;
            graph    = BuildGraph <char>(nodes, letters);

            foreach (var t in nodes)
            {
                AdjacencyNode node = graph.map[t];
                node.Visted = 1;
                Debug.Assert(index == 0);
                word[index] = node.NodeContent;
                BuildCombinations(node, word);
                node.Visted     = 0;
                word[index + 1] = (char)0;
            }

            return(wordList);
        }
Exemplo n.º 2
0
        private void BuildCombinations(AdjacencyNode node, char[] runningWord)
        {
            var    list = node.Adjacency;
            string w;

            foreach (int nn in list)
            {
                AdjacencyNode al = graph.map[nn];

                if (al.Visted == 1)
                {
                    continue;
                }

                if (!dict.PrefixExist(runningWord))
                {
                    return;
                }

                ++index;
                runningWord[index] = al.NodeContent;
                if (dict.Contains(runningWord))
                {
                    if (GameDice.QInSet == true)
                    {
                        // buffer is len=20, longest possible word is 17.
                        // so i will iterate until 18, and ii 19.  No overflow
                        for (int i = 0, ii = 0; i < tmp.Length - 1; i++, ii++)
                        {
                            tmp[ii] = runningWord[i];

                            // if there is a 'Q', append a 'U'
                            if (runningWord[i] == 'Q')
                            {
                                ii++;
                                tmp[ii] = 'U';
                            }
                        }
                        w = new string(tmp);
                    }
                    else
                    {
                        w = new string(runningWord);
                    }

                    if (wordList.ContainsKey(w) == false)
                    {
                        wordList.Add(w, true);
                    }
                }

                al.Visted = 1;
                BuildCombinations(al, runningWord);
                runningWord[index] = (char)0;
                --index;
                al.Visted = 0;
            }
        }
Exemplo n.º 3
0
        private AdjacencyMap BuildGraph <T>(int[,] array, char[,] arrayOfContent)
        {
            #region NEIGHBORS_ARRAY
            // neighbors to each node in a 4x4 matrix moving clockwise
            int[,] neighbors =
            {
                { 01, 11, 10, -01, -01, -01,      -01, -01 },        // 0,0
                { 02, 12, 11,  10,  00, -01,      -01, -01 },        // 0,1
                { 03, 13, 12,  11,  01, -01, -01 - 01, -01 },        // 0,2
                { 13, 12, 02, -01, -01, -01,      -01, -01 },        // 0,3
                { 11, 21, 20,  00,  01, -01,      -01, -01 },        // 1,0
                { 12, 22, 21,  20,  10,  00,       01,  02 },        // 1,1
                { 13, 23, 22,  21,  11,  01,       02,  03 },        // 1,2
                { 23, 22, 12,  02,  03, -01,      -01, -01 },        // 1,3
                { 21, 31, 30,  10,  11, -01,      -01, -01 },        // 2,0
                { 22, 32, 31,  30,  20,  10,       11,  12 },        // 2,1
                { 23, 33, 32,  31,  21,  11,       12,  13 },        // 2,2
                { 33, 32, 22,  12,  13, -01,      -01, -01 },        // 2,3
                { 31, 20, 21, -01, -01, -01,      -01, -01 },        // 3,0
                { 32, 30, 20,  21,  22, -01,      -01, -01 },        // 3,1
                { 33, 31, 21,  22,  23, -01,      -01, -01 },        // 3,2
                { 32, 22, 23, -01, -01, -01,      -01, -01 }         // 3,3
            };
            #endregion
            var map = new AdjacencyMap();
            int xx = 0; int yy = 0;
            int x; int y;

            for (x = 0; x < 4; x++)
            {
                for (y = 0; y < 4; y++)
                {
                    var node = new AdjacencyNode {
                        NodeNumber  = array[x, y],
                        Adjacency   = new List <int>(),
                        NodeContent = arrayOfContent[x, y]
                    };

                    while (yy < 8 && neighbors[xx, yy] != -1)
                    {
                        var m = neighbors[xx, yy] / 10;
                        var n = neighbors[xx, yy] % 10;
                        node.Adjacency.Add(array[m, n]);
                        ++yy;
                    }
                    ++xx;
                    yy = 0;
                    map.AddNode(node);
                }
            }
            return(map);
        }
        public IEnumerable <AdjacencyNode> GetAdjacencyVertexIterator(V vertex)
        {
            if (!_groupNodes.ContainsKey(vertex))
            {
                yield break;
            }
            AdjacencyNode ad = _groupNodes[vertex].head;

            while (ad != null)
            {
                yield return(ad);

                ad = ad.next;
            }
        }
Exemplo n.º 5
0
 public void AddNode(AdjacencyNode node)
 {
     map.Add(node.NodeNumber, node);
 }
            public readonly VertexNode Vertex; //结点指向的顶点

            #endregion Fields

            #region Constructors

            public AdjacencyNode(VertexNode vertex, AdjacencyNode next)
            {
                this.Vertex = vertex;
                this.Next = next;
            }
 internal AdjacencyNode(GroupNode to, E edge)
 {
     this.to   = to;
     this.edge = edge;
     next      = null;
 }
 internal AdjacencyNode(GroupNode to, E edge, AdjacencyNode prev) : this(to, edge)
 {
     prev.next = this;
 }