Exemplo n.º 1
0
        public void remove(object key)
        {
            string strKey = key.ToString();

            // Tokenize the key
            char[] tokens   = strKey.ToCharArray();
            Trie   tNewNode = null;

            // Add each key character to its own trie node
            // First search if the token already exists
            int tokenIndex = searchToken(tokens[0]);

            if (tokenIndex == -1)
            {
                return;
            }
            else
            {
                // To Do: Typechecking before casting
                tNewNode = (Trie)m_trieList[tokenIndex];

                // Remove the first character from key and
                // pass it to the next level
                string strTemp = new string(tokens);
                strTemp = strTemp.Remove(0, 1);

                // Condition to break recursion.
                // Remove it
                if (strTemp.Length == 0)
                {
                    tNewNode = null;
                    m_trieList.RemoveAt(tokenIndex);

                    return;
                }
                // Recurse with remaining tokens
                tNewNode.remove(strTemp);
                if ((tNewNode.getChildCount() == 0) && (tNewNode.getValue() == null))
                {
                    m_trieList.RemoveAt(tokenIndex);
                }
            }
        }
Exemplo n.º 2
0
        public object getValue(object key)
        {
            string strKey = key.ToString();

            // Tokenize the key
            char[] tokens   = strKey.ToCharArray();
            Trie   tNewNode = null;

            // Add each key character to its own trie node
            // First search if the token already exists
            int tokenIndex = searchToken(tokens[0]);

            if (tokenIndex == -1)
            {
                return(null);
            }
            else
            {
                // To Do: Typechecking before casting
                tNewNode = (Trie)m_trieList[tokenIndex];
            }
            // Remove the first character from key and
            // pass it to the next level
            string strTemp = new string(tokens);

            strTemp = strTemp.Remove(0, 1);

            // Condition to break recursion.
            // Get value from the last key token node
            if (strTemp.Length == 0)
            {
                return(tNewNode.m_obj);
            }
            // Recurse with remaining tokens
            return(tNewNode.getValue(strTemp));
        }
Exemplo n.º 3
0
        public override char Move()
        {
            // First check if threathened
            int[][] grid     = game.GetBoard();
            int     gridSize = game.GetGridSize();
            int     mark     = GetMark();
            int     otherMark;
            char    defendChoice  = ' ';
            char    winningChoice = ' ';
            char    choice        = ' ';

            if (mark == 1)
            {
                otherMark = 2;
            }
            else
            {
                otherMark = 1;
            }

            // Players play either '1' or '2'
            int count;

            // Check rows
            for (int row = 0; row < gridSize; row++)
            {
                count = 0;
                for (int col = 0; col < gridSize; col++)
                {
                    if (grid[row][col] == otherMark)
                    {
                        count++;
                    }
                }
                if (count == gridSize - 1)
                {
                    // Defense
                    for (int col = 0; col < gridSize; col++)
                    {
                        if (grid[row][col] == 0)
                        {
                            defendChoice = (char)(row * gridSize + col + 97);
                        }
                    }
                }
            }

            // Check columns
            for (int col = 0; col < gridSize; col++)
            {
                count = 0;
                for (int row = 0; row < gridSize; row++)
                {
                    if (grid[row][col] == otherMark)
                    {
                        count++;
                    }
                }
                if (count == gridSize - 1)
                {
                    // Defense
                    for (int row = 0; row < gridSize; row++)
                    {
                        if (grid[row][col] == 0)
                        {
                            defendChoice = (char)(row * gridSize + col + 97);
                        }
                    }
                }
            }

            // Check positive diagonal
            count = 0;
            for (int row = 0, col = 0; (row < gridSize && col < gridSize); row++, col++)
            {
                if (grid[row][col] == otherMark)
                {
                    count++;
                }
            }
            if (count == gridSize - 1)
            {
                // Defense
                for (int row = 0, col = 0; (row < gridSize && col < gridSize); row++, col++)
                {
                    if (grid[row][col] == 0)
                    {
                        defendChoice = (char)(row * gridSize + col + 97);
                    }
                }
            }

            // Check negative diagonal
            count = 0;
            for (int row = 0, col = gridSize - 1; (row < gridSize && col >= 0); row++, col--)
            {
                if (grid[row][col] == otherMark)
                {
                    count++;
                }
            }
            if (count == gridSize - 1)
            {
                // Defense
                for (int row = 0, col = gridSize - 1; (row < gridSize && col >= 0); row++, col--)
                {
                    if (grid[row][col] == 0)
                    {
                        defendChoice = (char)(row * gridSize + col + 97);
                    }
                }
            }

            // See if we have learnt anything in the past
            ArrayList alPossibilities = (ArrayList)memory.getChildTries(game.GetMoves());

            if (alPossibilities != null)
            {
                if (alPossibilities.Count > 0)
                {
                    // For the time being, select the first possibility
                    Trie t = (Trie)alPossibilities[new Random().Next(alPossibilities.Count)];
                    if (t.getValue() != null)
                    {
                        winningChoice = t.getChar();
                    }
                    else
                    {
                        choice = t.getChar();
                    }
                }
                else
                {
                }
            }
            else
            {
                // Go for a random placement
                while (true)
                {
                    choice = (char)(new Random().Next(game.GetGridSize() * game.GetGridSize()) + 97);
                    if (game.IsEmpty(choice))
                    {
                        break;
                    }
                }
            }
            if (winningChoice != ' ')
            {
                choice = winningChoice;
            }
            else if (defendChoice != ' ')
            {
                choice = defendChoice;
            }

            return(choice);
        }