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

use*_*998 9 c# wpf

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

在此输入图像描述

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

在此输入图像描述

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

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

在此输入图像描述

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

在此输入图像描述

我正在使用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()
Run Code Online (Sandbox Code Playgroud)

代码显示横向和向下:

    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);

        }
    }
Run Code Online (Sandbox Code Playgroud)

Gus*_*dor 1

您可能需要在这里重新定义您的数据模型,因为我认为您在第一个障碍 - 系统分析上失败了。当我们只想编写代码并且总是意味着重大重构时,它会让我们感到困惑。

在这里考虑一下您的域名。填字游戏。如果我们正在读报纸拼图,而你不知道某条线索的答案,那么当你问朋友时你会怎么说。

6 个字母,线索是“blah blah”

...后面是您已经知道的任何字母。

现在我们知道这个谜题需要知道每个答案中有多少个字母,并且每个答案都需要一条线索。我们还知道,在您填写之前,字母是隐藏的,但我们需要在某些时候知道正确的答案。

谜题如何出现在纸张的背面?线索不是写成 1、2、3 等。它们是 4 向下、1 横向等。现在我们知道您需要存储一些位置数据。

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

1. 将每条线索作为自己的条目并附有文字

线索“1”方向“交叉”位置“1,1”回答“你好”描述“问候”

根据条目计算出网格大小并相应地定位字母。优点:易于合作。所有信息都集中在一处 缺点:可能会损坏数据。该方法可以在同一位置定义2个不同的字母。

2. 单独的条目但在网格中回答文本

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

您的代码必须:

  1. 计算出网格尺寸
  2. 填充网格
  3. 填充线索列表
  4. 将用户条目转换为 CSV 文本文件,以便您可以根据答案验证输入并
  5. 告诉用户如果

至于将线索链接到条目文本框。通过包含该字母的线索的描述来设置Tooltip每个属性。textbox他们做对了。

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

<Grid>
  <TextBlock x:Name="TextBlock_NumberLabel"/>
  <TextBox x:Name="TextBox_LetterEntry"/>
<Grid>
Run Code Online (Sandbox Code Playgroud)

在任何需要数字的方块中使用它而不是纯文本框。