Example #1
0
        /// <summary>
        /// Seperate a one-group crozzle into multi-group crozzle
        /// </summary>
        /// <param name="cp">One-group crozzle</param>
        /// <returns>One multi-group crozzle</returns>
        public static CrozzlePartial SeperateGroups(CrozzlePartial cp)
        {
            int         bestScore     = 0;
            List <Word> bestUsedWords = new List <Word>();

            for (int i = 0; i < cp.GetUsedWord().Count; i++)
            {
                List <Word> usedWords = new List <Word>();
                for (int j = 0; j < cp.GetUsedWord().Count; j++)
                {
                    usedWords.Add(cp.GetUsedWord()[j]);
                }
                Word deleteWord = cp.GetUsedWord()[i];
                usedWords.Remove(cp.GetUsedWord()[i]);
                int groups;
                try
                {
                    WordGroupServiceClient ws;
                    string endpoint = "BasicHttpBinding_IWordGroupService";
                    ws = new WordGroupServiceClient(endpoint);
                    Grid     grid       = new Grid(PublicInfo.GetFullRows(), PublicInfo.GetFullColumns(), usedWords);
                    string[] stringGrid = ConvertToString(grid.GetGrid(), PublicInfo.GetFullRows(), PublicInfo.GetFullColumns());
                    groups = ws.Count(stringGrid);
                }
                catch
                {
                    // use local group calculation method if online method fail
                    groups = CalculateGroup(usedWords);
                }
                int score = CalculateScoreAfterRemovingAWord(cp, cp.GetUsedWord()[i]);
                if (groups >= PublicInfo.GetMinGroups() && groups <= PublicInfo.GetMaxGroups() && score > bestScore && CheckAllIntersections(usedWords))
                {
                    bestScore     = score;
                    bestUsedWords = usedWords;
                }
            }
            // generate a multi-groups crozzle with used words
            cp.SetUsedWord(bestUsedWords);
            cp.SetScore(bestScore);
            cp.SetGrid(new Grid(PublicInfo.GetFullRows(), PublicInfo.GetFullColumns(), bestUsedWords));
            return(cp);
        }
Example #2
0
        private CrozzlePartial GenerateInitialCrozzle(string initialWordContent, List <string> wlist)
        {
            CrozzlePartial cp = new CrozzlePartial();

            // set initial word position
            Word initialWord = SetInitialWordPositionAndCrozzlePosition(initialWordContent, cp);

            // set initial crozzle
            List <Word> usedWord = new List <Word>();

            usedWord.Add(initialWord);
            Grid g = new Grid(PublicInfo.GetFullRows(), PublicInfo.GetFullColumns(), usedWord);

            cp.SetGrid(g);
            cp.SetUsedWord(usedWord);
            cp.SetWordlist(wlist);
            int initialScore = GetInitialScore(initialWordContent);

            cp.SetScore(initialScore + PublicInfo.GetPointsPerWord());
            return(cp);
        }
Example #3
0
        private Word SetInitialWordPositionAndCrozzlePosition(string initialWordContent, CrozzlePartial cp)
        {
            Word initialWord = new Word();

            initialWord.SetWordContent(initialWordContent);
            initialWord.SetType("ROW");
            double temp = (PublicInfo.GetFullRows() - 1) / 2.0;
            int    y    = (int)Math.Ceiling(temp);

            initialWord.SetRows(y);
            temp = (PublicInfo.GetFullColumns() - initialWordContent.Length) / 2.0;
            int x = (int)Math.Ceiling(temp);

            initialWord.SetColumns(x);
            cp.SetHeight(PublicInfo.GetFullRows());
            cp.SetWidth(PublicInfo.GetFullColumns());
            cp.SetMaxHeight(y);
            cp.SetMaxWidth(x + initialWordContent.Length - 1);
            cp.SetMinHeight(y);
            cp.SetMinWidth(x);
            return(initialWord);
        }
