加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#中的填字游戏算法

发布时间:2020-12-16 02:03:04 所属栏目:百科 来源:网络整理
导读:我正在为我的单一任务做填字游戏.几乎完成了,但这是一个我无法解决的问题. 我们需要加载包含已完成的填字游戏的csv文件,但我们需要进行一些验证以确保填字游戏文件有效. 限制是: 言语无法重复. 单词可以水平放置,但只能从左向右运行. 单词可以垂直放置,但只
我正在为我的单一任务做填字游戏.几乎完成了,但这是一个我无法解决的问题.

我们需要加载包含已完成的填字游戏的csv文件,但我们需要进行一些验证以确保填字游戏文件有效.

限制是:

>言语无法重复.
>单词可以水平放置,但只能从左向右运行.
>单词可以垂直放置,但只能从高到低.
>水平字必须与一个或多个垂直字相交.
>垂直单词必须与一个或多个水平单词相交.
>每个单词必须用空格或网格边界分隔.

我已经完成了上面的所有限制,但我仍然坚持到这里

>您只能拥有一组连接的单词,即一组连接的单词
单词不能与另一组连接的单词断开连接.

填字游戏文件的一部分是这样的:(由于缺乏声誉,我无法上传图片)

R   O   B   E   R   T           
        I                       
J   I   L   L                   
E       L           J   O   H   N
S                           A   
S       M   A   R   Y       R   
I       A       O           R   
C       R       G   A   R   Y

…….

到目前为止,我所做的与此有关:

>名为Crozzle的类表示此填字游戏文件.其中一个属性是
public List CrozzleWords,包含文件中的所有单词.
>类名WordInCrozzle代表Crozzle中的每个单词.每个单词都有一个属性来记录交叉点的位置.例如单词’ROBERT’与单词’BILL’有一个交点,交叉点的位置是(int)[0,3],交叉处的字母是’B’.
> WordInCrozzle类中的单词具有另一个名为Direction的属性,它表示单词的方向,可以是垂直方向也可以是水平方向.

这是我的解决方案:

public bool ContainsOneGroup()
    {
        bool flag = true;

        // a temp crozzle word list
        List<WordInCrozzle> tempWords = _crozzle.CrozzleWords;

        // start from the first item in wordlist,whatever which word is using
        WordInCrozzle word = tempWords[0];
        if (word.IntersectionPosition.Count > 0)
        {
            // step1. get a word randomly 'Word'  OK -- WordInCrozzle word = tempWords[0];
            // step2. get wordInCrozzle List  OK -- List<WordInCrozzle> tempWords = _crozzle.CrozzleWords;
            // step3. find the intersection position(s) of the word 'Word' and store it to a temp list 'positionOfIntersection'  OK -- List<int[]> positionOfIntersection = word.IntersectionPosition;

            List<int[]> positionOfIntersection = word.IntersectionPosition;

            // remove the first word
            tempWords.Remove(word);
            //crozzleBackup.CrozzleWords.Remove(word);

            // step4. if can grab an intersection position from 'positionOfIntersection' (means any)
            while (positionOfIntersection.Any())
            {
                foreach (WordInCrozzle w in tempWords)
                {
                    for (int i = 0; i < w.IntersectionPosition.Count; i++)
                    {
                        if (ArraysEqual(w.IntersectionPosition[i],positionOfIntersection[0]))
                        {
                            w.IntersectionPosition.Remove(positionOfIntersection[0]);
                            positionOfIntersection.Remove(positionOfIntersection[0]);
                            //tempWords.CrozzleWords[i].IntersectionPosition.Remove(w.IntersectionPosition[i]);
                            if (w.IntersectionPosition.Count > 0)
                            {
                                // store the positionOfIntersections,if this is null,and still have word in tempWords,means there are more than one group of words
                                positionOfIntersection.AddRange(w.IntersectionPosition);

                            }
                            // after get the position,remove the word
                            tempWords.Remove(w);
                        }
                    }
                }
            }
            // step9. if there is no more intersection position left,and no word in wordInCrozzle List,means only one group in the crozzle
            //          Otherwise,more than one group of word
            if (tempWords.Any())
            {
                _errors.Add(new Error(ErrorType.CrozzleError,"More than one group of connected words found"));
                flag = false;
            }
        }
        else
        {
            _errors.Add(new Error(ErrorType.CrozzleError,"More than one group of connected words found")); // if there is no intersection in a word,means there must more than one group of words
            flag = false;
        }
        return flag;
    }

但是当我运行它时,我有一个’System.InvalidOperationException’,它告诉我当我正在做foreach时我无法修改tempWords.

谁能告诉我怎么做?或者是否有任何算法可以找出填字游戏文件中只有一组单词?

解决方法

枚举时不允许从数组中删除元素.我通常做的只是制作数组的副本并枚举一下:

foreach (WordInCrozzle w in tempWords.ToArray())
{
    ...
    tempWords.Remove(w);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读