Ejemplo n.º 1
0
        public Board(int width, string config, ContentManager content)
        {
            m_width = width;
            m_activeConfiguration = config;

            m_spriteFont     = content.Load <SpriteFont>("VCR");
            m_spriteFontMini = content.Load <SpriteFont>("VCRMINI");
            m_squareTexture  = content.Load <Texture2D>("Square");

            m_trieTree           = new CharNode();
            m_trieTree.character = '.';
            m_trieTree.children  = new List <CharNode>();

            m_busyDFS = new bool[m_width, m_width];

            for (int i = 0; i < m_width; i++)
            {
                for (int j = 0; j < m_width; j++)
                {
                    m_busyDFS[i, j] = false;
                }
            }

            m_alreadyBeenMap = new Dictionary <string, bool>();

            InitializeDictionary();

            List <SolutionWord> tmp = null;

            TestConfiguration(m_activeConfiguration, ref tmp);
        }
Ejemplo n.º 2
0
        private void AddStringToTree(string str, int index, CharNode crtNode)
        {
            bool addedForward = false;

            foreach (var child in crtNode.children)
            {
                if (index < str.Length)
                {
                    if (child.character == Char.ToLower(str[index]))
                    {
                        AddStringToTree(str, index + 1, child);
                        addedForward = true;
                    }
                }
                else
                {
                    if (child.character == '_')
                    {
                        addedForward = true;
                    }
                }
            }

            if (!addedForward)
            {
                if (index < str.Length)
                {
                    CharNode newNode = new CharNode();
                    newNode.character = Char.ToLower(str[index]);
                    newNode.children  = new List <CharNode>();
                    crtNode.children.Add(newNode);

                    AddStringToTree(str, index + 1, newNode);
                }
                else
                {
                    CharNode newNode = new CharNode();
                    newNode.character      = '_';
                    newNode.children       = new List <CharNode>();
                    newNode.completeString = str;
                    crtNode.children.Add(newNode);
                }
            }
        }
Ejemplo n.º 3
0
        private void DFS(int line, int column, ref char[,] charMatrix, CharNode crtNode, ref int score, ref List <SolutionWord> solution)
        {
            int[] dirY = new int[] { 0, -1, 0, 1, -1, 1, -1, 1 };
            int[] dirX = new int[] { 1, 0, -1, 0, -1, 1, 1, -1 };

            if (ValidPositionInBoard(line, column))
            {
                if (!m_busyDFS[line, column])
                {
                    CharNode nextNode = new CharNode();
                    nextNode.character = '~';            // Invalid character

                    if (charMatrix[line, column] != '#') // The Qu character
                    {
                        foreach (CharNode child in crtNode.children)
                        {
                            if (child.character == charMatrix[line, column])
                            {
                                nextNode = child;
                            }
                        }
                    }
                    else
                    {
                        foreach (CharNode child in crtNode.children)
                        {
                            if (child.character == 'q')
                            {
                                nextNode = child;
                            }
                        }

                        if (nextNode.character != '~')
                        {
                            foreach (CharNode child in nextNode.children)
                            {
                                if (child.character == 'u')
                                {
                                    nextNode = child;
                                }
                            }
                        }

                        if (nextNode.character == 'q')
                        {
                            nextNode.character = '~';
                        }
                    }

                    if (nextNode.character != '~')
                    {
                        m_busyDFS[line, column] = true;

                        for (int i = 0; i < dirY.Length; i++)
                        {
                            int newLine   = line + dirY[i];
                            int newColumn = column + dirX[i];

                            DFS(newLine, newColumn, ref charMatrix, nextNode, ref score, ref solution);
                        }

                        foreach (CharNode child in nextNode.children)
                        {
                            if (child.character == '_')
                            {
                                if (!m_alreadyBeenMap.ContainsKey(child.completeString))
                                {
                                    m_alreadyBeenMap[child.completeString] = true;
                                    int scr = WordScore(child.completeString.Length);

                                    score += scr;

                                    if (scr != 0 && solution != null)
                                    {
                                        SolutionWord solWord = new SolutionWord();
                                        solWord.word  = child.completeString;
                                        solWord.score = scr;

                                        solution.Add(solWord);
                                    }

                                    // For debugging purposes:
                                    // Console.WriteLine("{0} {1}", child.completeString, scr);
                                }
                            }
                        }

                        m_busyDFS[line, column] = false;
                    }
                }
            }
        }