如何在WPF中创建填字游戏?

时间:2013-08-15 06:15:23

标签: c# wpf

我创建了10 x 10个TextBox,用户应该将单词输入到相关TextBox中以获取单词的位置。我将所有内容保存到文本文件中:

enter image description here

然后在WPF方面,我阅读文本文件并在面板中填充TextBoxes,但问题是填字游戏已经关闭并且提示会引导您回答,每个提示都会有一个数字来指示哪个是哪个。但是,我想不出一种方法可以将数字的拼图编号与数字和数字之间的提示联系起来。这就是现在的样子:

enter image description here

注意数字(我在油漆中编辑它们以显示我想要的东西)在横向和向下旁边,我需要显示这些数字。

在我的数据库中,我将文件的位置存储在一个表中,并将提示和答案存储在另一个表中,如下所示:

enter image description here

这是提示(横向和向下)和答案:

enter image description here

我正在使用Entity框架lambda表达式来检索横向和向下。

感谢任何有关此问题的帮助,以便将数字分配给拼图中的“横越”和“下”。

这是我展示拼图的代码:

  protected void Across()
    {
        IList<ModelSQL.puzzlecontent> lstAcross = daoPuzzleContent.GetAcross();

        foreach (ModelSQL.puzzlecontent lista in lstAcross)
        {
            Label tbA = new Label();
            tbA.Content = lista.Hint;
            tbA.Width = Double.NaN;
            tbA.BorderBrush = Brushes.CadetBlue;
            tbA.BorderThickness = new Thickness(2);
            stackPanel1.Width = Double.NaN;
            stackPanel1.Children.Add(tbA);
            words.Add(lista.Answer);

        }
    }

    protected void AddPuzzle()
    {
        // foldername of the txt file.
        //  using (StreamReader reader = File.OpenText((@daoWordPuzzle.GetfileURL())))
        string[] fileData = File.ReadAllLines(@"C:\Users\apr13mpsip\Desktop\OneOrganizer\OneOrganizer\WordPuzzle\educational.txt");

        string[] lineValues;

        int row = 0;
        int col;
        int hint = 1;

        string[][] rowcol = new string[fileData.Length][];

        foreach (string line in fileData)
        {
            lineValues = line.Split(new string[] { "," }, StringSplitOptions.None);


            rowcol[row] = new string[lineValues.Length];

            col = 0;

            foreach (string value in lineValues)
            {
                rowcol[row][col] = value;
                col++;
            }
            row++;

        }


        for (int i = 0; i < rowcol.GetLength(0) ; i++)
        {
            for (int j = 0; j < rowcol[i].GetLength(0) ; j++)
            {


                int iadd =  i+1 < rowcol.GetLength(0) ? i+1 : 100;
                int iminus = i-1 >= 0 ? i-1 : 100;
                int jadd =  j+1 < rowcol.GetLength(0) ? j+1 : 100;
                int jminus = j-1 >= 0 ? j-1 : 100;
                var self = rowcol[i][j]; // current value

                var top = iminus == 100 ? "" : rowcol[iminus][j];
                var bottom = iadd == 100 ? "" : rowcol[iadd][j];
                var left = jminus == 100 ? "" : rowcol[i][jminus];
                var right = jadd == 100 ? "" : rowcol[i][jadd];

                //ACROSS HORIZONTAL
                if (
                    (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(top) && String.IsNullOrEmpty(left)) ||
                    (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(right) && String.IsNullOrEmpty(bottom) && !String.IsNullOrEmpty(top) && String.IsNullOrEmpty(left)) ||
                    (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(right) && String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(top) && String.IsNullOrEmpty(left)) ||
                    (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && !String.IsNullOrEmpty(top) && String.IsNullOrEmpty(left))
                  )
                {
                    wordAcross = "";
                    for (int k = 0; k < 10; k++)
                    {
                        wordAcross += rowcol[i][k];
                        if (k == 9)
                        {
                            puzzlewordAcross.Add(wordAcross);
                            // print hello and live
                        }
                    }

                }

                //DOWN VERTICAL
                if (
                     (!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(left)) ||
                     (!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(top) && String.IsNullOrEmpty(left)) ||
                     (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(top) && String.IsNullOrEmpty(left))
                    )
                {

                    wordDown = "";
                    for (int k = 0; k < 10; k++)
                    {
                        wordDown += rowcol[k][j];
                        if (k == 9)
                        {
                            puzzlewordDown.Add(wordDown);
                            // print holy and leducated 
                        }


                    }
                }

                //Check Top , Left , Bottom , Right value.                      
                if (
                    (!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(left)) ||
                    (!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(top) && String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && !String.IsNullOrEmpty(left)) ||
                    (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(left)) ||
                    (!String.IsNullOrEmpty(self) && !String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(left)) ||
                    (!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(top) && String.IsNullOrEmpty(right) && !String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(left)) ||
                    (!String.IsNullOrEmpty(self) && String.IsNullOrEmpty(top) && !String.IsNullOrEmpty(right) && String.IsNullOrEmpty(bottom) && String.IsNullOrEmpty(left))

                   )
                {

                    TextBox tbox = new TextBox();
                    tbox.Height = 50;
                    tbox.Width = 50;
                    tbox.Text = hint.ToString();
                    wrapPanel1.Children.Add(tbox);



                    tbox.GotFocus += (source, e) =>
                    {
                        if (!string.IsNullOrEmpty(tbox.Text))
                        {
                            string Str = tbox.Text.Trim();
                            double Num;
                            bool isNum = double.TryParse(Str, out Num);
                            if (isNum)
                                tbox.Text = "";
                        }
                        else
                        {
                            tbox.Text = "";
                        }

                    };

                    hint++;
                }
                else
                {
                    TextBox tbox2 = new TextBox();
                    tbox2.Height = 50;
                    tbox2.Width = 50;
                    if (String.IsNullOrEmpty(self))
                    {
                        tbox2.Background = Brushes.Black;
                        tbox2.Focusable = false;
                    }
                    wrapPanel1.Children.Add(tbox2);
                }// end of top bottom left right.

            }


        }
    } // End of AddPuzzle()

