Esempio n. 1
0
        static Pair <WordBlock, int> PlaceWord(ref List <WordBlock> data, Alphaword word, int width, int height)
        {
            List <Pair <WordBlock, int> > results = new List <Pair <WordBlock, int> >();

            for (int i = 0; i < data.Count; ++i) // loop through currently available words
            {
                if (data[i].IsHorizontal)        // vertical new word
                {
                    var curr_result = PlaceVertical(ref data, word, i, width, height);
                    if (curr_result != null)
                    {
                        results.Add(curr_result);
                    }
                }
                else if (data[i].IsVertical)                // horizontal new word
                {
                    var curr_result = PlaceHorizontal(ref data, word, i, width, height);
                    if (curr_result != null)
                    {
                        results.Add(curr_result);
                    }
                }
            }
            if (results.Count == 0)
            {
                return(null);
            }
            results.Sort((x, y) => x.Right.CompareTo(y.Right));
            return(results[results.Count - 1]);
        }
Esempio n. 2
0
 public WordBlock(Alphaword w, Coordinates s, Coordinates e)
 {
     start = s;
     end   = e;
     word  = w;
 }
Esempio n. 3
0
 public WordBlock(Alphaword w)
 {
     start = end = Coordinates.Zero;
     word  = w;
 }
Esempio n. 4
0
 static Alphaword CopyWord(Alphaword word)
 {
     return(new Alphaword(string.Copy(word.word), string.Copy(word.hint)));
 }
Esempio n. 5
0
        static Pair <WordBlock, int> PlaceVertical(ref List <WordBlock> data, Alphaword word, int i, int width, int height)
        {
            List <Pair <WordBlock, int> > results = new List <Pair <WordBlock, int> >();

            for (int l = 0; l < data[i].Length; ++l)
            {
                Coordinates cell = data[i][l];
                for (int j = 0; j < word.Length; ++j)
                {
                    if (word.word[j] != data[i][cell])
                    {
                        // cant overlap
                        continue;
                    }
                    // check if vertical word can fit in grid.
                    Coordinates v_start    = new Coordinates(cell.x, cell.y - j);
                    Coordinates v_end      = new Coordinates(cell.x, cell.y + (word.Length - j - 1));
                    bool        v_front_ok = !(v_start.y < 0);
                    bool        v_back_ok  = !(v_end.y >= height);
                    if (!v_front_ok)
                    {
                        // because word is pushed "upwards" to test whole word,
                        // if word leaves top of board then it will definitely be
                        // invalid for the rest.
                        break;
                    }
                    else if (!v_back_ok)
                    {
                        // no space below, try next new word letter.
                        continue;
                    }
                    else
                    {
                        int  score   = 1;
                        bool placeOK = true;
                        // try placement. check if all other words overlap properly, if any overlap occurs.
                        for (int w = 0; w < data.Count && placeOK; ++w)
                        {
                            if (w == i)
                            {
                                continue;
                            }
                            // check if word clashes with other word
                            for (int y = 0; y < word.Length; ++y)
                            {
                                int         x     = cell.x;
                                Coordinates pos   = new Coordinates(x, v_start.y + y);
                                char        other = data[w][pos];
                                if (other == Cell.Empty)
                                {
                                    // not clashing
                                    Coordinates[] dir = new Coordinates[]
                                    {
                                        new Coordinates(pos.x + 1, pos.y),
                                        new Coordinates(pos.x - 1, pos.y),
                                        new Coordinates(pos.x, pos.y + 1),
                                        new Coordinates(pos.x, pos.y - 1)
                                    };
                                    bool near = false;
                                    // cannot be right beside the word.
                                    for (int d = 0; d < dir.Length && !near; ++d)
                                    {
                                        char test = data[w][dir[d]];
                                        if (test != Cell.Empty)
                                        {
                                            near = true;
                                        }
                                    }
                                    if (near)
                                    {
                                        placeOK = false;
                                    }
                                    continue;
                                }
                                else if (other == word.word[y] && data[w].IsHorizontal)
                                {
                                    // overlap same character
                                    break;
                                }
                                else
                                {
                                    // fail
                                    placeOK = false;
                                }
                            }
                        }
                        if (placeOK)
                        {
                            // add word block and score.
                            results.Add(Pairs.MakePair(new WordBlock(word, v_start, v_end), score));
                        }
                    }
                }
            }
            if (results.Count == 0)
            {
                return(null);
            }
            results.Sort((x, y) => x.Right.CompareTo(y.Right));
            return(results[results.Count - 1]);
        }