Beispiel #1
0
    /// <summary>
    /// try's to find a letterset that is an subsett of length one bigger
    /// than the lettersett provided.
    /// If no sett is found an empty array is returned
    /// </summary>
    /// <param name="letterSett">the set to find candidate setts toc</param>
    /// <returns>an array of candidate lettersets, can be empty if none are found</returns>
    List <SetValuePair> FindNextLetterSetts(string[] letterSett)
    {
        List <SetValuePair> possibleLetterSetts = new List <SetValuePair>();

        foreach (SetValuePair svp in this.allSettValuePairs.GetListFromIndex(letterSett.Count() + 1))
        {
            if (WUArrays.IsASubsetB(letterSett, svp.letter_sett))
            {
                possibleLetterSetts.Add(svp);
            }
        }
        return(possibleLetterSetts);
    }
Beispiel #2
0
    /// <summary>
    /// returns a list of the possible reduction sets
    /// to the active set of size reductionWordSize
    /// </summary>
    /// <param name="reductionWordSize">the size of the reduction words</param>
    /// <returns>the possible reduction setts</returns>
    List <SetValuePair> GetPossibleReductions(int reductionWordSize)
    {
        List <SetValuePair> possibleReductionSetts = new List <SetValuePair>();

        string[] activeAr = this.activeLetterSet.ToArray();
        foreach (SetValuePair svp in this.allSettValuePairs.GetListFromIndex(reductionWordSize))
        {
            if (WUArrays.IsASubsetB(svp.letter_sett, activeAr))
            {
                possibleReductionSetts.Add(svp);
            }
        }
        return(possibleReductionSetts);
    }
Beispiel #3
0
    /// <summary>
    /// Tries to remove the provided letter from the board
    /// </summary>
    /// <param name="letter">the letter object to remove from the board</param>
    /// <returns>true if successful false if not</returns>
    private bool BoardTryRemoveLetter(LetterGameLetter letter)
    {
        bool suc         = false;
        var  foundLetter = WUArrays.MultiDimFind(tileMap, letter);

        if (foundLetter != null)
        {
            tileMap[letter.XPos, letter.YPos] = null;
            letter.IsOnBoard = false;
            letter.XPos      = -1;
            letter.YPos      = -1;

            this.RefreshLetterCountDisplay();
            suc = true;
        }

        return(suc);
    }
Beispiel #4
0
    /// <summary>
    /// try's to get candidate setts from the active letter sett by
    /// reducing it with different words, the extent of the trying
    /// is hardcoded
    ///
    /// if a set is found the active letter sett is automatically changed
    /// </summary>
    /// <returns>a list of candidate sets, can be empty if none are found</returns>
    List <SetValuePair> GetFromReduced()
    {
        int activeSetLen   = this.activeLetterSet.Count;
        int tmpRedWordSize = this.reduceWordSizeMin;

        string[] activeAr = this.activeLetterSet.ToArray();

        List <SetValuePair> possibleLetterSetts = new List <SetValuePair>();

        var rand = new System.Random();

        while (true)
        {
            if (tmpRedWordSize >= activeSetLen - this.startWordSearchAt || tmpRedWordSize >= this.reduceWordSizeMax)
            {
                return(possibleLetterSetts);
            }

            List <SetValuePair> possibleReductWords = this.GetPossibleReductions(tmpRedWordSize);
            int tmpCount = possibleReductWords.Count;
            for (int i = 0; i < tmpCount; i++)
            {
                int          currentTest = rand.Next(0, possibleReductWords.Count);
                SetValuePair posR        = possibleReductWords[currentTest];

                List <String> tmpActiveSequence = WUArrays.RemoveAllAFromB(posR.letter_sett, activeAr);
                tmpActiveSequence.Sort();

                List <SetValuePair> possibleSetts = this.FindNextLetterSetts(tmpActiveSequence.ToArray());
                if (possibleSetts.Count > 0)
                {
                    //print($"Reduced letter sett by {tmpRedWordSize} caracters");
                    this.activeLetterSet = tmpActiveSequence;
                    return(possibleSetts);
                }

                possibleReductWords.RemoveAt(currentTest);
            }

            tmpRedWordSize += 1;
        }
    }
