Exemplo n.º 1
0
 /// <summary>
 /// Check whether insert letter's surroundings have two other letters
 /// </summary>
 /// <param name="testLetter"></param>
 /// <param name="insertionLetter"></param>
 /// <returns></returns>
 public static bool HasAdjacentLetters(LetterInfo testLetter, LetterInfo insertionLetter, int CrozzleRow, int CrozzleColumn, char[,] GridContent)
 {
     if (testLetter.Column == insertionLetter.Column - 1)
     {
         if (insertionLetter.Column + 1 < CrozzleColumn && GridContent[insertionLetter.Row, insertionLetter.Column + 1] != ' ')
         {
             return(true);
         }
     }
     else if (testLetter.Column == insertionLetter.Column + 1)
     {
         if (insertionLetter.Column - 1 >= 0 && GridContent[insertionLetter.Row, insertionLetter.Column - 1] != ' ')
         {
             return(true);
         }
     }
     else if (testLetter.Row == insertionLetter.Row - 1)
     {
         if (insertionLetter.Row + 1 < CrozzleRow && GridContent[insertionLetter.Row + 1, insertionLetter.Column] != ' ')
         {
             return(true);
         }
     }
     else if (testLetter.Row == insertionLetter.Row + 1)
     {
         if (insertionLetter.Row - 1 >= 0 && GridContent[insertionLetter.Row - 1, insertionLetter.Column] != ' ')
         {
             return(true);
         }
     }
     return(false);
 }
        /// <summary>
        /// Calculate the score based on current grid
        /// </summary>
        public static int CalculateScore(int pointsPerWord, int CrozzleRow, int CrozzleColumn, char[,] GridContent, Dictionary <char, int> IntersectingPointsPerLetter, Dictionary <char, int> NonIntersectingPointsPerLetter)
        {
            int score = 0;

            score += (CountWordAmount(CrozzleRow, CrozzleColumn, GridContent) * pointsPerWord);
            for (int r = 0; r < CrozzleRow; r++)
            {
                for (int c = 0; c < CrozzleColumn; c++)
                {
                    if (GridContent[r, c] != ' ')
                    {
                        if (LetterInfo.IsInsertionLetter(r, c, CrozzleRow, CrozzleColumn, GridContent))
                        {
                            score += IntersectingPointsPerLetter[GridContent[r, c]];
                        }
                        else
                        {
                            score += NonIntersectingPointsPerLetter[GridContent[r, c]];
                        }
                    }
                }
            }
            return(score);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Determin whether the insert word could insert into the letter in insert letter list, and process insertion if positive
        /// </summary>
        /// <param name="insertionLetter"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        private bool InsertWords(LetterInfo insertionLetter, String word)
        {
            if (IsEmpty())
            {
                return(false);
            }

            List <int> locations = new List <int>();

            for (int i = 0; i < word.Length; i++)
            {
                if (word[i] == insertionLetter.Letter)
                {
                    locations.Add(i);
                }
            }

            if (locations.Count == 0)
            {
                return(false);
            }

            String direction   = vertical;
            int    insertIndex = -1;
            int    decrease    = -1;
            int    plus        = -1;

            // Determine if it could be inserted horizontally
            foreach (int location in locations)
            {
                decrease = location;
                plus     = word.Length - location;

                if (insertionLetter.Column - decrease < 0 || insertionLetter.Column + plus > CrozzleColumn)
                {
                    continue;
                }
                bool Continue = false;
                for (int gLocation = insertionLetter.Column - decrease; gLocation < insertionLetter.Column + plus; gLocation++)
                {
                    if (gLocation == insertionLetter.Column)
                    {
                        continue;
                    }

                    if (GridContent[insertionLetter.Row, gLocation] != ' ')
                    {
                        Continue = true;
                        break;
                    }

                    if (gLocation == insertionLetter.Column - 1 || gLocation == insertionLetter.Column + 1)
                    {
                        // The 4 direction of inserted letter only allows 1 letter
                        if (CheckSurrounding(insertionLetter.Row, gLocation) > 1)
                        {
                            Continue = true;
                            break;
                        }

                        LetterInfo testLetter = new LetterInfo(insertionLetter.Row, gLocation, word[gLocation - (insertionLetter.Column - decrease)]);

                        // Check surrounding specific circumstances (2 letters around)
                        if (LetterInfo.HasAdjacentLetters(testLetter, insertionLetter, CrozzleRow, CrozzleColumn, GridContent))
                        {
                            Continue = true;
                            break;
                        }
                    }
                    else
                    {
                        if (CheckSurrounding(insertionLetter.Row, gLocation) > 0)
                        {
                            Continue = true;
                            break;
                        }
                    }
                }
                if (Continue == true)
                {
                    continue;
                }
                else
                {
                    // The word could be inserted horizontaly
                    direction   = horizontal;
                    insertIndex = location;
                    break;
                }
            }

            if (direction == vertical)
            {
                foreach (int location in locations)
                {
                    decrease = location;
                    plus     = word.Length - location;

                    if (insertionLetter.Row - decrease < 0 || insertionLetter.Row + plus > CrozzleRow)
                    {
                        continue;
                    }
                    bool Continue = false;
                    for (int gLocation = insertionLetter.Row - decrease; gLocation < insertionLetter.Row + plus; gLocation++)
                    {
                        if (gLocation == insertionLetter.Row)
                        {
                            continue;
                        }

                        if (GridContent[gLocation, insertionLetter.Column] != ' ')
                        {
                            Continue = true;
                            break;
                        }

                        if (gLocation == insertionLetter.Row - 1 || gLocation == insertionLetter.Row + 1)
                        {
                            // the same as vertical
                            if (CheckSurrounding(gLocation, insertionLetter.Column) > 1)
                            {
                                Continue = true;
                                break;
                            }

                            LetterInfo testLetter = new LetterInfo(gLocation, insertionLetter.Column, word[gLocation - (insertionLetter.Row - decrease)]);

                            if (LetterInfo.HasAdjacentLetters(testLetter, insertionLetter, CrozzleRow, CrozzleColumn, GridContent))
                            {
                                Continue = true;
                                break;
                            }
                        }
                        else
                        {
                            if (CheckSurrounding(gLocation, insertionLetter.Column) > 0)
                            {
                                Continue = true;
                                break;
                            }
                        }
                    }
                    if (Continue == true)
                    {
                        continue;
                    }
                    else
                    {
                        // The word can be inserted vertically
                        direction   = vertical;
                        insertIndex = location;
                        break;
                    }
                }
            }
            // If thw word can't be inserted
            if (insertIndex == -1)
            {
                return(false);
            }

            if (direction == horizontal)
            {
                for (int gLocation = insertionLetter.Column - decrease, i = 0; gLocation < insertionLetter.Column + plus; gLocation++, i++)
                {
                    GridContent[insertionLetter.Row, gLocation] = word[i];
                }
            }
            else if (direction == vertical)
            {
                for (int gLocation = insertionLetter.Row - decrease, i = 0; gLocation < insertionLetter.Row + plus; gLocation++, i++)
                {
                    GridContent[gLocation, insertionLetter.Column] = word[i];
                }
            }
            SaveLetters();
            score = WordInfo.CalculateScore(pointsPerWord, CrozzleRow, CrozzleColumn, GridContent, IntersectingPointsPerLetter, NonIntersectingPointsPerLetter);
            return(true);
        }