Esempio n. 1
0
        private void addToAnnex(string word, WordCoordinate coordinate)
        {
            if (coordinate.Direction == 0)
            {
                Console.WriteLine(horizontalAnnex);
                List <string> l;
                try
                {
                    l = horizontalAnnex[coordinate.X];
                }
                catch (Exception)
                {
                    //throw Exception;
                    l = new List <string>();
                }
                l.Add(word);
                horizontalAnnex[coordinate.X] = l;
            }
            else
            {
                List <string> l;
                try
                {
                    l = verticalAnnex[coordinate.Y];
                }
                catch (Exception)
                {
                    l = new List <string>();
                }

                l.Add(word);
                verticalAnnex[coordinate.Y] = l;
            }
        }
Esempio n. 2
0
        private object[] pickupWord(List <string> remainingWords)
        {
            WordCoordinate maxCoordinate = null;
            string         theWord       = null;

            foreach (string word in remainingWords)
            {
                List <WordCoordinate> coordinates = getPossibleCoordinates(word);

                if (coordinates.Count == 0)
                {
                    continue;
                }

                coordinates = coordinates.OrderBy(r => r, new WordCoordinateComparator()).ToList();

                if (maxCoordinate == null || (maxCoordinate.Score < coordinates[0].Score))
                {
                    maxCoordinate = coordinates[0];
                    theWord       = word;
                }
            }

            return(new object[] { theWord, maxCoordinate });
        }
Esempio n. 3
0
        public virtual int generate()
        {
            List <string> remainingWords = new List <string>();

            foreach (string worda in words)
            {
                remainingWords.Add(worda);
            }

            //initialization
            string         word       = remainingWords[0].ToUpper();
            WordCoordinate coordinate = new WordCoordinate(0, 0, rnd.Next(2), 0);

            /*new WordCoordinate(rnd.nextInt(grid.getMaxRows()-word.length()),
             *      rnd.nextInt(grid.getMaxColumns()-word.length()),rnd.nextInt(2),0);*/

            while (!string.ReferenceEquals(word, null))
            {
                writeWordToGrid(word, coordinate);
                remainingWords.Remove(word);

                addToAnnex(word, coordinate);

                object[] result = pickupWord(remainingWords);
                word       = (string)result[0];
                coordinate = (WordCoordinate)result[1];
            }

            return(words.Count - remainingWords.Count);
        }
Esempio n. 4
0
        private void writeWordToGrid(string word, WordCoordinate coordinate)
        {
            int k = 0;

            for (int i = (coordinate.Direction == 0) ? coordinate.Y : coordinate.X; i < word.Length + ((coordinate.Direction == 0) ? coordinate.Y : coordinate.X); i++)
            {
                grid.setValue(coordinate.Direction == 0 ? coordinate.X : i, coordinate.Direction == 0 ? i : coordinate.Y, word[k++]);
            }
        }
Esempio n. 5
0
        private void writeWordToGrid(string word, WordCoordinate coordinate)
        {
            int k      = 0;
            int startX = 0;
            int startY = 0;

            for (int i = (coordinate.Direction == 0) ? coordinate.Y : coordinate.X; i < word.Length + ((coordinate.Direction == 0) ? coordinate.Y : coordinate.X); i++)
            {
                if (k <= 0)
                {
                    startX = coordinate.X;
                    startY = coordinate.Y;
                }
                grid.setValue(coordinate.Direction == 0 ? coordinate.X : i, coordinate.Direction == 0 ? i : coordinate.Y, word[k++]);
            }

            generatedWords.Add(new GeneratedWord(word, coordinate.Direction, startX, startY));
        }
Esempio n. 6
0
        public virtual double generate()
        {
            List <string> remainingWords = new List <string>();

            foreach (string worda in words)
            {
                remainingWords.Add(worda);
            }

            //initialization
            string         word       = remainingWords[0].ToUpper();
            WordCoordinate coordinate = new WordCoordinate(0, 0, rnd.Next(2), 0);

            /*new WordCoordinate(rnd.nextInt(grid.getMaxRows()-word.length()),
             *      rnd.nextInt(grid.getMaxColumns()-word.length()),rnd.nextInt(2),0);*/

            while (!string.ReferenceEquals(word, null))
            {
                writeWordToGrid(word, coordinate);
                remainingWords.Remove(word);

                addToAnnex(word, coordinate);

                object[] result = pickupWord(remainingWords);
                word       = (string)result[0];
                coordinate = (WordCoordinate)result[1];
            }

            //foreach (GeneratedWord gWord in generatedWords)
            //{
            //    Console.WriteLine(gWord.getWord() + ": " + gWord.getDirection() + "(" + gWord.getStartX() + ", " + gWord.getStartY() + ")");
            //}
            int    usedCount  = words.Count - remainingWords.Count;
            double totalScore = calculateNumIntersectionsEachWord();

            grid.clear();
            return((totalScore / (double)usedCount) * ((double)usedCount / (double)words.Count));
        }
Esempio n. 7
0
        private int checkScore(string word, WordCoordinate coordinate)
        {
            int row = coordinate.X;
            int col = coordinate.Y;

            if (col < 0 || row < 0)
            {
                return(0);
            }

            int count = 1;
            int score = 1;

            foreach (char?letter in word.ToCharArray())
            {
                if (!isEmpty(row, col) && grid.getValue(row, col) != letter.Value)
                {
                    return(0);
                }

                if (grid.getValue(row, col) == letter.Value)
                {
                    score++;
                }

                if (coordinate.Direction == 1)
                {
                    //vertical
                    if (grid.getValue(row, col) != letter.Value && (!isEmpty(row, col + 1) || !isEmpty(row, col - 1)))
                    {
                        return(0);
                    }

                    if (count == 1 && !isEmpty(row - 1, col))
                    {
                        return(0);
                    }

                    if (count == word.Length && !isEmpty(row + 1, col))
                    {
                        return(0);
                    }

                    row++;
                }
                else
                {
                    //Horizontal

                    if (grid.getValue(row, col) != letter.Value && (!isEmpty(row - 1, col) || !isEmpty(row + 1, col)))
                    {
                        return(0);
                    }

                    if (count == 1 && !isEmpty(row, col - 1))
                    {
                        return(0);
                    }

                    if (count == word.Length && !isEmpty(row, col + 1))
                    {
                        return(0);
                    }

                    col++;
                }

                count++;
            }

            return(score);
        }