9

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

在此处输入图像描述

然后在 WPF 方面,我阅读了文本文件并在面板中填充了文本框,但问题是填字游戏有向下和跨越提示,引导您找到答案,每个提示都有一个数字来指示哪个是哪个。但是,我想不出一种方法将数字的谜题编号与向下和跨数字的提示联系起来。这是它现在的样子:

在此处输入图像描述

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

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

在此处输入图像描述

这是提示(上下)和答案:

在此处输入图像描述

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

感谢任何帮助,将数字分配给拼图中的 Across 和 Down。

这是我显示谜题的代码:

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

显示 Across 和 Down 的代码:

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

        }
    }
4

1 回答 1

1

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

在这里考虑您的域。填字游戏。如果我们是一个阅读报纸的谜题,而你不知道线索的答案,当你问朋友时你会说什么。

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

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

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

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

您可以通过 2 种方式实现这一点。

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>

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

于 2013-08-15T10:48:14.017 回答