Example #4
0
        public static CrozzlePartial InsertWord(CrozzlePartial cp, Word s, int addScore)
        {
            CrozzlePartial returnCP = new CrozzlePartial();

            returnCP.SetWidth(cp.GetWidth());
            returnCP.SetHeight(cp.GetHeight());
            returnCP.SetMaxHeight(cp.GetMaxHeight());
            returnCP.SetMinHeight(cp.GetMinHeight());
            returnCP.SetMaxWidth(cp.GetMaxWidth());
            returnCP.SetMinWidth(cp.GetMinWidth());
            List <string> wordlist = new List <string>();

            for (int i = 0; i < cp.GetWordlist().Count; i++)
            {
                wordlist.Add(cp.GetWordlist()[i]);
            }
            List <Word> usedWord = new List <Word>();

            for (int i = 0; i < cp.GetUsedWord().Count; i++)
            {
                usedWord.Add(cp.GetUsedWord()[i]);
            }
            Grid grid = new Grid();

            int score = cp.GetScore();

            wordlist.Remove(s.GetWordContent());
            usedWord.Add(s);
            char[,] charGrid = new char[PublicInfo.GetFullRows(), PublicInfo.GetFullColumns()];
            for (int i = 0; i < PublicInfo.GetFullRows(); i++)
            {
                for (int j = 0; j < PublicInfo.GetFullColumns(); j++)
                {
                    charGrid[i, j] = cp.GetGrid().GetGrid()[i, j];
                }
            }
            int minHeight = cp.GetMinHeight();
            int maxHeight = cp.GetMaxHeight();
            int minWidth  = cp.GetMinWidth();
            int maxWidth  = cp.GetMaxWidth();

            if (s.GetType().Equals("ROW"))
            {
                int rows         = s.GetRows() - 1;
                int columnsBegin = s.GetColumns() - 1;
                int columnsEnd   = s.GetColumns() + s.GetWordContent().Length - 2;
                if (rows > maxHeight)
                {
                    returnCP.SetMaxHeight(rows);
                }
                if (rows < minHeight)
                {
                    returnCP.SetMinHeight(rows);
                }
                if (columnsBegin < minWidth && columnsEnd > maxWidth)
                {
                    returnCP.SetMaxWidth(columnsEnd);
                    returnCP.SetMinWidth(columnsBegin);
                }
                else if (columnsBegin < minWidth && columnsEnd <= maxWidth)
                {
                    returnCP.SetMinWidth(columnsBegin);
                }
                else if (columnsBegin >= minWidth && columnsEnd > maxWidth)
                {
                    returnCP.SetMaxWidth(columnsEnd);
                }
                for (int i = 0; i < s.GetWordContent().Length; i++)
                {
                    charGrid[s.GetRows() - 1, s.GetColumns() + i - 1] = s.GetWordContent()[i];
                }
            }
            else if (s.GetType().Equals("COLUMN"))
            {
                int columns   = s.GetColumns() - 1;
                int rowsBegin = s.GetRows() - 1;
                int rowsEnd   = s.GetRows() + s.GetWordContent().Length - 2;
                if (columns > maxWidth)
                {
                    returnCP.SetMaxWidth(columns);
                }
                if (columns < minWidth)
                {
                    returnCP.SetMinWidth(columns);
                }
                if (rowsBegin < minHeight && rowsEnd > maxHeight)
                {
                    returnCP.SetMaxHeight(rowsEnd);
                    returnCP.SetMinHeight(rowsBegin);
                }
                else if (rowsBegin < minHeight && rowsEnd <= maxHeight)
                {
                    returnCP.SetMinHeight(rowsBegin);
                }
                else if (rowsBegin >= minHeight && rowsEnd > maxHeight)
                {
                    returnCP.SetMaxHeight(rowsEnd);
                }
                for (int i = 0; i < s.GetWordContent().Length; i++)
                {
                    charGrid[s.GetRows() + i - 1, s.GetColumns() - 1] = s.GetWordContent()[i];
                }
            }
            score += addScore;
            grid.SetGrid(charGrid);
            returnCP.SetGrid(grid);
            // Set & Return
            returnCP.SetUsedWord(usedWord);
            returnCP.SetWordlist(wordlist);
            returnCP.SetGrid(grid);
            returnCP.SetScore(score);
            return(returnCP);
        }