显示横向和向下的代码:

    protected void Down()
    {
        IList<ModelSQL.puzzlecontent> lstDown = daoPuzzleContent.GetDown();

        foreach (ModelSQL.puzzlecontent listd in lstDown)
        {
            Label tbD = new Label();
            tbD.Content = listd.Hint;
            tbD.Width = Double.NaN;
            tbD.BorderBrush = Brushes.CadetBlue;
            tbD.BorderThickness = new Thickness(2);
            stackPanel2.Width = Double.NaN;
            stackPanel2.Children.Add(tbD);


        }
    }

    protected void Across()
    {
        IList<ModelSQL.puzzlecontent> lstAcross = daoPuzzleContent.GetAcross();

        foreach (ModelSQL.puzzlecontent lista in lstAcross)
        {
            Label tbA = new Label();
            tbA.Content = lista.Hint;
            tbA.Width = Double.NaN;
            tbA.BorderBrush = Brushes.CadetBlue;
            tbA.BorderThickness = new Thickness(2);
            stackPanel1.Width = Double.NaN;
            stackPanel1.Children.Add(tbA);
            words.Add(lista.Answer);

        }
    }

1 个答案:

答案 0 :(得分:1)

您可能需要在此重新定义数据模型,因为我认为您在第一个障碍 - 系统分析时失败了。当我们只想编写代码并且总是意味着一个大的重构时,它会抓住我们所有人。

在此处考虑您的域名。填字游戏。如果我们正在阅读报纸谜题而你不知道线索的答案,那么当你问朋友时你会怎么说。

  

6个字母,线索是'blah blah'

...后面跟着你已经知道的任何信件。

现在我们知道这个谜题需要知道每个答案中有多少个字母,每个答案都需要一个线索。我们也知道字母是隐藏的,直到你填写它们,但我们需要知道正确的答案。

这个谜题如何出现在论文的后面?线索不是1,2,3等。它们是4倒,1横跨等等。现在我们知道你需要存储一些位置数据。

你可以通过两种方式实现这一目标。

<强> 1。将每个线索作为自己的条目完成文本

线索'1' 方向'跨越' 位置'1,1' 回答'你好' 描述'问候'

根据条目和位置字母计算出网格大小。 优点:易于使用。所有信息都在一个地方 缺点:可能的数据损坏。此方法可以在同一位置定义2个不同的字母。

<强> 2。单独的条目,但在网格中回答文本

这与您现在的方式非常相似,但您将文本分隔为CSV网格,如第一个屏幕截图所示。然后,您可以使用方法1中的线索条目,但省略“答案”字段。

您的代码必须:

  1. 计算网格尺寸
  2. 填充网格
  3. 填充列表 线索
  4. 将用户条目转换为CSV文本文件,以便您 可以根据答案验证输入和
  5. 告诉用户是否
  6. 至于将线索链接到输入文本框。设置每个Tooltip的{​​{1}}属性,并附上包含该字母的线索的说明。     他们做对了。

    最后(这可能就是你想要的那个),在条目文本框中添加正确的数字,你必须利用WPF的布局管道。不要只是在你的网格中放置一个文本框,放入另一个网格!我将向您展示它在XAML中的外观,但您可能希望在代码中生成它。

    textbox

    在您想要数字的任何方格中使用它代替纯文本框。