示例#1
0
        public static void GenerateCrozzle(CrozzlePartial cp)
        {
            if (!Crozzle.aTimer.Enabled)
            {
                return;
            }
            List <Word> wordsToInsert = CanInsertWords(cp);
            int         length        = wordsToInsert.Count();

            if (length == 0)
            {
                // one crozzle is found
                if (cp.GetScore() > highScore)
                {
                    highScore = cp.GetScore();
                }
                else
                {
                    return;// return if the new crozzle's score is not high enough
                }
                if (PublicInfo.GetMinGroups() > 1)
                {
                    cp = SeperateGroups(cp);
                }
                if (cp.GetScore() > HighScoreCrozzle.GetScore())
                {
                    HighScoreCrozzle = cp;

                    // print score of crozzle
                    Console.WriteLine("===============================================================");
                    Console.WriteLine("New Highest Score: " + cp.GetScore());
                    Console.WriteLine("===============================================================");
                }
                return;
            }
            if (cp.GetUsedWord().Count >= PublicInfo.GetRows() && cp.GetUsedWord().Count <= PublicInfo.GetRows() + PublicInfo.GetColumns() && cp.GetScore() <= cp.GetUsedWord().Count *averageScorePerWord * 5 / 6)
            {
                return;
            }
            for (int i = 0; i < length; i++)
            {
                if (wordsToInsert[i].GetAddScore() < 0)
                {
                    continue;
                }
                CrozzlePartial c = InsertWord(cp, wordsToInsert[i], wordsToInsert[i].GetAddScore());
                GenerateCrozzle(c);
            }
        }
示例#2
0
        private static int CalculateScoreAfterRemovingAWord(CrozzlePartial cp, Word removeWord)
        {
            int score = cp.GetScore();

            // reverse the process of adding a word
            score -= PublicInfo.GetPointsPerWord();
            for (int i = 0; i < cp.GetUsedWord().Count; i++)
            {
                if (!cp.GetUsedWord()[i].GetType().Equals(removeWord.GetType()) && CrozzleValidation.Crossing(cp.GetUsedWord()[i], removeWord))
                {
                    char crossingLetter = CrozzleValidation.GetCrossingLetter(cp.GetUsedWord()[i], removeWord);
                    score -= WordInfo.intersectingPointsPerLetter[crossingLetter];
                    score += WordInfo.nonIntersectingPointsPerLetter[crossingLetter];
                }
            }
            return(score);
        }
示例#3
0
        private void CreateCrozzle()
        {
            Config configuration = new Config();

            configuration.SetConfig("configuration.txt");
            string intersectionsPerPoints    = configuration.GetIntersectingPointsPerLetter();
            string nonIntersectionsPerPoints = configuration.GetNonIntersectingPointsPerLetter();

            InitializePublicInfo(configuration);

            WordInfo wordlist = new WordInfo();

            wordlist.SetWordList("wordList.txt");
            wordlist.SetIntersectingPointsPerLetter(intersectionsPerPoints);
            wordlist.SetNonIntersectingPointsPerLetter(nonIntersectionsPerPoints);
            List <string> wlist = wordlist.GetWordList();


            // preprocess wordlist
            SIT323Crozzle.CreateCrozzle.SetPotentialValue(wlist, wordlist.GetIntersectingPointsPerLetter(), wordlist.GetNonIntersectingPointsPerLetter());
            string initialWordContent = PreProcess(wlist);

            // get initial crozzle
            CrozzlePartial initialCrozzle = GenerateInitialCrozzle(initialWordContent, wlist);

            aTimer.Start();
            SIT323Crozzle.CreateCrozzle.GenerateCrozzle(initialCrozzle);
            CrozzlePartial goodCrozzle = SIT323Crozzle.CreateCrozzle.GetBestCrozzle();

            aTimer.Stop();

            // get grid and show crozzle
            string style       = configuration.GetStyle();
            bool   uppercase   = configuration.GetUppercase();
            Grid   crozzleGrid = goodCrozzle.GetGrid();

            char[,] largeGrid = crozzleGrid.GetGrid();
            char[,] grid      = new char[PublicInfo.GetRows(), PublicInfo.GetColumns()];
            for (int i = 0; i < PublicInfo.GetRows(); i++)
            {
                for (int j = 0; j < PublicInfo.GetColumns(); j++)
                {
                    if (i + goodCrozzle.GetMinHeight() <= goodCrozzle.GetMaxHeight() && j + goodCrozzle.GetMinWidth() <= goodCrozzle.GetMaxWidth())
                    {
                        grid[i, j] = largeGrid[i + goodCrozzle.GetMinHeight(), j + goodCrozzle.GetMinWidth()];
                    }
                }
            }
            String crozzleHTML = @"<!DOCTYPE html>
                                <html>
                                <head>" + style + @"</head><body><table>";

            for (int row = 0; row < PublicInfo.GetRows(); row++)
            {
                String tr = "<tr>";
                for (int column = 0; column < PublicInfo.GetColumns(); column++)
                {
                    if (grid[row, column].CompareTo('\0') != 0)
                    {
                        tr += @"<td style='background:" + configuration.GetBgcolourNonEmptyTD();
                        if (uppercase == true)
                        {
                            tr += "'>" + grid[row, column].ToString().ToUpper() + @"</td>";
                        }
                        if (uppercase == false)
                        {
                            tr += "'>" + grid[row, column].ToString().ToLower() + @"</td>";
                        }
                    }
                    else
                    {
                        tr += @"<td style='background:" + configuration.GetBgcolourEmptyTD();
                        if (uppercase == true)
                        {
                            tr += "'>" + grid[row, column].ToString().ToUpper() + @"</td>";
                        }
                        if (uppercase == false)
                        {
                            tr += "'>" + grid[row, column].ToString().ToLower() + @"</td>";
                        }
                    }
                }
                tr += "</tr>";

                crozzleHTML += tr;
            }
            crozzleHTML += @"</table>";
            crozzleHTML += "<p>score: ";
            crozzleHTML += goodCrozzle.GetScore();
            crozzleHTML += "</p>";
            crozzleHTML += "</body></html>";
            CrozzleBrowser.DocumentText = crozzleHTML;
            ErrorBrowser.DocumentText   = " ";
        }
示例#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);
        }