Beispiel #5
0
    /// <summary>
    /// Generates a letter that will in combination with the
    /// Earlier generated letters for on or more words*
    ///
    /// *if the algorithm cant find or reduce down to get a letter a random one will be picked
    /// </summary>
    /// <returns> the generated letter</returns>
    public String GenerateLetter()
    {
        List <SetValuePair> nextUsedSets = new List <SetValuePair>();

        string[] activeAr = this.activeLetterSet.ToArray();

        if (this.activeLetterSet.Count >= this.startWordSearchAt)
        {
            // try normal generation
            nextUsedSets = this.FindNextLetterSetts(activeAr);

            // no normal found try find from reduced
            if (nextUsedSets.Count == 0)
            {
                nextUsedSets = this.GetFromReduced();
            }

            if (nextUsedSets.Count > 0)
            {
                activeAr = this.activeLetterSet.ToArray(); // if it has been reduced in reduce
                var          rand     = new System.Random();
                int          val      = rand.Next(0, nextUsedSets.Count);
                SetValuePair usedSett = nextUsedSets[val];

                String nextLetter = WUArrays.RemoveAllAFromB(activeAr, usedSett.letter_sett)[0];

                this.AddLetter(nextLetter);
                return(nextLetter);
            }
        }

        // ether the letter search yielded no results or the word was to short
        String l = this.letterFrequency.GetLetterByFrequency();

        this.AddLetter(l);
        return(l);
    }
Beispiel #6
0
 private LetterGameLetter[] TryGetConnectedLetters(int xPos, int yPos, int dimension)
 {
     return(WUArrays.GetConnected(this.tileMap, xPos, yPos, dimension));
 }
Beispiel #7
0
    /// <summary>
    /// Gets all the connected non null values from the
    /// provided x,y coordinates in ether x or y direction
    /// if the starting point is null null is returned
    /// </summary>
    /// <param name="ar">the array </param>
    /// <param name="x">the x cord of the starting point</param>
    /// <param name="y">the y cord of the starting point</param>
    /// <param name="axis">the axis to search 1 is Y axis(row) 0 is X axis(col)</param>
    /// <typeparam name="T">the type of the array</typeparam>
    /// <returns>An array of all the connected elements form the starting point in the given dir</returns>
    public static T[] GetConnected <T>(T[, ] ar, int x, int y, int axis)
    {
        int xSize = ar.GetLength(0);
        int ySize = ar.GetLength(1);

        if (x >= xSize || y >= ySize)
        {
            // if the targetet pos is outside the array throw an exception
            throw new IndexOutOfRangeException("Target possision out of range");
        }
        else if (EqualityComparer <T> .Default.Equals(ar[x, y], default(T)))
        {
            // if the target pos is null return null
            return(null);
        }

        int lowerBound;
        int upperBound;
        int startIndex;

        T[] valuesList;

        if (axis == 1) // Y
        {
            valuesList = WUArrays.GetCol(ar, x);
            startIndex = y;
        }
        else   // X
        {
            valuesList = WUArrays.GetRow(ar, y);
            startIndex = x;
        }

        lowerBound = valuesList.GetLowerBound(0);
        upperBound = valuesList.GetUpperBound(0);

        // find upper bound
        for (int i = startIndex; i <= upperBound; i++)
        {
            if (EqualityComparer <T> .Default.Equals(valuesList[i], default(T)))
            {
                upperBound = i - 1;
                break;
            }
        }

        // find lower bound
        for (int i = startIndex; i >= lowerBound; i--)
        {
            if (EqualityComparer <T> .Default.Equals(valuesList[i], default(T)))
            {
                lowerBound = i + 1;
                break;
            }
        }

        int diff = upperBound - lowerBound;

        T[] ret = new T[diff + 1];

        for (int i = 0; i <= diff; i++)
        {
            ret[i] = valuesList[lowerBound + i];
        }

        return(ret);
    }