Example #5
0
        public static int CalInsertScore(CrozzlePartial cp, Word word)
        {
            char[,] grid = cp.GetGrid().GetGrid();
            int minHeight     = cp.GetMinHeight();
            int maxHeight     = cp.GetMaxHeight();
            int minWidth      = cp.GetMinWidth();
            int maxWidth      = cp.GetMaxWidth();
            int currentHeight = maxHeight - minHeight + 1;
            int currentWidth  = maxWidth - minWidth + 1;
            int result        = 10;

            if (word.GetType().Equals("ROW"))
            {
                int rows         = word.GetRows() - 1;
                int columnsBegin = word.GetColumns() - 1;
                int columnsEnd   = word.GetColumns() + word.GetWordContent().Length - 2;
                if (columnsBegin < 0)
                {
                    return(-1);
                }
                if (columnsEnd > PublicInfo.GetFullColumns() - 1)
                {
                    return(-1);
                }
                if (rows > maxHeight)
                {
                    int newHeight = rows - minHeight + 1;
                    if (newHeight > PublicInfo.GetRows())
                    {
                        return(-1);
                    }
                }
                if (rows < minHeight)
                {
                    int newHeight = maxHeight - rows + 1;
                    if (newHeight > PublicInfo.GetRows())
                    {
                        return(-1);
                    }
                }
                if (columnsBegin < minWidth && columnsEnd > maxWidth)
                {
                    int newWidth = columnsEnd - columnsBegin + 1;
                    if (newWidth > PublicInfo.GetColumns())
                    {
                        return(-1);
                    }
                }
                else if (columnsBegin < minWidth && columnsEnd <= maxWidth)
                {
                    int newWidth = maxWidth - columnsBegin + 1;
                    if (newWidth > PublicInfo.GetColumns())
                    {
                        return(-1);
                    }
                }
                else if (columnsBegin >= minWidth && columnsEnd > maxWidth)
                {
                    int newWidth = columnsEnd - minWidth + 1;
                    if (newWidth > PublicInfo.GetColumns())
                    {
                        return(-1);
                    }
                }
                if (columnsBegin - 1 >= 0)
                {
                    if (grid[rows, columnsBegin - 1] != '\0')
                    {
                        return(-1);
                    }
                }
                if (columnsEnd + 1 <= PublicInfo.GetFullColumns() - 1)
                {
                    if (grid[rows, columnsEnd + 1] != '\0')
                    {
                        return(-1);
                    }
                }
                for (int i = 0; i < word.GetWordContent().Length; i++)
                {
                    result += WordInfo.nonIntersectingPointsPerLetter[word.GetWordContent()[i]];
                    if (grid[rows, columnsBegin + i] != '\0')
                    {
                        if (grid[rows, columnsBegin + i] != word.GetWordContent()[i])
                        {
                            return(-1);
                        }
                        else
                        {
                            result += WordInfo.intersectingPointsPerLetter[word.GetWordContent()[i]];
                            result -= 2 * WordInfo.nonIntersectingPointsPerLetter[word.GetWordContent()[i]];
                        }
                        // new
                        if (columnsBegin + i + 1 <= PublicInfo.GetFullColumns() - 1)
                        {
                            if (grid[rows, columnsBegin + i + 1] != '\0')
                            {
                                return(-1);
                            }
                        }
                    }
                    else
                    {
                        if (rows != 0)
                        {
                            if (grid[rows - 1, columnsBegin + i] != '\0')
                            {
                                return(-1);
                            }
                        }
                        if (rows != PublicInfo.GetFullRows() - 1)
                        {
                            if (grid[rows + 1, columnsBegin + i] != '\0')
                            {
                                return(-1);
                            }
                        }
                    }
                }
            }
            else if (word.GetType().Equals("COLUMN"))
            {
                int columns   = word.GetColumns() - 1;
                int rowsBegin = word.GetRows() - 1;
                int rowsEnd   = word.GetRows() + word.GetWordContent().Length - 2;
                if (rowsBegin < 0)
                {
                    return(-1);
                }
                if (rowsEnd > PublicInfo.GetFullRows() - 1)
                {
                    return(-1);
                }
                if (columns > maxWidth)
                {
                    int newWidth = columns - minWidth + 1;
                    if (newWidth > PublicInfo.GetColumns())
                    {
                        return(-1);
                    }
                }
                if (columns < minWidth)
                {
                    int newWidth = maxWidth - columns + 1;
                    if (newWidth > PublicInfo.GetColumns())
                    {
                        return(-1);
                    }
                }
                if (rowsBegin < minHeight && rowsEnd > maxHeight)
                {
                    int newHeight = rowsEnd - rowsBegin + 1;
                    if (newHeight > PublicInfo.GetRows())
                    {
                        return(-1);
                    }
                }
                else if (rowsBegin < minHeight && rowsEnd <= maxHeight)
                {
                    int newHeight = maxHeight - rowsBegin + 1;
                    if (newHeight > PublicInfo.GetRows())
                    {
                        return(-1);
                    }
                }
                else if (rowsBegin >= minHeight && rowsEnd > maxHeight)
                {
                    int newHeight = rowsEnd - minHeight + 1;
                    if (newHeight > PublicInfo.GetRows())
                    {
                        return(-1);
                    }
                }
                if (rowsBegin - 1 >= 0)
                {
                    if (grid[rowsBegin - 1, columns] != '\0')
                    {
                        return(-1);
                    }
                }
                if (rowsEnd + 1 <= PublicInfo.GetFullRows() - 1)
                {
                    if (grid[rowsEnd + 1, columns] != '\0')
                    {
                        return(-1);
                    }
                }
                for (int i = 0; i < word.GetWordContent().Length; i++)
                {
                    result += WordInfo.nonIntersectingPointsPerLetter[word.GetWordContent()[i]];
                    if (grid[rowsBegin + i, columns] != '\0')
                    {
                        if (grid[rowsBegin + i, columns] != word.GetWordContent()[i])
                        {
                            return(-1);
                        }

                        else
                        {
                            result += WordInfo.intersectingPointsPerLetter[word.GetWordContent()[i]];
                            result -= 2 * WordInfo.nonIntersectingPointsPerLetter[word.GetWordContent()[i]];
                        }
                        // new
                        if (rowsBegin + i + 1 <= PublicInfo.GetFullRows() - 1)
                        {
                            if (grid[rowsBegin + i + 1, columns] != '\0')
                            {
                                return(-1);
                            }
                        }
                    }
                    else
                    {
                        if (columns != 0)
                        {
                            if (grid[rowsBegin + i, columns - 1] != '\0')
                            {
                                return(-1);
                            }
                        }
                        if (columns != PublicInfo.GetFullColumns() - 1)
                        {
                            if (grid[rowsBegin + i, columns + 1] != '\0')
                            {
                                return(-1);
                            }
                        }
                    }
                }
            }
            return(result);
        }